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

148 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
148 Comments
Stephen
9/20/2012 05:15:07 pm

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

Reply
opencart developer link
8/7/2013 10:33:49 pm

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
drew link
3/21/2016 04:13:00 pm

i really am green but at the end of this page i wanted to find a way to show the outputs labelled in another manner without a counter. is the following acceptable? or is there a more efficient way of doing what i just did?

package kbtutorial;

public class MathLearning {

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

public static void main(String[] args) {

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

example(result);
example2(resultTwo);
example3(resultThree);
example4(resultFour);
example5(resultFive);

}

public static void example(double output) {
System.out.println("The first answer is: " + output);

}

public static void example2(double output) {
System.out.println("The second answer is: " + output);

}

public static void example3(double output) {
System.out.println("The third answer is: " + output);

}

public static void example4(double output) {
System.out.println("The Fourth answer is: " + output);

}

public static void example5(double output) {
System.out.println("the Fifth(final) answer is: " + output);

}
}



This prints:

The first answer is: 25.0
The second answer is: -5.0
The third answer is: 220.0
The Fourth answer is: 1.4666666666666666
the Fifth(final) answer is: 7.0

Reply
Blake Allen
3/21/2016 08:11:59 pm

Drew, the counter really is the quickest and simplest way you could do this. All he is doing is making the increasing the counter value by 1 whenever the blahBlahBlah method is called. So then he puts the counter value in the concatenated string to output whatever result he's talking about (# 1-5). The counter inside the blahBlahBlah method is used so that you DON'T have to write ("the first, second, etc answer is " + output) 5 times.

Ricardo
9/22/2012 11:06:45 pm

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
9/22/2012 11:58:19 pm

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
salman
6/13/2017 05:12:08 am

sorry :)

Reply
Jose Mari Lumanlan
10/3/2012 04:48:02 pm

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

Reply
James C link
10/4/2012 01:38:11 am

Thank you :)

Reply
David
11/5/2012 03:31:51 am

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/6/2012 04:40:17 pm

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
Jason S
11/24/2013 11:44:37 am

Ok that was tripping me out. I read above about copying the code and I assumed that it meant highlighting and using a copy command instead of typing it out. Thanks for clarifying. I thought I was crazy.

Kachal
2/25/2014 12:01:09 pm

I am loving the the style of your tutorials. It is actually fun to learn. Thank you! I too am in favor of the "option" of being able to cut and paste the code rather than typing it all in so I can manipulate the code to understand.

Dave
2/25/2014 09:42:51 pm

Great tutorial James, thanks. People stop asking him to be able to copy and paste the code. Writing it out helps you to learn it better. There's a reason why this is the best tutorial around. Plus, when you make your own apps, you'll have to write out your own code.

Noah
8/19/2014 02:36:30 am

Type it. A lot of it IS repetitive but you'll hate having to look up how to define a class every time in the future.

Rats link
11/6/2012 08:10:34 pm

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/8/2012 08:36:08 am

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 08:44:18 am

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 09:02:26 am

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

Reply
Reece
11/13/2012 11:23:30 am

Great stuff thank you!

Reply
Jane
11/17/2012 12:08:32 pm

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

Reply
James
11/17/2012 02:35:31 pm

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

Reply
Jane
11/17/2012 02:45:02 pm

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
1/29/2013 10:03:45 am

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 04:57:11 am

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 06:43:47 am

Thanks!

Reply
Shmuel Chayempour link
11/22/2012 05:42:45 pm

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/7/2012 08:52:42 am

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 08:51:14 am

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

Reply
Jan
1/3/2013 09:18:15 pm

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
1/4/2013 08:42:12 am

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
Anushka
3/26/2014 04:12:34 am

StevieB, can you tell me why every instance of the learn() method called is considered a separate one? Since one method is calling another method in the same class, is it not the same instance? I fail to understand why 'counter' is declared as a static variable?

Noah
8/19/2014 02:42:47 am

