Using if statements and comparison operators

In this exercise you will pratice using if statements, which tie together with comparison operators.

A Dice Roll Program

A 4-sided die roll

Create a new java source file called DiceRoll.java. Don't forget the preamble:

//Name: TODO
//References: TODO

If you forgot how to create a blank Java program, refer back to the programs you have already written or a previous lab.

First we will outline our plan for the program by writing some comments inside the "main" part where we will later fill in some programming code (don't forget the indenting):

		//Get a random number
		//Use the random number to decide the outcome of a die roll
		//Get a second random number
		//Decide the outcome of a second die roll
		//Print out which roll was higher

Take a look at the following documentation.

Class Math

static doublerandom()
          Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.

Do you remember how to use a static method by looking at the Javadocs? To use this one you would write this:

		//Get a random number
		double myNumber;
		myNumber = Math.random();

You might imagine that the phrase Math.random() momentarily "becomes" a random number when that line is executed.

We want to simulate a 4-sided die, with a 25% probability of each side coming up. Our random number is a decimal number with a range from 0 to 1, so one way to relate this to a die roll is to use if statements. Add the following variable declaration and if statement after the comment talking about the outcome of the die roll.

		//Use the random number to decide the outcome of a die roll
		int roll;
		if (myNumber < .25) {
			roll = 1;
		}

When Java gets to this if statement in the course of running your program, it decides whether the condition (myNumber < .25) is true or false. If it is true, it runs any statements contained in the following code block (between the { and the }). If it is false, then it skips over it. Here is a rundown of the ways you can compare numbers in Java (note that we use two equal signs to test for equality, whereas one equal sign is used to change the value of a variable):

comparison operator meaning
< less than
> greater than
<= less than or equal to
>= greater than or equal to
== (exactly) equal to
!= not equal to

You might guess that we could use several if statements to determine the outcome of the die roll. Although that can be made to work, a simpler way of solving this same problem is to use a single, multi-part if statement instead of four separate if statements. Add some code so you get the following:

		//Use the random number to decide the outcome of a die roll
		int roll;
		if (myNumber < .25) {
			roll = 1;
		}
		else if (myNumber < .5) {
			roll = 2;
		}
		else if (myNumber < .75) {
			roll = 3;
		}
		else {
			roll = 4;
		}
		ConsoleIO.printLine("The first roll was " + roll);

Be sure you understand how this works: the 4 sections (if,    else if,    else if,    else) all comprise a single multi-part if statement. If the condition (myNumber < .25) is true, then it assigns 1 to the variable roll, and skips all the way down to the ConsoleIO.printLine statement. If it was false, then Java tries the first else if condition, (myNumber < .5). If that turns out to be true, it executes the following block, setting roll to 2, and then skips down to the ConsoleIO.printLine. If it was false, then it tries the next else if condition. If all 3 conditions turn out to be false, then the else block is executed. This can only happen if myNumber is from .75 to 1.0, leaving a 25% chance of rolling a 4.

In short, exactly one of the 4 code blocks above will be executed, depending on the value of myNumber. However, the printLine statement comes after the whole compound if statement, so it is always executed afterwords. Compile your code, and then run the program a few times. Make sure it all makes sense to you.

Now add in some code that makes a second die roll occur (be sure to use another call to random() to get a new random number, because if you reuse the old random number then you will get the same result again)! Store the result of the second die roll in an integer variable called secondRoll, using the same ideas. Print out the result. Compile and run your code to make sure it works correctly.

Now print out which die roll was higher (or say that they were equal), using another multi-part if statement. This would go below your comment that says //Print out which roll was higher. Note that you can put any code you want inside the body of an if statement (including a ConsoleIO.printLine). Your program should now look something like this when you run it:

The first roll was: 2
The second roll was: 2
Both rolls were the same

Or maybe like this, depending on how the rolls turn out:

The first roll was: 4
The second roll was: 3
The first roll was higher

Run it a few times to make sure it's getting results that make sense. When you are done, have the instructor check your work.



Bonus challenge: If you are looking for an extra challenge, see if you print out the maximum of the two rolls using the following documentation.

Class Math

static intmax(int a, int b)
          Returns the greater of two int values.

The int a and int b that appear in between the parentheses indicate that we are to supply two int values as input, separated by a comma.