Picture
Lesson #1-12: Analysis of the BySimpleIMeanSimple class

Looking at yesterday's "game" in-depth.

Yesterday, we created our very first game. Now we will dissect the class behind it.

Here's the code: 
Picture
Picture
1. The first line is simple: import the Random class.
2. We then declare the name of the class: BySimpleIMeanSimple
3. Then a static integer called dieValue, which will hold the value of the rolled die is created 
4. Next, we create the main method, which just invokes a method called rollDie();

5. The rollDie() method is then created. It has the following parts.

I. Creation of the Random object called rand.
II. Assignment of a random number between 0 and 5 (+1 since numbers range from 1 to 6 on a die) to the dieValue integer we created earlier.
III. It uses System.out.println to display the value of the integer dieValue.
IV. It invokes the method testDieValue(), which is defined below the rollDie() method. This testDieValue() method requires an integer parameter, so we input the new dieValue, which contains the random number between 1 and 6.

6. Next we declare the testDieValue() method. This is where the Control Flow statements happen (we control the way that the program proceeds based on conditional statements).
It takes the value of integer dieValue and then carries out the appropriate if statement.

I. If the value is less than or equal to 2, the player loses, so it displays, "You lose."
II. If the value is exactly 6 (==), it displays "You Win! Congratulations!"
III. If the value is 3 or 4 or 5 (This could've been written in many ways, such as: if (dieValue >= 3 && dieValue <= 5)...), it rerolls, invoking again the rollDie() method, which creates a random number and assigns it as the value of dieValue and then uses this value to invoke the testDieValue();

Someone asked earlier, what is the purpose of the: System.out.println();
Well, it is there to create an empty line in the console (where the output is displayed). It helps organize the way the program "speaks" to you.

NOTE: Make sure you are keeping up with the braces. When you do actually programming, braces won't be color coded to show you where the boundaries of each class or method are!

Lesson #1-13: Switches in Java

Java, much like railroad tracks, incorporates switches to direct the path of execution.
We will look at switches using an example that we have used before: a coin toss.

NOTE: Make sure that the class name in the Package Explorer to the left side of the screen on Eclipse matches that of the one stated in the code. Otherwise, you will get an error. The reason for this is that when you press Play, Eclipse looks for the class that with the name of the file that you are executing, and it will not be able to locate it.

(i.e. You should create a class named CoinTossSwitch.java and copy this code): 
Picture
Let's examine the switch in detail:
Picture
As you can tell, the format of a switch is as follows:

switch (variable){

case 1: doThis();
break;

case 2: doThat();
break;

case 3: doOther();
break;

}

Inside the parentheses (), you write the variable name that you are testing.
If this variable is an integer, you write:

case 1, case 2, and so on.

If it is is a boolean, you would write:

case true, case false.

If it is a String, you would write whichever values that the String variable can take.
For example, if your String can contain any color value, you would use:

case "red", case "blue", and so on.

The purpose of the "break;" is to separate the cases. If you forget to put the break, the code will not stop executing after a case is satisfied. It will keep going. Break can also be used to end loops (which we will discuss in the coming lessons).

__________

Applying this to the example code above,

1. We are testing an integer called randomInt.
2. If randomInt is 0, the computer will output: "Heads!" and break the case.
3. If randomInt is 1, the computer will output: "Tails!" and break the case.

What would be a practical application of this code? Let's say that we have a game in which the player can select one of three characters.
Let's name them Mario, Luigi, and Yoshi.

We would then create a String variable that holds the value of the name, and then use a switch to carry out the appropriate response.
Note: The following will only work in Java 1.7. If you have 1.6 or below, just look through it to see what it is doing!  
Picture
Picture
We store the current character's value in the String variable currentCharacter and use a switch to change the character's attributes.


Tomorrow we will discuss looping. We are getting closer to developing a game, so hang tight and stay tuned!

If you want to thank me for the guide, you can press Thanks or donate here or download TUMBL +!

Thank you guys for reading and I'm here if you have any questions!

Comment below and Like us on Facebook. 
 


Comments

Reece
11/16/2012 4:11pm

really helpful stuff thank you!!!


Reply
Steve
12/10/2012 6:38pm

These tutorials are great. I love that you gave an example of how the code could be used. Very helpful, and I look forward to continuing on in these tutorials!

Reply
Dnnis
01/03/2013 6:20pm

Thanks Sir very Helpful Tutorial be keeping on it!!

Reply



Leave a Reply

    Author

    James Cho is the lead developer at Kilobolt Studios. He is a college student at Duke University and loves soccer, music, and sharing knowledge.