Sunday, December 19, 2010

Sorry, again!

Well I've kinda forgotten about my blog for a while.  I'm gonna try to keep on posting and maybe introduce new concepts with some games!

Tuesday, October 12, 2010

11. Explanation on GuessingRandomNumber class

Okay so I've just posted the class code here in case you didn't want to download it over at the source code page.  Copy and paste it then look at the comments, they will explain what is going on!!!
  1. import java.util.Scanner;
  2. import java.util.Random;
  3. public class GuessRandomNumber
  4. {
  5.     public static void main(String[] args)
  6.     {
  7.         //Create a new scanner object called input
  8.         Scanner input = new Scanner(System.in);
  9.         //Create a new random object called generator
  10.         Random generator = new Random();
  11.         //Here we will count how many guesses the user has
  12.         int guessCounter = 0;
  13.         //This boolean is true if the user hasn't won
  14.         boolean userHasNotWon = true;
  15.         //This sets the number that the user is trying to guess to a number from 0-20
  16.         int answer = generator.nextInt(20);
  17.         //This while loop will run while the user hasn't won
  18.         while(userHasNotWon == true)
  19.         {
  20.             //Here, we ask the user from between 0-20 and assign it to an int called guess
  21.             System.out.print("Guess a number between 0 and 20: ");
  22.             int guess = input.nextInt();
  23.             //If the guess is over twenty then we tell the user
  24.             if(guess > 20) {
  25.                 System.out.println("The number can't be more than twenty!");
  26.             }
  27.             //If the guess is less than 0 then we tell the user
  28.             if(guess < 0) {
  29.                 System.out.println("The number can't be less than zero!");
  30.             }
  31.             //If the guess is in the right range then we check whether they were right or not
  32.             if(guess >= 0 && guess <= 20) {
  33.                 //If the guess is too high then we tell the user and increment the guess counter by one
  34.                 if (guess < answer) {
  35.                     System.out.println("Too low");
  36.                     guessCounter += 1;
  37.                 }
  38.                 //If the guess is too low then we tell the user and increment the guess counter by 1
  39.                 if (guess > answer) {
  40.                     System.out.println("Too high");
  41.                     guessCounter +=1;
  42.                 }
  43.                 /**If the user guesses the right number then we tell them they won and
  44.                 tell them how many times they had to guess**/
  45.                 if (guess == answer) {
  46.                     System.out.println("YOU WIN!!! And you guessed " + guessCounter + " times!!!");
  47.                     userHasNotWon = false;
  48.                 }
  49.             }
  50.         }
  51.     }
  52. }

Monday, October 11, 2010

10. Random numbers

So our super-cool user input program is, well, super-cool but we want to make a game.  I mentioned making a where the user has to guess the number and you tell the user if the number is too high or too low.  We will also count the number of times they guess and print that out at the end.  However, if we want this game to be truly fun then we can't have the user guessing the same number over and over.  To solve this problem we are going to use random numbers!

To get the code for the random numbers (much like getting the code for the Scanner) you have to import it like so:
import java.util.Random;
So that's that and now to use the code.  In order to create a Random object you do this:
Random generator = new Random();
This creates a new Random object called generator where you can get random numbers from.  To get a random number from it you use this function:
generator.nextInt(10);
This will get a random number between 0 and 10.  So it gets an integer so to be able to use the number you have to assign it to an integer like so:
int r = generator.nextInt(10);
Now the integer r has a random number between 0 and 10!  Now, I have a challenge for you.  Try making the guessing number game I described above!  Don't worry, I'll have the code in the source code section and you can download it from there but try making your own and sending it to me at drenguinp@gmail.com or you can post it in the comments.

Hint:  Use  a while loop that goes while the user hasn't guessed correctly.  Also, use if loops to check if the user has guessed correctly or not. 

What do you want tutorials on?

I will continue with my regular basic java tutorials and hope to keep that going for a while but I was wondering what you guys would like a tutorial on?  I will try to make a tutorial for anything you want just leave a comment telling me what you want!

Sunday, October 10, 2010

9. Comments

So here's a super quick tutorial about comments, this is just because it is important and super useful and I have a little bit of time so anyways.  Comments are a way for programmers to keep track of what they are programming.  The computer doesn't pay attention to them so they are really useful for keeping track of stuff.

To do a single-line comment you do this:
//this is a single line comment
To do a multi-line comment you do this:
/** This is a multiple line comment
I can write on more than one line
and the computer will still not
care what I write!!!!!!**/
You can use comments to label certain parts of your program such as:
//this for loop runs from 1 to 10
See how this makes it easier for the programmer to understand what if going on in the program?  It is good programming practice to leave comments so that you actually understand what you are writing! 
That was a super-quick tutorial, I will post more about the random numbers and such tomorrow (hopefully!).

See you then!

8. The if and else statement

Okay so now that you can read input from the user and do loops and you know about variables and all sorts of stuff it's time to learn about if statements and these things are awesome.  So here a quick example of an if statment:
if(I am hungry) {
    eat food;
}
Basically you just check if something is true and if it is then you do whatever follows.  Fairly simple.  So lets use the user input program that we had, if you don't have it you can follow tutorial #7 and that should catch you up or you can download the code from the source code page on my site.  Anyways, open up the Userstuff java program and change it to this:
import java.util.Scanner;

