Monday, August 9, 2010

2. Java does math!

 Okay so now that you can print out some words lets try some MATH!!!!!  If you are going to become a programmer then you are going to need some knowledge of math so before we go on stop and think.  Can I deal with math?
If yes then congratulations you're in!
If not then you might want to look for another career.

So, now that we've sorted out that problem lets get down to the coding.

Open up Eclipse and create a new class inside of the project that we created in the last tutorial.  Call the class Math.  Now copy the code from the HelloWorld class and paste it into the Math class.  Now change this line of code:
Public class HelloWorld {
to
Public class Math {
This change is necessary because you have a different class name and if your Math class was named HelloWorld then this might happen (just watch the beginning of the video to get the idea!).  

So anyway, now that you have your new Math class (that isn't named HelloWorld) you need code to put inside it.  How about we see what 1+1 equals!  So just replace:
System.out.println("Hello World");
to
System.out.println(1+1);
Now click that little run button (if that prints out Hello World then do Run  -> Run).  

Now you should see 2 printed out on the console near the bottom of the screen.  Mess around with the code.  Change 1+1 to 187+138 or something.  Good Job!

No comments:

Post a Comment