Hey Anushka,
Since we are only dealing with one instance of MathLearning the static is not really necessary. static comes in when you have a lot of instances of a class and you want then to "share" a common variable.

Go back one lesson and look for the versionNumber. James goes over static there.

Peter
1/17/2013 05:16:16 am

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.
1/20/2013 08:42:04 am

Your entire:

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

Is wrong.

Reply
Noah
8/19/2014 02:44:22 am

You just keep over writing result. that is why you get one 5 and all 0s

Edwin link
1/21/2013 12:22:40 am

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.
1/21/2013 12:50:45 am

System.out.printIn(output);

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

Reply
batsman
1/25/2013 07:34:51 am

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
1/29/2013 10:12:10 am

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
2/2/2013 05:06:24 am

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
2/2/2013 05:07:10 am

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

Reply
Chuck
2/6/2013 09:56:57 am

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
2/6/2013 10:32:05 am

Derp. I meant to divide. Opps. :X

Reply
Kadeem
2/7/2013 07:46:06 am

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
2/9/2013 09:07:18 am

Your code does not match the example..

Reply
Kadeem
2/7/2013 08:00:36 am

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

Reply
Noah
8/19/2014 02:47:51 am

it should look like this.

class MathLearning{

public static void main(sting[] args){

}

}

Reply
shawn revels
2/14/2013 04:22:14 am

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
2/14/2013 04:24:40 am

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

Reply
Tony
2/15/2013 11:44:31 pm

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
3/13/2013 10:12:02 pm

Niceee tutorial....

Reply
Yash
4/2/2013 02:20:17 am

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
4/3/2013 10:52:54 pm

Very nice tutorial...excellent for beginners :D

Reply
Gary
4/9/2013 05:31:57 am

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
4/24/2013 03:24:27 pm

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
5/13/2013 01:37:37 am

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

Reply
wawandck link
5/15/2013 12:09:23 am

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
5/17/2013 09:54:21 am

% 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
5/16/2013 08:36:01 am

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
5/20/2013 11:55:14 pm

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
5/27/2013 02:07:14 am

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.
5/27/2013 02:32:37 am

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
5/27/2013 03:40:54 pm

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

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

Trevor
6/4/2013 01:54:51 am

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
6/4/2013 12:14:43 pm

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
Henry
2/20/2014 06:32:13 am

Thanks a bunch for pointing this out. Considering that this is just a simple calculator program wouldn't it be best to set all relevant variables as doubles instead of integers? it seems to be "easier" based on the limited scope of the program.

Faran Tariq
6/19/2013 06:31:58 pm

Thank You very much

Reply
Chris
6/24/2013 12:07:31 pm

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
Noah
8/19/2014 02:52:48 am

You need to move the System.out.println(*stuff*) line into the method blahBlahBlah(double output)

it should look like this...

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

Reply
Ryan
7/1/2013 05:24:25 am

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
siadink
2/17/2014 02:47:13 pm

the 's' in system must be upper case (S)

Reply
Mike Woodhouse
7/20/2013 10:41:49 pm

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
7/26/2013 03:53:40 pm

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

Reply
gaurang
8/10/2013 12:30:51 am

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

Reply
Website design templates link
8/22/2013 03:28:31 pm

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/4/2013 09:51:24 pm

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
Consulting for performance link
10/21/2013 07:45:21 pm

What a nice article! Very useful information has been shared. You really did an awesome work. Thank you so much. Request you to keep sharing in future also.

Arif
9/6/2013 04:21:24 pm

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

Reply
dragonmaster61
9/7/2013 01:45:16 pm

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

Reply
PacMan13x link
10/19/2013 03:05:41 pm

Did you change your variables from "int" to "double?"

Reply
Somanabolic Muscle Maximizer link
10/1/2013 03:27:41 pm

Good stuff, keep it coming.

Reply
Noctis
11/3/2013 06:06:16 pm

