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 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.   
 


Comments

Stephen
09/21/2012 12:15am

I'm following this pretty well, I appreciate you explanation of JAVA so far.

Reply
opencart developer link
08/08/2013 5:33am

Nothing wrong if I say that you are really a wonderful writer "Java framework- Run Time Environment". You always raise the social causes in your article which is respectful and admirable for me as an individual. Regards. Good Job.

Reply
Ricardo
09/23/2012 6:06am

Hi, thanks for the explanation, but I really don't get what is a counter. As you said the maximum it goes to conclude the loop is 10, so why it stopped at 5?

Reply
James C
09/23/2012 6:58am

Ricardo, a counter is just a way you can keep track of how many times a loop runs. There is no built in "maximum" like 10. I chose to make it end at 10.

In the Counter class example above, we created a while loop with a counter integer.

We set the condition of this loop to run as long as counter is less than 10: (while (counter < 10))...

And in each repetition (iteration) of this loop, we added +1 to the counter integer, and so that loop will run 10 times until the condition of the counter integer being less than 10 is no longer satisfied.

As to why it stops at 5 in the NEXT EXAMPLE (completely separate from the example above), that is because we never created a while loop to go to 10 in this example. Instead, we created a counter to add 1 each time we call the blahBlahBlah(double output){} method.

In the main method, we run blahBlahBlah 5 times with 5 different inputs (or parameters). Each time that blahBlahBlah is ran, we defined it so that the value of an integer called Counter will rise by one, and print ("Result " + counter + " is " + output).

It will really help if you write these codes yourself on Eclipse and manipulate them, seeing how the results change.

If you have any other questions, let me know!

Reply
Jose Mari Lumanlan
10/03/2012 11:48pm

Impressive tutorial.... :) very informative and helpful..

Reply
10/04/2012 8:38am

Thank you :)

Reply
David
11/05/2012 11:31am

You should link http://forum.xda-developers.com/showthread.php?p=28632593 or type out the code instead of having a picture so people can copy and paste, just a suggestion

Reply
James C.
11/07/2012 12:40am

I thought that since you can already see the output from this page, there's really no need to copy and paste to see the same output.

I'd rather have people type it out line by line and see how it all comes together.

Reply
Rats link
11/07/2012 4:10am

Hi James,

I have already learnt java as a subject in my colleges, but I haven't studied to this extent. It was really nice to travel along with the explanations whatever you have given and its very useful to follow :) Waiting for developing a game in android :D (fingers crossed).

Looking Forward for more from you :)


Regards,
Rats

Reply
richard burns
11/08/2012 4:36pm

Thanks for a great series of tutorials, I am moving along even if it is at a snails pace. :) If I publish an app onto the play store using these tutorials I will throw a few euro your way. Thanks again.

Reply
Shawn
11/12/2012 4:44pm

Apparently I am just too stupid to grasp the concepts depicted here. I am lost, bored and still have no clue.

Reply
James C
11/13/2012 5:02pm

Sorry to hear that. If there's anything I can re-explain, let me know!

Reply
Reece
11/13/2012 7:23pm

Great stuff thank you!

Reply
Jane
11/17/2012 8:08pm

Wait, you want us to declare a class called MathLearning twice, with one inside the other? Why?

Reply
James
11/17/2012 10:35pm

Jane, what do you mean? I do not see any nested classes.

Reply
Jane
11/17/2012 10:45pm

I refer to the following

<blockquote>1. Create a new class file in the src folder of your Eclipse project, and name it MathLearning.
2. Start by declaring the class:
class MathLearning{
}
</blockquote>

Creating a new class file in Eclipse automatically declares the class.

Nick
01/29/2013 6:03pm

No, you CREATE a class called MathLearning. Then you have to DECLARE the class in the first few lines by typing
"class MathLearning{
}"

Reply
James
11/18/2012 12:57pm

I made it more clear. Thanks for letting me know! Some people like to start from a blank page to practice Java when they start out, and I didn't want to leave that out.

Reply
Jane
11/18/2012 2:43pm

Thanks!

Reply
Shmuel Chayempour link
11/23/2012 1:42am

Hi, thanks for the great tutorial. I just have one problem. I don't understand where to put the "class Conuter { etc.". Is that in a new class file in the src folder? Here are my results:
Result 1 is 25.0
Result 2 is -5.0
Result 3 is 220.0
Result 4 is 1.0 //?
Result 5 is 7.0

Reply
Steve
12/07/2012 4:52pm

I had a similar problem. Did you make sure to change the variables to doubles when you first define them? That was my error, and once I switched them to doubles as opposed to integers it worked wonderfully.

Reply
Todor
11/28/2012 4:51pm

Very good tutorials, I learned java programming in school but forgot some stuff .. Thanks for helping me to remind them :))

