[Android Software Development] tjdwowh:
Lesson #1-9: If and else statements

In Java, we can control the "flow" of the program by using conditional statements.

Let's take a look at this simple example.

[COLOR="Blue"]class DrinkingAgeTest {
[/COLOR]
[COLOR="SeaGreen"]//Class-wide integer age.
[/COLOR]static int age = 18;

[COLOR="RoyalBlue"]	public static void main(String[] args) {
[/COLOR]
[COLOR="Red"]if (age >= 21){
System.out.println("You may drink.");
}[/COLOR]

[COLOR="DarkOrange"]else{
System.out.println("You are too young to drink.");
System.out.println("Come back in " + (21 - age) + " years.");
}[/COLOR]

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

This is a pretty simple class. Let me explain some of the things we have here.

1. class DrinkingAgeTest creates a class called DrinkingAgeTest.

2. We then create a static integer age (this does not have to be static if it is placed inside the main method).

3. Of course we create a main method, as all Java programs will look for this as a starting point.

4. Now the interesting parts:

[COLOR="Red"]if (age >= 21){
System.out.println("You may drink.");
}[/COLOR]

[COLOR="DarkOrange"]else{
System.out.println("You are too young to drink.");
System.out.println("Come back in " + (21 - age) + " years.");
}[/COLOR]

Meet the if statement. It is written like so:

if (conditionOne){

}

//You can have as many if statements as you want.

if (conditionTwo){

}

//If none of the previous conditions are met, it carry out instructions in "else"

else{

}

When Java encounters the if statement, it checks each condition to see if it is satisfied (true).

If so, it carries out everything inside the braces for that if statement. If not, the statement is completely skipped.

Applying this to the example class, the compiler checks if age, which is 18, is greater than or equal to 21.

It is not, so it skips the line:

System.out.println("You may drink.");

Since none of the previous conditions are met, it automatically goes into the else statement, which outputs:

You are too young to drink.
Come back in 3 years.

Note: If you have multiple "if" statements, each of them is checked to see if true. If only one of the conditions can be true (i.e. the conditions you are testing for are mutually exclusive), it is therefore more efficient to use the "else if" statement like so:

if (conditionOne){

doThis();
}

else if(conditionTwo){

doThat();
}

else{
doOther();
}

Now we are going to apply this to simulate probability.

Lesson #1-10: Simulating Probability: Relational Operators

In the lesson #1-8, we created a random object called rand and invoked the method "nextInt."

rand.nextInt(11);

Recall that the above generates a number between 0 and 10 (11 numbers starting with zero).

Now let's simulate probability, starting with a simple example.

import java.util.Random;

[COLOR="SeaGreen"]//Let's simulate a coin toss![/COLOR]

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

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

[COLOR="SeaGreen"]//Create a Random object called rand.
[/COLOR]
Random rand = new Random();

[COLOR="Red"]	if (rand.nextInt(2) == 1){
System.out.println("heads");
}[/COLOR]
[COLOR="DarkOrange"]
else if (rand.nextInt(2) == 0){
System.out.println("tails");
}[/COLOR]

[COLOR="Orange"]	else if (rand.nextInt(2) == 2){
System.out.println("side. Fix your random number generator!");
}[/COLOR]

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

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

This is also a pretty straightforward class. We use the random number generator to create an number between 0 and 1 (inclusive) and set it equal to a newly created integer: coinInt.

We arbitrarily decide that the value of 1 is equal to heads and the value of 0 is equal to tails. Now we test the value of coinInt using else if statements (mutually exclusive events) and display the appropriate string (text).

Since the value of coinInt should never (theoretically) be 2, I created a third statement that is only read if coinInt does somehow become 2 (it should be as common as landing a coin on its side).

In these two lessons, we used two operators: == and >=.

Here are all six relational operators:

== equal to
!= not equal to
> greater than
>= greater than or equal to
< less than
<= less than or equal to

Lesson #1-11: Conditional Operators and Our First Game

In this lesson, we are going to create a simple game. Oh, don't get too excited. By simple, I mean simple.

Before we do that, here's a quick lesson on conditional operators:

|| means or.
&& means and.

When you have an if statement like below:

if (conditionOne == true || conditionTwo == true){
doThis();
}

Then if either conditionOne or conditionTwo is true, it will invoke the "doThis()" method;

When you have a statement like this:

if (conditionOne && conditionTwo){
doThat();
}

Then both conditionOne and conditionTwo must be satisfied before "doThat" method is called.

NOTE: When you just write a boolean (variables with value of either true or false, such as conditionOne) without stating: == true or == false, it is assumed that you actually wrote boolean = true.

In other words:

if (condition == true){
doThis();
}

and

if (condition){
doThis();
}

Are equivalent.

Now back to our game!

Here is how the game works. You roll a 6-sided die. If you land a 1 or 2 you lose. If you get a 3, 4, or 5, you roll again. If you get a 6, you win.

To write good programs, you have to plan beforehand. You should think the following:

1. I need a random number generator.

2. I should test if:

I. a number is less than or equal to 2.

II. if not, I should test whether if that number is between 3 and 5 (inclusive)

III. if not, I should test whether if that number is 6.

3. I should carry out an appropriate response.

Simple. Now let's create this class.

[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]
}

I want you to spend time with this class. Copy and paste it into eclipse and play around with it.

We will discuss this class in detail in tomorrow's lesson!

You might be surprised what you have already learned! :D

A tip: if you don't know what the purpose of a certain line of code is, it is a good idea to remove it temporarily (comment it out and run it). You can compare results with and without the statement =)

If you have any questions:

Pm me or email me!