CSCI-1320 Homework 10: Guessing Game

Instructor: Todd Dole

Purpose Statement

Did someone say "loops"? This assignment requires several loops. The instructions are fairly explicit regarding the behavior the program should exhibit. That will drive how you construct your loops. There are places where the do-while form of the while loop would be a bit more elegant than the traditional while.

Instructions

Construct a program that will play a guessing game with the user.

You can call your class GuessingGame, and save it as GuessingGame.java

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.

Please submit your GuessingGame.java file through Canvas.

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.