Homework #10 (Due: Fri 13 Feb)
Construct a program that will play a guessing game with the user. The
computer will initiate play by "thinking" of a number between 1 and 100 and
then asking the user to guess it. The computer will respond by stating
whether the guess was too low, too high, or correct. The user will be
required to continue guessing until they have identified the number.
Having the user guess involves generating a ``random'' number. The
following mathematical statement can be used to store such a value in the
integer variable n:
n= (int) (Math.random()*100.0)+1;
Additional requirements of the program are:
- Validate the user's guess to require it to be in the range 1 to
100. If they enter a value outside that range, display an error
message and have them re-enter the value.
- After the user has guessed the secret number, congratulate them and
then ask them if they want to play again. They should indicate this
by entering a 'y' or an 'n'.
You should validate their response by giving an error message and
having them re-enter the value if they enter anything other than one of
those two responses.
- If the user wants to play again, the computer should select another
secret number and allow the user to continue guessing.
You should begin the process of writing this program by constructing an
algorithm for the game itself. I recommend that you initially ignore the
requirements for validating input and for repeating the entire program.
Instead focus simply on playing the game. Once that is working you can
make enhancements. If you aren't sure where to start then follow these
- Construct an algorithm (using pseudocode or a flow chart) for
playing the game.
- Start writing the program. Don't type in the
entire game, though. Simply create a program that generates a random
number in the range 1 to 100 and then displays it. Compile and test
- Then add a prompt to have the user make a guess. Put that question
in a loop that will stop only when the user's guess matches the
number. Compile and test the program.
- After the loop print a message that congratulates the
user for guessing the right number. Compile and test.
- Inside the loop, after the user has entered their
guess, check to see if their guess was too high or too low and give
them feedback appropriately. Compile and test.
- Save your program under another name because you now have a
working program that plays the game! All that's left is adding
loops to validate user input and to repeat the actions of the program.
if the user wants to.
- Add a loop that will allow the user to play the game again.
Compile and test.
- Replace every request for user input with an appropriate loop
that validates that input. Do this one question at a time, compiling
and testing after you complete each.
When turning in your program, you should bring a printout of your source
code to class and you should email a copy of your source code to the
homework account. If you print from the JB 202 lab computers, remember
that your printout will appear in JB 104. (You are welcome to print your
program anywhere you like, however.)
Your program will be graded according to the following criteria: