This assignment will give you practice with interactive programs, if/else statements, methods that return values, String methods and while loops. You are going to write a program that allows the user to play a simple guessing game in which your program thinks up an integer (this has been done for you as a method) and allows the user to make guesses until the user gets it right. For each incorrect guess you will tell the user whether the right answer is higher or lower. Your program is required to exactly reproduce the format and behaviour of the log of execution at the end of this write-up, so you may want to look that over first. A template of the program (MITnnnnnGuess.java) has been generated for you and it includes method frameworks that you will have to correctly finish in order for the program to run.
- Your program should be stored in a file called MITnnnnnGuess.java where MITnnnnn is your MIT student ID.
- Define a class constant for the maximum number (MAX_GUESS) that the user can guess.
- Correctly implement the following static methods in addition to method main:
- A method that generates a random number between 1 and maximum number defined in 2 (this method has been generated for you)
- a method to give instructions to the user
- a method to play one game with the user
- a method to report overall results to the user
- Use a while loop to keep the program running until the user does not want to play anymore
- When you ask the user whether or not to play again, you should use the “next()” method of the Scanner class to read a one-word answer from the user. You should continue playing if this answer begins with the letter “y” or the letter “Y”. Notice that the user is allowed to type words like “yes”. You are to look just at the first letter of the user’s response and see whether it begins with a “y” or “n” (either capitalized or not) to determine whether to play again.
- At the end of the log you are to report various statistics about the series of games played by the user, these being the total number of games played (use a variable to keep track of this), the total number of guesses made (use another variable to keep track of this), and the average number of guesses per game.
This program allows you to play a guessing game. I will think of a number between 1 and 100 and will allow you to guess until you get it. For each guess, I will tell you whether the right answer is higher or lower than your guess. I'm thinking of a number... Your guess? 20 higher Your guess? 40 higher Your guess? 60 higher Your guess? 80 higher Your guess? 100 lower Your guess? 90 lower Your guess? 88 lower Your guess? 86 You got it right in 8 guesses Do you want to play again? Yes I'm thinking of a number... Your guess? 20 higher Your guess? 40 higher Your guess? 60 higher Your guess? 80 higher Your guess? 82 higher Your guess? 84 higher Your guess? 86 higher Your guess? 88 higher Your guess? 90 higher Your guess? 92 higher Your guess? 94 lower Your guess? 93 You got it right in 12 guesses Do you want to play again? YES I'm thinking of a number... Your guess? 20 higher Your guess? 40 higher Your guess? 60 lower Your guess? 58 lower Your guess? 56 You got it right in 5 guesses Do you want to play again? No Overall results: total games = 3 total guesses = 25 guesses/game = 8.333333333333334 max guesses = 12