class MathLearning{
}
2. Add the main method inside:
class MainLearning{ <<<< You mean "MathLearning" Right ?
public static void main(String[] args){
}

couse i understood class MathLearning{
}
class MainLearning{
public static void main(String[] args){
}

and that gives error :D

Reply
Seo Chan link
11/22/2013 01:21:14 pm

Great tutorial! xD

I have one question, my "Result 4 is 1.4666666666666666".
How to make the result is "1.466..." like the turoial above?

Thanks before ^.^/

Reply
frank
2/10/2014 06:21:27 pm

Hi Chan,
The result really is 1.4666666666666666
1.466.. is what you write when you don't want to type out all the sixes.

Reply
Cheeno
11/25/2013 04:25:22 am

thanks a million..I am 18 ..and i am learning this so easily ...wow ..so excited and thanks for keepin it free mate..
support from Pakistan ..

Reply
Nick
1/15/2014 10:44:01 pm

Great tutorial first of all. Everything seems to be working for the most part, but I was wondering if it matters that my results show with no spaces? (Result1is25.0) Can anybody tell me if this matters or how to fix it? Thanks.

Reply
Nimisha
2/5/2014 08:54:56 pm

When I type- int result; int resultTwo; and etc I get a yellow underline under result and the numbers. Is that normal? I'm only 15 and have a few doubts.
Please help

Reply
Nimisha
2/5/2014 08:58:57 pm

I'm sorry for bothering. It's nothing. The problem is resoved

Reply
jose jayant
2/14/2014 12:36:56 am

What is the purpose of static?why it is used with variable?

Reply
jose jayant
2/14/2014 12:37:16 am

What is the purpose of static?why it is used with variables
?

Reply
Unknown Helper
2/14/2014 09:16:56 am

Jose, we use static to allow every method to manipulate the data it holds. So since the main(parent) class holds the "static int counter =0;" Every method that is in that class will be able to change the final value. We use it with the variable because the loop calls for an incrementing(increasing) value. While(value is less than 10){Print the lines of <value(1)>}. If you did not set a limit for the while loop. It will go on forever(until the program crashes or the computer locks/freezes). And if you were to make the variable's final form non-changeable, the loop will go on forever since counter will equal 0 forever.
I hope this helps any.

Reply
salar
2/14/2014 11:27:21 am

where is output shown i get this in console bar terminated what does it mean

Reply
henry
2/20/2014 06:35:01 am

expand the console view...

Reply
Andy
2/21/2014 03:01:27 am

Hi,

I'm new to Java and have some very very little programming knowledge in VB.

I have the following questions:

1. Why do you declare the variables a,b,c as static?I tried as non-static but it doesn't work, can you explain why?
2.Why isn't it possible to declare the variables inside the main method?
3. I didn't read yet what you wrote on Counter class but I tried setting everyt variable to double and achieved the desired result.What's the difference

Thanks for the great tutorial :)

Reply
Omar Elhajj
2/21/2014 08:42:50 pm

I don't really understand the difference between the operations variable++ and ++variable... Can anyone explain me that?
I also have some doubt with the operation %...
Thanks a lot, and very great tutorial!

Reply
Anushka
3/25/2014 05:00:46 am

variable++ or ++variable both do the same thing to 'variable' i.e. they increment it's current value by 1. But if your were to assign the value of 'variable' (let's say it's 5) to another variable, say 'number', then,
number=++variable;
'number' would have value 6 and so would 'variable'
i.e. the value of 'variable' is first incremented and then assigned to 'number'.
On the other hand,
number=variable++;
'number' would have value 5 and 'variable' would have 6
i.e.value of 'variable' if assigned to 'number' and then 'variable' is incremented

Reply
Dave
2/23/2014 09:51:38 am

Hi James, great tutorial! Best I could fine, thanks for doing it. I'm understanding this pretty well so far but I'm getting an error toward the end. I have it set up exactly as your tutorial and it says "counter cannot be resolved to a variable". I would just skip and move on but I really want to understand this. Thanks!

Reply
Newb Tester
2/24/2014 08:41:03 am

Quick question.
What am I doing if I input.
/Begin Example.

public class counter {
static int counter = 0;

public static void main(String[] args) {
while (counter <=10);

counter++;

System.out.println(counter);

}
}

/End example.

Explain the difference between changing the "{" by "while (counter <10)" to a ";".
And why would it cause the loop not to end.
I did it like that originally on my own...and my computer hung because I dont think it ever stopped counting.

I'm just wondering what's the difference.

Reply
Antonio
2/28/2014 03:00:39 pm

Great tutorial. I learned to program java while I was in college; however, I haven't use it in years and eventually I forgot a lot. I want to start programming again and hopefully create a descent android game. Thanks again for your dedication that you put on these tutorials.

Reply
Anushka
3/26/2014 03:17:42 am

Can someone please tell me why 'counter' has to be declared as a static variable? Won't it be considered as the same instance everytime blahBlahBlah() is called?

Reply
Raghavendra Ganesh
5/5/2014 12:55:51 am

By declaring static, the variable wont renew itself for each instance.

If the variable, i.e. counter is not declared as static, then everytime that the method blahblahblah() is called then the count will be reset and we wont get the count printed as 1 2 3 4 so on.

Reply
emz
3/27/2014 12:47:20 am

>.< whey this is the result when I run? "Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The local variable result may not have been initialized
The local variable resultTwo may not have been initialized
The local variable resultThree may not have been initialized
The local variable resultFour may not have been initialized
The local variable resultFive may not have been initialized

at MathLearning.main(MathLearning.java:33)"

i tried changing the value of the variables but errors keep popping.


public class MathLearning {

// the variables directly below this comment
// are class-wide variables

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

public static void main(String[] args) {

// The following are local variables
/* meaning they belong to the main method only.
*/

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

int result1 = a + b;
int resultTwo1 = a -b;
int resultThree1 = a * c;
int resultFour1 = c / b;
int resultFive1 = c % b;

/*these assign values to the five "result variables"
* you could have assigned when you declared them above
*/

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

/*the following statements invoke
* the blahBlahBlah method
* by inputting the required
* integer parameter
*/

}

public static void blahBlahBlah (int ouput) {

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

System.out.println(output);

}




}

erm im a newbie to java, can anyone pls help me and point out where i did wrong >.<...thanks!

Reply
emz
3/27/2014 01:08:28 am

sorry didnt notice i put "1" on int result1 = a + b;
its now removed but errors still show =/
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Duplicate local variable result
Duplicate local variable resultTwo
Duplicate local variable resultThree
Duplicate local variable resultFour
Duplicate local variable resultFive

at MathLearning.main(MathLearning.java:22)

D: pls help me this is really bugging me

Reply
emz
3/27/2014 01:08:38 am

sorry didnt notice i put "1" on int result1 = a + b;
its now removed but errors still show =/
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Duplicate local variable result
Duplicate local variable resultTwo
Duplicate local variable resultThree
Duplicate local variable resultFour
Duplicate local variable resultFive

at MathLearning.main(MathLearning.java:22)

pls help me this is really bugging me

Reply
emz
3/27/2014 01:09:03 am

sorry didnt notice i put "1" on int result1 = a + b;
its now removed but errors still show
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Duplicate local variable result
Duplicate local variable resultTwo
Duplicate local variable resultThree
Duplicate local variable resultFour
Duplicate local variable resultFive

at MathLearning.main(MathLearning.java:22)

pls help me this is really bugging me

Reply
emz
3/27/2014 01:09:37 am

sorry didnt notice i put "1" on int result1 = a + b;
its now removed but errors still show
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Duplicate local variable result
Duplicate local variable resultTwo
Duplicate local variable resultThree
Duplicate local variable resultFour
Duplicate local variable resultFive

at MathLearning.main(MathLearning.java:22)

pls help me this is really bugging me

Reply
Yaniv
3/28/2014 09:34:40 pm

Reply us your code again without all the comments and stuff.
I wanna see clean code and i will help u.

Reply
khalil
4/20/2014 09:08:40 am

(The First e.g) It was easier than what i was though, because I was thinking that i had to put some thing instead of blahBlahBlah, I did not know that the whole answer is already there, Thanks James.

public class MathLearning {

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

public static void main(String[] agrs){

int result;
int resultTow;
int resultThree;
int resultFour;
int resultFive;

result = a + b;
resultTow = a - b;
resultThree = a * c;
resultFour = c / b;
resultFive = c % b;
blaBlaBla(result);
blaBlaBla(resultTow);
blaBlaBla(resultThree);
blaBlaBla(resultFour);
blaBlaBla(resultFive);

}
public static void blaBlaBla(int output) {
System.out.println(output);


}
}

Reply
Rick
5/12/2014 01:05:28 pm

Hello, I couldn't get it to run at all. Can you tell me what i did wrong?

public class HelloWorld {

public static void main(String[] args) {
System.out.println("Hello Kilobolt!");
}

}

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.println(output);

}
}

