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.
(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! :) ***
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...
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:
so we won't waste time discussing it at the moment.
Example of a counter follows:
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:
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:
But what are the pluses used for?
In this case, these pluses are not additions. They just let you combine different variables and literals into one String piece 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.