Reply
Jan
01/04/2013 5:18am

Hi,
My Output is:
"Result 0 is -5.0
Result 0 is 220.0
Result 0 is 1.4666666666666666
Result 0 is 7.0"

I'm confused, I did type every line code by code, my code:
"
class MathLearning {

static double a = 10;
static double b = 15;
static double c = 22;

static int counter = 0;

public static void main(String[] args) {
double result;
double resultTwo;
double resultThree;
double resultFour;
double resultFive;

result = a + b;
resultTwo = a - b;
resultThree = a * c;
resultFour = c / b;
resultFive = c % b;

learn(result);
learn(resultTwo);
learn(resultThree);
learn(resultFour);
learn(resultFive);

}

public static void learn(double output) {

System.out.println("Result " + counter + " is " + output); }

}
"

Thanks for Your Tut & Anwser =)

Reply
StevieB
01/04/2013 4:42pm

Hi Jan,

The problem is that you forgot to put the counter++ in the learn() method, your learn method should look like this:

public static void learn(double output) {
counter++;
System.out.println("Result " + counter + " is " + output);
}

Hope this helps :)

Reply
Peter
01/17/2013 1:16pm

Hey there i have a problem i have followed the instructions till now but i cant seem to see where i have gone wrong :/ when i click to run i get none of those results i get:
5
0
0
0
0
i have things like this
public class MathLearning {

static int a = 10;
static int b = 15;
static int c = 20;

public static void main(String[] args) {

int result;
int resultTwo = 0;
int resultThree = 0;
int resultFour = 0;
int resultFive = 0;

result = a+b;
result = a-b;
result = a*c;
result = c/b;
result = c%b;

blahBlahBlah(result);
blahBlahBlah(resultTwo);
blahBlahBlah(resultThree);
blahBlahBlah(resultFour);
blahBlahBlah(resultFive);
}

public static void blahBlahBlah(int output) {
// TODO Auto-generated method stub

System.out.println(output);
}
and i have to put the 0 above because if i dont it says it has an error and i cant run it!

Reply
James C.
01/20/2013 4:42pm

Your entire:

result = a+b;
result = a-b;
result = a*c;
result = c/b;
result = c%b;

Is wrong.

Reply
Edwin link
01/21/2013 8:22am

i have this errors :
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method printIn(int) is undefined for the type PrintStream

at MathLearning.blahBlahBlah(MathLearning.java:30)
at MathLearning.main(MathLearning.java:22)


where i have gone wrong??

class MathLearning {

static int a = 10;
static int b = 15;
static int c = 22;

public static void main(String[] args){

int result;
int resultTwo;
int resultThree;
int resultFour;
int resultFive;

result = a + b;
resultTwo = a - b;
resultThree = a * c;
resultFour = c / b;
resultFive = c % b;

blahBlahBlah(result);
blahBlahBlah(resultTwo);
blahBlahBlah(resultThree);
blahBlahBlah(resultFour);
blahBlahBlah(resultFive);
}

public static void blahBlahBlah(int output){
System.out.printIn(output);
}

}

Help me

Reply
James C.
01/21/2013 8:50am

System.out.printIn(output);

This is print Ln not print In. It's an l not an i,

Reply
batsman
01/25/2013 3:34pm

im only understanding about half of it, and its easy for me to make mistakes still

ive done some (non java) programming before and im starting to see some conections to it
this is much more complicated tough, but ill just finish the beginner tutorial fiirst, and do some parts over if i dont understand them again later

my output for result 4 was just 1.0
everything else was right so i have no idea what went wrong there

and thanks a lot for the tutorials, they will surely help me understand the basics so i can start working on my first game, well at least finish it this time
i hope you an help me with some more techniques after i finished these tutorialsand dont understand something yet

but thank you

Reply
Nick
01/29/2013 6:12pm

The result for number 4 is indeed 1. The variable is a integer, so the operator must round to the nearest whole number, which is 1. if you change all of the data types to doubles, the results will be more accurate.

Reply
Max
02/02/2013 1:06pm

hey James,

Pretty decent tutorial. Having some trouble following along as a programming newb, but I'm rehashing those sections. Anyway, a small suggestion regarding the print out. When talking about the + signs it would make much more sense if you indicated that when printing out strings, each individual word or space must be enclosed in quotations and separated by a + sign.

1 small suggestion, a few small things need to be explained

Reply
Max
02/02/2013 1:07pm

Oh, whoops. Ignore that last line; it's an error

Reply
Chuck
02/06/2013 5:56pm

I'm not getting a remainder for result3. I have no clue why.

public class Math {
static double a = 8;
static double b = 10;
static double c = 3;
static int counter = 0;
public static void main(String[] args) {
double result;
double result2;
double result3;

result = a + b;
result2 = b - c;
result3 = a % c;

mathResult(result);
mathResult(result2);
mathResult(result3);
}
public static void mathResult(double output) {
counter++;
System.out.println("Result " + counter + " is " + output);
}

}

Reply
Chuck
02/06/2013 6:32pm

Derp. I meant to divide. Opps. :X

Reply
Kadeem
02/07/2013 3:46pm

i have an error with class MathLearning. It says the nested type MathLearning cannot hide an enclosing type.

class MathLearning
{
static class MainLearning
{
public static void main(String[] args)
{
class MathLearning
{

}
}
}
}

Reply
James C
02/09/2013 5:07pm

Your code does not match the example..

Reply
Kadeem
02/07/2013 4:00pm

I keep getting erros so i'm skipping it.i can't find any solutions.

Reply
shawn revels
02/14/2013 12:22pm

Mine also says - Exception in thread "main" java.lang.Error: Unresolved compilation problem:
resultfive cannot be resolved to a variable

at MathLearning.main(MathLearning.java:34)
I believe its refering to

public static void main(String[] args)

I dont understand!! My text looks like this.

class MathLearning {

static int a = 10;
static int b = 15;
static int c = 22;

static int counter = 0;

public static void main(String[] args) {

int result;
int resultTwo;
int resultThree;
int resultFour;
int resultFive;

result = a + b;
resultTwo = a - b;
resultThree = a * c;
resultFour = c / b;
resultFive = c % b;

blahBlahBlah (result);
blahBlahBlah (resultTwo);
blahBlahBlah (resultThree);
blahBlahBlah (resultFour);
blahBlahBlah (resultFive);
}
public static void blahBlahBlah (double output) {
counter++;

System.out.println ("Result" + counter + "is" + output);



}




}

Reply
shawn revels
02/14/2013 12:24pm

Never mind, I had resultfive instead of resultFive. I changed it and it ran fine!

Reply
Tony
02/16/2013 7:44am

Great tutorial it is easy to understand and i am learning a lot the only thing i hope is that you get deeper into explaining why certain things are the way they are ex. main(string[] args).

Reply
HRS
03/14/2013 5:12am

Niceee tutorial....

Reply
Yash
04/02/2013 9:20am

Best tutorial ever!!!!
Faaar better than any java book!!!!
I know the basics of C++, so the code is not so confusing for me, but still I am learning from the beginning because its sooo fun to learn from you....sorry for bad eng..

Reply
Archie
04/04/2013 5:52am

Very nice tutorial...excellent for beginners :D

Reply
Gary
04/09/2013 12:31pm

Hello, thanks for the great tutorial.
I am looking forward to finishing it.
Having fun so far, its a lot better than I thought as I mainly used visual basic in university.
Thanks again

Reply
obed
04/24/2013 10:24pm

Thanks for the great tutorial.

i am only getting bits and pieces of the terminology(not the tuts fault)
is it OK to continue with the tutorial or should i continue to reread until i grasp all the terminology
thanks a bunch

Reply
Dana
05/13/2013 8:37am

Thanks for tutorial - it's AWESOME! Tomorrow I'm starting day 5 - can't wait! (^.^)v

Reply
wawandck link
05/15/2013 7:09am

could you please explain to me the mean of "%"?
I dont get it,
c%b = 7 >>> 22%15 = 7
c%a = 2 >>> 22%10 = 2
a%c = 10 >>> 10%22 = 10
How do we write those in Math?
Thank you

Reply
Brendan S
05/17/2013 4:54pm

% is called "modulo" and returns the result of a division expression. For example, 29%7 would be 1, because it basically multiplies the 7 until it hits as close to the 29 as it can (28) and thenreturns the difference.

Reply
Tibor
05/16/2013 3:36pm

Thanks for great tutorials!
I have a question though. When I declare counter variable as nonstatic, it gives me an error. Why it must be static? The same happens when I declare blahBlahBlah method nonstatic, and I'm confused :p

Reply
Omey Rajebhosale
05/21/2013 6:55am

Thanku...It was soo cool.
you have a talent to express something..
I truly understand everything in this tut. It's my first step to create a game..really luking forward to your help....

yours,
Om

Reply
Albert
05/27/2013 9:07am

Hi Steve! I'm new here, i se it's been a year since you started this but i hope you can reply me :D

So, i got the codes working but my output looks like this:

Result1is25.0
Result2is-5.0
Result3is220.0
Result4is1.4666666666666666
Result5is7.0

How do i add spaces in my outputs? This might not be an important question but i really want to know what causes this. Thanks a lot! :D
Will continue Day 5 tomorrow! :D

Reply
James C.
05/27/2013 9:32am

Albert,

You are missing the spaces. Should be : "Result " + counter + " is " + output.

Also comments often get missed so if you need immediate help please email us at support@kilobolt.com.

Reply
Albert
05/27/2013 10:40pm

Corrected it! I missed the space between the " ".

Thanks a lot Mr. James! :D
Continuing to Day 5!

Trevor
06/04/2013 8:54am

So my results are

Result 2 is -5.0
Result 3 is 220.0
Result 4 is 1.0
Result 5 is 7.0

I don't get the 1.4666.....

My code looks like this

public static void main(String[] args){

// The following are local variables
// They belong to the main method only

double result;
double resultTwo;
double resultThree;
double resultFour;
double resultFive;

//These assign values to the five "result variables".
// (you could've assigned when you declared them above)

result = a + b;
resultTwo = a - b;
resultThree = a * c;
resultFour = c / b;
resultFive = c % b;

//The following states
//invoke the blahblahblah method
//by "imputing" the required
//integer parameter.

blahBlahBlah(result);
blahBlahBlah(resultTwo);
blahBlahBlah(resultThree);
blahBlahBlah(resultFour);
blahBlahBlah(resultFive);
}

public static void blahBlahBlah(double output) {

//Add one to counter.
counter++;

//This method takes the parameter
//and creates a variable called output.
//This variable is then displayed
// in the below statement.

System.out.println("Result " + counter + " is " + output);
}

};


