Kilobolt
  • Home
  • Tutorials
    • Game Development Tutorial >
      • Unit 1: Beginning Java >
        • Before you begin...
        • Day 1: Setting Up
        • Day 2: Java Basics
        • Day 3: More Basics
        • Day 4: Java Math
        • Day 5: More Math
        • Day 6: If... else...
        • Day 7: More Control Flow
        • Day 8: Looping
        • Day 9: More on Looping
        • Day 10: Inheritance, Interface
        • Day 11: Threads and Graphics
      • Unit 2: Creating a Game I >
        • Day 1: Foundations
        • Day 2: Basic Framework
        • Day 3: Taking User Input
        • Day 4: Enter the Robot
        • Day 5: Background and Sprites
        • Day 6: Adding Enemies
        • Day 7: Shooting Bullets
        • Day 8: Animations
        • Day 9: 2D-Arrays
        • Day 10: Painting the Tilemap
      • Unit 3: Creating a Game II >
        • Day 1: Level Creation - Part 1
        • Day 2: Level Creation - Part 2
        • Day 3: Level Creation - Part 3
        • Collision Detection Basics
        • Day 4: Collision Detection Part 1
        • Day 5: Collision Detection Part 2
        • Day 6: Collision Detection Part 3
        • Day 7: Health System & Death
        • Day 8: Basic AI & Final Touches
      • Unit 4: Android Game Development >
        • Day 1: Introduction to Android
        • Day 2: Setting up for Development
        • Day 3: Creating our First Android Application
        • Day 4: Parts of an Android Application
        • Day 5: The Android Game Framework: Part I
        • Day 6: The Android Game Framework: Part II
        • Create an Android Game From Scratch (or port your existing game)
        • Day 7: Creating an Android Game (From Start to Finish)
      • Reference Sheet
    • Zombie Bird Tutorial (Flappy Bird Remake) >
      • Unit 1: Building the Game >
        • Introduction
        • Day 1: Flappy Bird - An In-depth Analysis
        • Day 2: Setting up libGDX
        • Day 3: Understanding the libGDX Framework
        • Day 4: GameWorld and GameRenderer and the Orthographic Camera
        • Day 5: The Flight of the Dead - Adding the Bird
        • Day 6: Adding Graphics - Welcome to the Necropolis
        • Day 7: The Grass, the Bird and the Skull Pipe
        • Day 8: Collision Detection and Sound Effects
        • Day 9: Finishing Gameplay and Basic UI
        • Day 10: GameStates and High Score
        • Day 11: Supporting iOS/Android + SplashScreen, Menus and Tweening
        • Day 12: Completed UI & Source Code
    • Android Application Development Tutorial >
      • Unit 1: Writing Basic Android Apps >
        • Before you begin...
        • Day 1: Android 101
        • Day 2: Getting to Know the Android Project
        • Day 3: The Development Machine
        • Day 4: Building a Music App - Part 1: Building Blocks
        • Day 5: Building a Music App - Part 2: Intents
        • Day 6: Building a Music App - Part 3: Activity Lifecycles
  • New Forum
  • About Us
    • Contact Us
  • Our Games
    • TUMBL: FallDown
  • Facebook
  • Twitter

GAME DEVELOPMENT TUTORIAL: DAY 1-4: Java Math

9/16/2012

149 Comments

 
Picture
Lesson #1-6: Fun(?) with Math
Not bored yet? Is Java beginning to interest you now? :)

I hope you don't dislike Math. If you do, don't let this hatred spill over to your feelings about Java and game development! 

I'm happy to tell you that Math is pretty straightforward. Let's begin with a few arithmetic operators. 
Picture
How to follow Examples: 
(if you are writing these programs yourself in Eclipse, your procedure should be:)

