created 01/24/99; revised 05/21/03


Chapter 20 Programming Exercises

Exercise 1

Improve the combination lock program as suggested in the last page of chapter 20:

The program simulates a combination lock. The combination consists of three integers stored in variables. The user must enter the three integers in the correct order. However, each entered integer can be within plus or minus three of the correct number.

For example, if the combination is 10-20-30, the following will work:

C:\>java ComboLock

Enter first number:
13
Enter second number:
17
Enter third number:
31
Lock opens

The if statements for this program use more complicated boolean expressions than in the program of chapter 20. Review chapter 12 if this has faded from you memory.

Click here to go back to the main menu.

Exercise 2

In the program of chapter 20 (and possibly in your implementation of Exercise 1) each number is tested as soon as it has been entered. Write the program in a different way: The program asks the user for the three numbers and reads them in, but does not do any testing until all three have been read in. Then there is a single if statement that tests if the correct combination has been entered.

Write this program first so that the user must enter each number exactly correct. Then write it so that each entered number need only be within plus or minus 3 of the exact number. The if statement in the second version will be very complicated.

Click here to go back to the main menu.

Exercise 3

Write a program that implements a guessing game:

The program picks a random number from 1 to 10. Now the user gets three guesses. As soon as the user enters the correct number the program writes a winning message and exits. If the user fails to enter the correct number in three guesses, the program writes a failure message and exits.

You will need Math.random(), which produces a random double between 0.0 and 1.0. For example:

double someValue =  Math.random() ;   // someValue is between 0.0 and 1.0

Use some other methods of the Math class along with some arithmetic to convert the double into a random integer in the desired range. Look in your Java documentation for further details. Here are two example runs of the program:

C:\>java GuessingGame

I am thinking of a number from 1 to 10.
You must guess what it is in three tries.
Enter a guess:
4
wrong
8
RIGHT!
You have won the game.

C:\>java GuessingGame
 
I am thinking of a number from 1 to 10.
You must guess what it is in three tries.
Enter a guess:
1
wrong
5
wrong
9
wrong
The correct number was 7.
You have lost the game.
Click here to go back to the main menu.

Exercise 4

Write a more complicated guessing game, similar to the one in Exercise 3. Now the program is to write "cold" when the guess is 3 or more away from the correct answer, "warm" when the guess is 2 away, and "hot" when the guess is 1 away. For example:

C:\>java GuessingGame

I am thinking of a number from 1 to 10.
You must guess what it is in three tries.
Enter a guess:
1
cold
3
warm
5
RIGHT!
You have won the game.

C:\>java GuessingGame

I am thinking of a number from 1 to 10.
You must guess what it is in three tries.
Enter a guess:
4
hot
7
warm
5
RIGHT!
You have won the game.

C:\>java GuessingGame

Exercise 5

Write an even more complicated guessing game. In this version, the full game consists of 10 "rounds," where each round is a game like exercise 4. After the 10 rounds, the program prints out how many of the 10 rounds were won and how many were lost.

Here is an example run:

C:\>java GuessingGame

round 1:

I am thinking of a number from 1 to 10.
You must guess what it is in three tries.
Enter a guess:
4
hot
7
warm
5
RIGHT!
You have won 1 out of 1 rounds.

round 2:

I am thinking of a number from 1 to 10.
You must guess what it is in three tries.
Enter a guess:
10
cold
5
cold
3
wrong
The correct number was 1
You have won 1 out of 2 rounds.

. . .

I am thinking of a number from 1 to 10.
You must guess what it is in three tries.
Enter a guess:
1
cold
6
warm
7
RIGHT!
You have won 7 out of 10 rounds.

Your rating: amateur.

C:\>java GuessingGame
Click here to go back to the main menu.