Saturday, August 14, 2010

5. The for loop

Loops are an incredibly important part of java and that's why I'm going to teach you about them!  There are two very important loops but I'll teach you about them separately.  So here goes for the for loop.

A loop is basically something that loop, or repeats, itself over and over.  It is useful if you need to do something 5 times and don't want to write the code 5 times.

Here's the format of a for loop

for(int i = 0; i < 10; i+=1) {
    (this is the stuff that the loop will do)
}

Some of this stuff will be new but don't worry.  
So let's start from the beginning, I basically said do whatever is inside the loop ten times.
The very first part of the loop is int i = 0; Now you know that semicolons end things so all this statement does is create an int called i that contains 0.  
The second part  is  i < 10; and this is the part that keeps the loop going however many times you specify it should loop.  I said that it should loop while i is less than ten so since i started at 0 it should loop ten times!
The third part is i+=1 notice that this doesn't have a semicolon.  Because it is the last statement of the for loop it doesn't need one, that's just the way that java works.  Anyways this statement adds 1 to i.  So it will increment i by one.  If I had said i+=2 then it would have incremented i by two every time and the for loop would only run 5 times.

Now that we know about for loops lets demonstrate their awesomeness!!!!
Create a class called forloop and create your main method then post this code inside the main method.

for(int i = 0; i < 10; i+=1) {
            System.out.println(i);
}

Now run it and then make i increment by two instead of one and see what happens.  Make the for loop run up to 1000 if you want.  Drop a comment if you need any help, Thanks! 

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!!!)

Wednesday, August 11, 2010

3. Variables

So this tutorial will just educate you about some variables!  Ready?

Here is a variable that you used in the very first tutorial.  It's called a String and what it is is a string of characters.  For example "Hello World" is H-e-l-l-o- -W-o-r-l-d put together!

To create a string you just do:
String x = "Hello World";
String auwbrub = "alkjbal urnileiu";
This would create a String called x that contains Hello World.  
You can call a string (almost) whatever you want and you can put (almost) anything in it as long as it has the quotation marks around it.

The next is an int which you worked with in the second tutorial.  The name int is short for integer which is any non-decimal number.
To create an in you just do:
int x = 6;
int aljbwiubr = 1234;
Just like a String (and most other variables) you can call it (almost) whatever you want and put (almost) whatever in it!  (I won't be repeating this anymore but just know that it applies to all the variables I'll be teaching you about today!!!!!!!!!!)

Variable number 3 is the char.  Char is short for character and it is a variable that contains one character.  So a char could be H or E or L and a string is a bunch of chars put together.  (A char can only contain one character so don't trying putting more that one character in one)

To create a char you just do:
char x = 'H';
char alvjn = 'a';
Notice that the char only uses ' whereas a String uses ".  This is very important so don't try to create a char like this:
char a = "t";
It won't work!

The next (and last) variable (for this tutorial) is the float.  A float is an int but you can have decimals.  An int can only contain a whole number like -9 or 4 but a float can have 6.9248 or -372.89.

To create a float you just do:
float x = 12.34f;
float alburw= 86.3f;
The float is good for precise measurements and blah.  The int is great for simple calculations and simple adding but when you can't use it you need the float.  Now that funny little at the end of the numbers tells the system that is is a float.  There is another variable called the double which I'll tell you about in a second but it is not the same as the float so the system needs a way to tell the difference between a float and a double so you have to add an f to tell the system that it's a float.
(The double is just like the float but can contain larger numbers than the float.  In java variables can only contain as much as they are designed to contain.  If a float can't hold a high enough number then you need to switch to a double.
To create a double you just do:
double x = 12.34;
double agbgleu = 86.3;
Okay so I snuck in another variable but hey, you came here to learn right?)

So now let's combine all that we've learned.  Create a new class called variables and put this code in there:

public class variables {
    public static void main(String[] args)
    {
        String a = "I'm awesome";
        int b = 123;
        char c = 'c';
        float d = 12.34f;
        double e = 12.34;
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);
        System.out.println(e);
    }
}


This shouldn't be anything new to you since we just went over all of it the only thing you need to know is how to call a variable.  To do it you just use it's name like in String a = "I'm awesome"; you can call it later by saying System.out.println(a); so you can store information and call it later.  Just DON'T HAVE TO VARIABLES WITH THE SAME NAME!!!!!  It will cause major problems and could really mess up your program so just don't do it!  If you have any problems just drop a comment.  Thanks!
  

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!

Sunday, August 8, 2010

1. Hello World!

So you wanna be a java programmer eh?  Well first you have to learn the lay of the land.  Now, before you program your first, er, program it would be really nice to have an IDE (integrated development environment).  Eclipse is probably the best and its FREEEEE so head on over and download it!

Now that you have your IDE you need to learn how to use it.  Follow these easy steps to begin!

1.  Open Eclipse (if you can't do this then I'm, I'm, I'm at a loss for words)
2.  Click File -> New -> Java Project name it whatever you want and then click Finish
3.  Then click File -> New -> Class and name it HelloWorld then click Finish

Now that you have your awesome new class you need to fill it with code!!!!!!!!!
So copy and paste the code below (Don't worry I'll explain it)

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


So now let's take a look at the code.

public class HelloWorld {
This is the start point of your class, everything inside the brackets is part of your class.

 public static void main(String[] args)
This is the "main" method.  It is the method that is called when your program runs.

System.out.println("Hello World");
This tells the system to print out Hello World
There are two ways to print
println - prints the message inside the quotation marks and then prints a line (just like hitting enter)
print - prints the message and nothing else
The semicolon at the end of the line of code marks the end of the line of code.  Without it Java wouldn't understand what ended where and it would mess up a whole lotta stuff!

So now that you understand it it's time to run the code.  To do this go to click the green run button!
 Now go to the bottom of your screen and you should see Hello World printed out!
(If you don't see Hello World then go to Window -> Show View -> Console.  Now run it again and it should work)
Congratulations your first java program is complete!  Give yourself a pat on the back and have a drink or something.  If you had any problems just leave a comment and I will get back to you.