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!