Reply
Rick
5/12/2014 02:49:51 pm

Nevermind...fixed that one. Now I have a new problem...

Reply
Rick
5/12/2014 02:51:19 pm

Says "counter cannot be resolved"

class MathLearning {

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

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;

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
Rick
5/12/2014 02:51:29 pm

Says "counter cannot be resolved"

class MathLearning {

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

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;

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
Rick
5/12/2014 02:51:50 pm

Says "counter cannot be resolved".

class MathLearning {

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

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;

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
ahmed
5/19/2014 09:29:31 am

you need to identify counter as an integer = 0
so just write below your class int class = 0; and it will run

Reply
ann susan link
5/29/2014 05:20:58 am

can anyone help please...here is my program
public class MathLearning {
// these are class-wide variables
static double a= 10;
static double b= 15;
static double c= 22;
static int counter= 0;

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



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

blahBlah(result);
blahBlah(resultOne);
blahBlah(resultTwo);
blahBlah(resultThree);
blahBlah(resultFour);









}
public static void blahBlah(double output){
while ( counter<5)

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


}
} ..when i run it i am getting "resultcounter is outpt" five times and not as per the tutorial...why is it so?

Reply
ann susan link
5/29/2014 05:38:08 am

even if i write it as below still i am getting " resultcounter is output" 5 times...
public class MathLearning {
// these are class-wide variables
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 resultOne;
double resultTwo;
double resultThree;
double resultFour;



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

blahBlah(result);
blahBlah(resultOne);
blahBlah(resultTwo);
blahBlah(resultThree);
blahBlah(resultFour);









}
public static void blahBlah(double output){
while ( counter<5)

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


}
}

Reply
weiiew
9/24/2014 05:30:57 am

counter and output should not be in quotes

Reply
shujaat
6/4/2014 11:31:30 am

you rock!!!

Reply
George
6/8/2014 12:14:27 am

Thank your for your explanation. Even though I'm new to this, I can understand this well, It's really helpful!

Reply
nick
7/27/2014 06:14:14 pm

u r awesome man!! its really a good stuff for beginners ...i'll continue with it :)

Reply
Neven
9/7/2014 12:34:38 am

i still dont get it what i need to write and where -.-

Reply
milsev
11/11/2014 11:54:38 am

Hello, I couldn't get it to run at all. Can you tell me what i did wrong?


public 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 (resultFour);
blahBlahBlah (resultFive);
}