public class Userstuff
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Please input your name and then click enter: ");
        String name = input.nextLine();
        System.out.print("Please input you age and then click enter: ");
        int age = input.nextInt();
        System.out.println("Hello " + name + " you are " + age + " years old");
        if (age < 16) {
            System.out.print(" and you're still a kid!");
        }
    }
}
So here we are just asking for the name and age, printing it out, and then checking if the person is still a kid.  If they are then you print out "and you're still a kid!"  This is pretty straightforward and pretty simple, maybe too simple.  What about if they are older than 16, you could do a second if statement but that just seems too complicated.  Luckily java already has an answer and is the else statement, lets use my earlier example to explain it better!
if(I am hungry) {
    eat food;
 
else {
    don't eat food;
 
So here we check if I am hungry, if I am I eat food but if I'm not I don't eat food.  So lets use this else statement for the Userstuff program.  After the if statement add this:
else {
    System.out.print(" and you now have responsibilities!");
 
A little insulting but that's not the point.  Now our program will print out " and you now have responsibilities!" if the person is 16 or older.  

So now that you know about the if statement try to make a simple game.  Something where people try to guess a number and you could tell them whether it's too high or too low using if statements!

By the way, I have not made a tutorial about using > or < signs and checking if things are true or not.  I will make a tutorial about these next and maybe one about random numbers so we can make a sweet number guessing game!  See you next time! 

Friday, October 8, 2010

7. Reading Input from the user!!!!!!

Okay so now that you know a couple things about java it's time to interact with your user!

In order to do this you need to use a Scanner which "scans" in what the user enters!  In order to use this scanner you need to have the code for it, don't worry you don't have to download some random code from some site you don't know, it's all on your computer already you just need to access it.  For regular java stuff (like what I've already told you) you don't need any special classes or code, this Scanner is something not included with the regular java stuff.

So enough of that, the code that you use to access the Scanner code is this:
import java.util.Scanner;
This will let you import the Scanner stuff you need.  Now you have to tell the computer you need to import before you actually do anything so this code HAS TO BE AT THE VERY TOP OF YOUR CLASS!!!!!!  Before you write anything put this at the very tippity-top.

In order to set up your scanner you need to do something like this.
Scanner input= new Scanner(System.in);
This creates a new scanner object which you can read data or input from.  (You don't have to call your scanner "input", you could name it "myScanner" or something and it wouldn't make a difference) So now that you have your scanner here's how you read in some code.

To read in the next line of input you do:
String myStringNameGoesHere = input.nextLine();
To read in the next integer you do this:
Int myIntNameGoesHere = input.nextInt();
To read whatever is inputted next (basically the next word) you do this:
String myStringNameGoesHere = input.next();

Okay so lets put it into action, here is the code for a simple java program that reads in you name and your age and then prints it out again:

import java.util.Scanner;

public class Userstuff
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Please input your name and then click enter: ");
        String name = input.nextLine();
        System.out.print("Please input you age and then click enter: ");
        int age = input.nextInt();
        System.out.println("Hello " + name + " you are " + age + " years old!");
    }
}


Create a new class called Userstuff and paste this code into it, hopefully it should work, if not post a comment and I'll try to help you out.  Next I'll show you how to use the if statement so you can tell the user different things depending on what they enter!  Stay tuned.

EDIT:  Now unfortunately this doesn't seem to accommodate long lines of stuff so the code  looks really weird.  If you copy and paste it it should look fine but if not then look at my source code section and download the source code for this.
Or, if your feeling really brave, try writing it in!!!!!

Tuesday, October 5, 2010

Sorry!

So I obviously haven't been posting for a while but I have been caught up with school work and stuff and I'm not sure if I want to keep posting!  I'm trying to get into iPhone programming right now and so I don't have too much time for this blog.  I will try to post little tutorials about Java stuff and may keep doing these simple basic Java tuts.  I'm also trying to figure out a good way to organize the site because it's not organized too well.  Anyways, that what I have to say!  I'll try to post soon!

Sunday, August 15, 2010

6. The while loop

Okay now that you've learned about the for loop you can learn about the while loop!!!!!

The while loop doesn't have all that complicated stuff in the parentheses you just have to put something that is true or false and it will execute it while it is true!  This leads right into a new variable, the boolean!!!!.

To create a boolean you just do:
boolean a = true;
boolean b = 1 < 2;

So both of these statements would be true because we set a to true and we set b to 1 is less than 2 which is also true.  The boolean is nice for while loops because the while loop will keep going until you set a boolean to false. (I'll get to this in just a sec)

So here's how you set up a while loop.

while(1<2) {
   System.out.println("1");


You say while(something is true) do it.  This loop would print out 1 infinitely so it's probably not a good idea to do it right now.  Now here's a way to use booleans in while loops. 

boolean c = true;
while(c) {
   System.out.println("c is true");
   c = false;
} 

This while loop would check if c is true.  Print out "c is true" and then set c to false.  So it should only print out "c is true" once.

The while loop is a good way to keep the computer processing something.  It is used in games to keep redrawing the screen.  Right now the for loop is probably the better loop for a beginner but it is good to have the knowledge of a while loop.

If you are confused just drop a comment!

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.