Friday, August 13, 2010

4. Working with variables

So now you got variables but what are you going to do with them?!?  Well here's a couple things that you can do.

First, you can do CRAZY math!!!!!!!
Here's some math shtuff.

int a = 1;
int b = 2;
int c = a+b;
double d = (c+b)/2;
System.out.println(a + " " + b + " " + c + " " + d;

Take a guess at what that will print out and then create a new class and compile it.  
(Remember that it is good practice to put it inside a main method before it will run!)
(Those spaces inside the quotes print out, spaces!)

Now what about Strings and chars?  Here you go!

String a = "Hi there";
char b = '!';
System.out.println(a + b + "!!!!");

Take a guess at what that does!!!

Now try doing this.
1.  Create two ints, one contains 4 and the other contains 6 (call the ints a and b)
2.  Now create a double that contains a + b / 2
3.  Now make a String that contains The number is (then insert your double here) (remember to use quotation marks around: The number is)
4.  Now print out the string!

If you have any troubles just let me know (this may be a bit confusing so if you need help feel free to comment and I'll help!!!)

No comments:

Post a Comment