public static void blahBlahBlah (int output) {

System.out.println (output);
}
}


this is not complete yet, but the problem is i couldnt get the "resultThree"

so the result is

25
-5
1
7

can you help me please, thanx

Reply
Rahul
1/8/2015 10:01:59 am

add this statement blahBlahBlah (resultThree);

Reply
miskimit
1/8/2015 08:05:54 pm

thank you so much for the great tutorial! :D

it's really helping me. there were some parts i found hard to understand, but once i started playing around and changing the order of things on eclipse, it all clicked perfectly.

i love you!

Reply
Chris
4/2/2015 12:51:26 am

When I click Run, it keeps running the HelloWorld program. How can I get it to run the MathLearning program?

Reply
Daniel
5/22/2015 11:46:41 pm

I wonder the same thing. I get an X and a bulb at resultFive and idk why

Reply
AFR
8/19/2015 06:33:41 am

In case you are still stuck. You have to save the class before you can run it.

Reply
Daniel
5/22/2015 11:45:15 pm

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 (int output){
System.out.println(output);
}
}

I know it's late, but maybe you guys can help me. I have an X and a bulb at resultFive

Reply
Guy
6/5/2015 03:22:55 am

Because its not " Double " ! Change int to Double ..

Reply
Mattias
6/22/2015 04:57:24 pm

