Homework #10 (Due: Fri 13 Feb)

Did somebody say \lq\lq loops''? This assignment requires several loo...
...t the number in 6 or fewer guesses they win; otherwise, they

Problem Description

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:

  1. 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.
  2. 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.
  3. If the user wants to play again, the computer should select another secret number and allow the user to continue guessing.

Important Information

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 steps:
  1. Construct an algorithm (using pseudocode or a flow chart) for playing the game.
  2. 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 your program.
  3. 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.
  4. After the loop print a message that congratulates the user for guessing the right number. Compile and test.
  5. 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.
  6. 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.
  7. Add a loop that will allow the user to play the game again. Compile and test.
  8. 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.

Submission of Programs / Grading Criteria

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:

Correctness/Completeness 10 pts
Documentation 5 pts
Design/Structure 5 pts
Total 20 pts

Quick Links