1. Create a new class file in the src folder of your Eclipse project, and name it MathLearning.
2. Start by declaring the class (if it's not declared already):  

class MathLearning{

}

2. Add the main method inside:

class MainLearning{

   public static void main(String[] args){

   }

}

3. Add the variable declarations:
     static int a = 10... and so on.
4. Write the first 10 statements of the main method.
5. Define the blahBlahBlah(int output) method below the main method, and write its single statement.
6. Finish the est of the main method (the last 5 statements)

***Remember that // denotes comments. Do not write those! :) ***
Picture
Picture
Before you copy this code into Eclipse, let's see if you know what the code will do.
Try and follow the code to see if you can write down the five outputs.

If you get stuck, check below:

1. We first declare a new class called MathLearning.
2. Next we declare 3 class-wide variable integers: a, b, and c.
3. We move on to create the main method with its typical parameter (String[] args).
4. Then we create 5 integers. Unlike before, we don't initialize (meaning we don't give them a starting value).
5. We assign values to the 5 integers that we created in step 4.
6. The statements that begin with blahBlahBlah... are invoking the blahBlahBlah method with a required integer parameter.
7. We setup the blahBlahBlah(int output) method.

Important notes:

The location of the blahBlahBlah(int output) method does not matter. We can put it above the main method or below the main method.
The main method, if you recall, will ALWAYS run first.

When the statements from step 6 above are "reached" (again, think Top to Bottom in the main method), Java will look for the method by the name of blahBlahBlah and follow 
the instructions contained therein.

The resulting output is... 
Picture
Wait a second. Everything makes sense, but resultFour is completely off!
22/15 is not 1... or is it?

Remember that the variables c and b (22 and 15) are both integers.
resultFour, which is calculated using c / b is also an integer. This means that when the calculation happens, the computer will
round down to the nearest whole number to keep the value of resultFour as an integer, as in the remainder is just thrown away. 
This is why you do not see a decimal result, but a lower value of 1.

The solution? Let's change all the int declarations to double declarations.

There's another problem. We get five lines of numbers, but they aren't labeled.
If we want each of the lines to display:

Result 1 is 25.
Result 2 is -5.
Result 3 is 220.
...
...

What could we do?

There's only one line that outputs text (System.out.println...) within the blahBlahBlah class, and it treats all the inputs the same way.
It has no idea of telling whether a given input is result or resultFive.

This is when we use a counter. 

To do so, we will learn... two more operators today: 
Picture
There are differences between 
Picture
and 
Picture
But the latter is much more commonly used and you can (for the most part) accomplish the exact same things without the former, 
so we won't waste time discussing it at the moment. 


Example of a counter follows: 
Picture
Let's dissect this class. 

1. We declare a Counter class.
2. We create an integer called counter with initial value of zero.
3. We create a main method.
4. Inside the main method, we create a "while loop" (we will learn this soon!)
5. The while loop, as long as counter is below 10, will add one, and display that number.

int counter starts at zero. As soon as Java reaches the while loop, it will add one, and print the new value of counter: 1.
int counter is now 1. The loop will add 1 again and print the new value of counter: 2.
int counter is now 2. The loop will add 1 again and print the new value of counter: 3.
...
int counter is now 10. The while (counter < 10) condition is no longer met, so the loop ends.

________________

Now let's apply this to the first MathLearning class example: 
Picture
Picture
Compare this to today's first example. 

1. I changed all the int declarations to double declarations to show decimal values.
2. I created a static int called counter (if this is not static, each time that the method is invoked, 
the value of counter will be 1, as each instance of this method will get its own counter integer), and used it to label each line of output. 

The console will display: 
Picture
3. Let's talk about this: 
Picture
You are familiar with System.out.println();
But what are the plusses used for?

In this case, these plusses are not additions. They just let you combine different variables and literals into one String (this is called concatenating) that can be displayed
using System.out.println();


So what ("Result " + counter + " is " + output) is really showing is:

"Result counter is output"


And we will conclude Day 4 here. :D

Thanks for reading! 
Confused? Comment below.   
Go to Day 3: More Basics
Go to Day 5: More Math
149 Comments

    Author

    James Cho is the lead developer at Kilobolt Studios. He is a college student at Duke University and loves soccer, music, and sharing knowledge. 


© 2014 Kilobolt, LLC. All rights reserved.