[Android Software Development] tjdwowh:
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:

[COLOR="DarkOrchid"]import java.util.Random;[/COLOR]

[COLOR="SeaGreen"]//The greatest game ever made follows.
[/COLOR]
[COLOR="Blue"]class BySimpleIMeanSimple {
[/COLOR]
static int dieValue;

[COLOR="RoyalBlue"]	public static void main(String[] args) {
[/COLOR]
rollDie();

[COLOR="RoyalBlue"]	}
[/COLOR]
[COLOR="DeepSkyBlue"]	static void rollDie(){
[/COLOR]
Random rand = new Random();

[COLOR="SeaGreen"]	//Assign a random number between 1 and 6 as dieValue
[/COLOR]	dieValue = rand.nextInt(6) + 1;
System.out.println("You rolled a " + dieValue);
[COLOR="SeaGreen"]	//Why add 1? Think about it.
[/COLOR]

testDieValue(dieValue);

[COLOR="DeepSkyBlue"]	}
[/COLOR]
[COLOR="MediumTurquoise"]	static void testDieValue(int dieValue){
[/COLOR]	[COLOR="Red"]	if (dieValue <= 2){[/COLOR]
System.out.println("You Lose.");
[COLOR="Red"]}[/COLOR]

[COLOR="Orange"]else if (dieValue == 3 || dieValue == 4 || dieValue == 5){[/COLOR]
System.out.println();
System.out.println("Rerolling.");
System.out.println();
rollDie();
[COLOR="Orange"]}[/COLOR]

[COLOR="SandyBrown"]else if (dieValue == 6){[/COLOR]
System.out.println("You win! Congratulations!");
[COLOR="SandyBrown"]}[/COLOR]

[COLOR="MediumTurquoise"]	}
[/COLOR]
}

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

[COLOR="Purple"]import java.util.Random;[/COLOR]

[COLOR="Blue"]class CoinTossSwitch {
[/COLOR]

[COLOR="RoyalBlue"]public static void main(String[] args) {[/COLOR]

Random rand = new Random();
int randomInt = rand.nextInt(2);

System.out.println(randomInt);

[COLOR="Green"] switch (randomInt){[/COLOR]
case 1: System.out.println("Heads!");
break;
case 0: System.out.println("Tails!");
break;
[COLOR="Green"]	}[/COLOR]

[COLOR="RoyalBlue"]	}[/COLOR]

[COLOR="Blue"]}[/COLOR]

Let's examine the switch in detail:

[COLOR="Green"] switch (randomInt){[/COLOR]
case 1: System.out.println("Heads!");
break;
case 0: System.out.println("Tails!");
break;
[COLOR="Green"]	}[/COLOR]

__________

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.

__________

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!

[COLOR="Green"]//Sample Character Selection Screen
[/COLOR]
[COLOR="Blue"]class CharacterSelect {
[/COLOR]

[COLOR="RoyalBlue"]	public static void main(String[] args) {
[/COLOR]
String currentCharacter = "Mario";

int maxLife;
int maxJump;
int maxSpeed;

[COLOR="Red"]	switch (currentCharacter){
[/COLOR]
[COLOR="DarkOrange"]case "Mario":[/COLOR]
System.out.println("You have selected Mario!");
maxLife = 70;
maxJump = 50;
maxSpeed = 25;
[COLOR="DarkOrange"]break;[/COLOR]

[COLOR="Olive"]	case "Luigi":	[/COLOR]
System.out.println("You have selected Luigi!");
maxLife = 40;
maxJump = 70;
maxSpeed = 30;
[COLOR="Olive"]break;[/COLOR]

[COLOR="Lime"]case "Yoshi": [/COLOR]
System.out.println("You have selected Yoshi!");
maxLife = 50;
maxJump = 30;
maxSpeed = 40;
[COLOR="Lime"]break;	[/COLOR]

[COLOR="Red"]}[/COLOR]

[COLOR="RoyalBlue"]}[/COLOR]

[COLOR="Blue"]}[/COLOR]

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!

PM me or email me!