Week Three Assignment

A couple more additions to the UI (User Interface):
  1. In the form editor, make sure the Components tab is selected and select jButton. Click on the cell just below the text field. For Component Name set it to "doneButton", and for Text set it to: "Oh I give up!"
  2. Add another JLabel to the right of the text field. Call it "outputLabel" and empty out the Text so that it is blank to start with.

Now let's do some coding!

On the left side of Eclipse is the Package Explorer. Right Click on the HiLoGameClass (Just below HiLoGame.java) and select "Open With" -> "Form Editor". This will show you how the program will look.

What we need to do though is start editing the code. While we have been creating the UI, the java code has been generated for us by Jigloo.

From the Package Explorer reselect the HiLoGame object and Open With Java Editor. This is the code behind the program!

On the right side of the screen is the Outline of the program. Select the HiLoGame Object. Just inside the scope of the class are several private fields. Such as guessLabel, outputLabel, doneButton and guessText. Jigloo created these for us. Make a new line just below the last private field and type in "private int randomNumber = 0;"

Note that every command must end with a semi colon. What this line does is define a new variable that our program will use. "private" means that it is only accessible within our program. No other program can access this variable. int is a whole number. It is the type of variable we are working with. int stands for "integer". Our program needs to keep track of a random number between 1 and 100. So we need to define that variable in the program. That's what this line does. We first create the variable and assign it to zero.

A concept that you need to understand is what null means. null means nothing. There is a difference between null and zero. Null has no value. It really means nothing. Zero is a real value. If you don't have a bank account, then the total amount of money you have in the bank is "null". But if you have a bank account and you're broke, the total amount in your account is zero. Do you understand the difference? Let's talk if you don't.

So what we did is create the variable that will store this random number and initialize it to zero. ints are a primitive data type which means that it cannot be initialized to null. It has to have something in it, so let's just set it to zero.

Now let's generate a random number and put the value into this randomNumber variable:

  1. From the Outline on the right side of Eclipse. Click on the HiLoGame() Constructor. The icon for the HiLoGame constructor has a green C next to it. Inside the constructor you will see this code:

    super();
    initGUI();

    Under initGUI(); line enter "randomNumber = (int)(Math.random() * 100);"

  2. Under that put "System.out.println("Random number is: " + randomNumber);"
What this does is generate the random number between 1 and 100 and put the value into the randomNumber variable. Then we show what the value is (just to make sure it works properly). The System.out.println is a way for the programmer to see whatever they want while the program is running. Programs that are completed and tested shouldn't have these System.out.println statements in there.

Go ahead and run the program and you should see in your Eclipse console something like this:

Random number is: 57

Close your program and rerun it. You should see the console regenerated a new random number. Let's stop there for this week. That is alot of stuff to soak in. Please call me if you have questions so that you can better understand.

Homework

  1. After the program shows in the console "Random number is: ...", get the console to also show "I am cool!".
  2. Learn more about how Eclipse and how to navigate inside it by reading this.

Concepts discussed

  1. NULL
  2. int
  3. System.out.println

If you have questions please email me!