Not quite sure why I'm not getting the correct answer for result 4.

Reply
Louis
06/04/2013 7:14pm

the variables "a" "b" and "c" above the main method need to each be set as "static double" instead of "static int" as well.

Reply
Faran Tariq
06/20/2013 1:31am

Thank You very much

Reply
Chris
06/24/2013 7:07pm

Great tutorial so far, i am stuck though. i get the error that says output cannot be resolved to a variable. My code is
public class MathLearning {

//variables are created below

static double a = 10;
static double b = 15;
static double c = 22;

//setting up counter below

static int counter = 0;
//the above starts the counter at 0



public static void main(String[] args){

//results are created below

double result;
double resultTwo;
double resultThree;
double resultFour;
double resultFive;

//results 1-5 are defined
result = a + b;
resultTwo = a - b;
resultThree = a * c;
resultFour = c / b;
resultFive = c % b;

//below the output method is assigned
blahBlahBlah(result);
blahBlahBlah(resultTwo);
blahBlahBlah(resultThree);
blahBlahBlah(resultFour);
blahBlahBlah(resultFive);

}
//below the output method is given instructions as an integer output
public static void blahBlahBlah(double output)
{
//this adds one to counter
counter++;
}
{
System.out.println("Result "+ counter + " is " + output);
}

}