Does the post and pre increment thing also apply to --?

Reply
Achmad Hendarsyah link
10/29/2015 11:07:09 pm

as far as i have learned a great tutorial, i appreciate that you share your knowledge with us, keep it up sir.

greetings from Indonesia;)

Reply
Gia
1/4/2016 04:39:46 am

HELP!! .I have literally retyped in everything down to the last letter repeatedly for the last hour but my eclipse program keeps telling me : "Could not find or load main class HelloWorld" (our project from before). I have deleted that class and started this one fresh, there are no errors that the program can find...am I nuts? What am I doing wrong? D:

Reply
AFR
1/4/2016 06:45:10 am

Sorry Gia, we can't help unless we see your code. Just copy and paste code here.

Reply
Gia
1/5/2016 02:30:19 am

Here it is:

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.println(output);
}

}

AFR
1/6/2016 09:42:14 am

code seems fine to me but why is it trying to run "HelloWorld" class. You are running the class "MathLearning" right? Maybe re-create "HelloWorld" class but don't delete it. I still have it in my src folder

Reply
Gia
1/9/2016 02:45:29 pm

I got the "Math Learning" class set up, but it now says:
Error: Could not find or load main class MathLearning

Reply
AFR
1/11/2016 10:53:26 am

how did you fix last issue? seems to me like it would be same fix.

Reply
Omar
4/19/2016 06:54:48 pm

I am not confused, I just wanted to thank you for the great lessons :)

Reply
Altukhova Violetta
4/22/2016 01:06:16 pm

Hi, i am new to the code and have question, i don't have answer to the code displayed in console, is this how it should be?

public class HelloWorld {

public static void main(String[] args) {
System.out.println("Hello Kilobolt!");
}
}
class MathLearning{

static int a = 18;
static int b = 13;
static int c = 7;

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);
}
}

Reply
mitch
4/29/2016 12:48:20 pm

I would remove

public static void main(String[] args) {
System.out.println("Hello Kilobolt!");
}
}

as that is your problem. You don't want two mains

Reply
Mitch
4/29/2016 12:44:11 pm

I like this code sample, I think you could continue it a little more and do something like this:


public class TestMathClass {

// lets declare static vars for the class
static int a = 10;
static int b = 15;
static int c = 22;

static int counter = 0;

// lets write a main method
public static void main(String[] args) {

//lets declare local variables to 'main'
int result1 = 0;
int result2 = 0;
float result3 = 0;
int result4 = 0;
int result5 = 0;

//lets do some math to make us feel special
result1 = a + b;
result2 = a - b;
result3 = a / b;
result4 = a * b;
result5 = a % b;

//execute the method to show results
ShowMeResults(result1);
ShowMeResults(result2);
ShowMeResults(result3);
ShowMeResults(result4);
ShowMeResults(result5);


}

// public class method 'ShowMeResults'
public static void ShowMeResults(int output) {
counter = CounterFun();
System.out.println("("+counter+")"+"result = " + output);
}

// what if we want to see float values ?
// public class method 'ShowMeResults'
public static void ShowMeResults(float output) {
counter = CounterFun();
System.out.println("("+counter+")"+"result = " + output);
}

//totally not needed, added a method to count LOL!
public static int CounterFun() { return (++counter); }
}

Reply
Mitch
4/29/2016 01:09:09 pm

Ooops... I posted the WRONG code above, should have been this:

import java.util.Random;

public class TestMathClass {

// lets declare static vars for the class
static int a = 10;
static int b = 15;
static int c = 22;

// lets get a little random with our code
static Random d = new Random();
static int randomInt = d.nextInt(100);

// we want to do some counting (in a weird way)
static int counter = 0;

// lets write a main method
public static void main(String[] args) {

//lets declare local variables to 'main'
int result1 = 0;
int result2 = 0;
float float_result3 = 0.0f;
int result4 = 0;
int result5 = 0;
int result6 = 0;

//lets do some math to make us feel special
result1 = a + b;
result2 = a - b;
float_result3 = (float) a / (float) b; //we need to cast
result4 = a * b;
result5 = (int) a % (int) b; //don't need (int) but this is casting.
result6 = randomInt;

//execute the method to show results
ShowMeResults(result1);
ShowMeResults(result2);
ShowMeResults(float_result3);
ShowMeResults(result4);
ShowMeResults(result5);
ShowMeResults(result6);
}

// public class method 'ShowMeResults'
public static void ShowMeResults(int output) {
counter = CounterFun();
System.out.println("("+counter+")"+"result = " + output);
}

// what if we want to see float values ?
// public class method 'ShowMeResults'
public static void ShowMeResults(float output) {
counter = CounterFun();
System.out.println("("+counter+")"+"result = " + output);
}

//totally not needed, added a method to count LOL!
public static int CounterFun() { return (++counter); }
}

Reply
Bereshna
8/11/2016 07:51:10 pm

Thank you so much please continue with new and fresh classes

Reply
Ghit Ioan
12/15/2016 09:16:50 am

Hi, thank you for this guide, i dont know really what i'm doing but i really wannt to lern coding. Thanks verry much !

Reply
Walter
4/25/2017 02:24:34 pm

Thanks for making these. Currently quickly refreshing Java and diving into 2D Game creation. This Tuts seem excellent for this. Very well made. However, in this lesson, I think there is some misleading statement. It says that the counter has to be declared as static. While this holds true when declaring the blahBlahBlah method also as static, so the acces only works with counter being also static then, it has nothing to do with that one would really need it to be static in a more realistic use case. As we work only inside one class/object here, the value of the counter wouldn´t stay one otherwise, as described. Even if it was not static. Because its a field and stores the value saved inside "counter" globally. So you can increment the counter even without it being static. It is a bit misleading or confusing I think. Especially could be for beginners. As it is jus not the right reason for "counter" being static here, afaics, and some other comment already asked about this as well. It doesnt correspond well with the former proper explanation of when to use static. Which is when you would want to manipulate the fields of multiple instances simultaneously. Otherwise great lessons so far. Already sneek peeked into the game part:) Really cool series, thanks.

Reply
Anon
5/28/2017 03:56:13 am

Thanks for these tutorials, I nearly understood everything. But what about this:

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

Why does counter need to be declared as a static variable? I don't get it. But that's my only problem so far.

Reply
Maharshi
5/8/2018 10:48:17 am

when i tried the code below it started showing errors ...please tell me is it how it should be ?


public class MathLearning {
static int a = 10;
static int b = 20;
static int c = 30;
public static void main(String[] args){
int result;
int resultTwo;
int resultThree;
int resultFour;
int resultFive;

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

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

public static double blahBlah (int output) {
System.out.println(output);
}
}

}

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. 


© 2014 Kilobolt, LLC. All rights reserved.