Reply
Ryan
07/01/2013 12:24pm

Very nice tutorial. I applaud you for taking the time and effort to make it.
I have a problem with my code. It says there are errors in my project.
When I hover the mouse over the word "system", it says that "system cannot be resolved" Do I have something out of place?

class MainLearning {

static int a = 10;
static int b = 15;
static int c = 22;

public static void main(String[] args) {
int result;
int resultTwo;
int resultThree;
int resultFour;
int resultFive;

result = a + b;
resultTwo = a - b;
resultThree = a * c;
resultFour = c / b;
resultFive = c % b;

blahBlahBlah(result);
blahBlahBlah(resultTwo);
blahBlahBlah(resultThree);
blahBlahBlah(resultFour);
blahBlahBlah(resultFive);
}

public static void blahBlahBlah(int output) {
system.out.println(output);
}



Thanks!
}

Reply
Mike Woodhouse
07/21/2013 5:41am

This is an excellent tutorial and I'm settling comfortably into Java. I do have a question though; I didn't declare a.b.c. and d as doubles however for result to resultFive i did do so. The answer for resultFour was an integer despite ResultFour being a double. Could you explain this please.

M

Reply
akshat
07/26/2013 10:53pm

please make easy ,undrstandable tutorials for kids
because i am 12 yr old.

Reply
gaurang
08/10/2013 7:30am

very nice,,!!
your way of explaining things is quite interesting thank you for this tutorial

Reply
Website design templates link
08/22/2013 10:28pm

You are a blogger and now my inspiration. I just love the blog post. Its very informative, interactive and quality content. I wish you all good luck for your coming blogs and posts. Keep sharing!!!!!!

Reply
Get results link
10/05/2013 4:51am

It is an inspiring 'Web design templates" blog post. The issue handles very nicely. I really appreciate the communication skill of yours and definitely come soon by the time you will complete another write-up.

Reply
Arif
09/06/2013 11:21pm

Whats the difference between Double Data type and Float Data Type ?

Reply
dragonmaster61
09/07/2013 8:45pm

I don't get how did the output shown have decimal points... Mine didn't show any.

Reply
Somanabolic Muscle Maximizer link
10/01/2013 10:27pm

Good stuff, keep it coming.

Reply



Leave a Reply

    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.