For this assignment you are limited to the language features in chapters 1 through 5 of the textbook.
In this program, the
program/computer "thinks of" an integer and the user guesses what the
number is until (s)he gets it right. For each incorrect guess, tell the
user whether the answer is higher or lower. Your program is required to
exactly
reproduce the sample output below.
Your main method must
contain a while loop that plays multiple games and prompts the user for
whether or not to play another game.
Your program is required
to have the following static methods in addition to the main method:
1. a method to give instructions to the user
2. a method that plays one game with the user (only one!)
3. a method that reports the overall results to the user
You may define more
methods than the ones described above if you find it helpful.
Define a class constant
that represents the maximum number used in the guessing game. For
example, if this constant is set to 100, the user is guessing a number
from 1 to 100. In your program, set the constant's value to 100 before
you submit your program, though your program should work if we change
this constant to other positive integers.
When you ask the user
whether or not to play again, read the response with the next() method
from the Scanner class (i.e., expect a one word answer). Continue
playing if the user's answer begins with 'y' or 'Y', i.e., the user can
type "yes", "yep", "yeah", "y", "Y", etc., to continue playing. Only
look at the first letter of the user's response to determine whether to
play again.
Assume that the user
always types an integer when guessing, and that the integer is always
in an appropriate range.
At the end of play, your
program will report various statistics about the games played by the
user. Report the total number of games played, the total number of
guesses made (all games included), the average number of guesses per
game, and the maximum number of guesses made in any single game.
Some hints and details:
1. This program must generate psuedo-random numbers. You must do this
using an object of type Random.
2. You will need to use
String class methods from chapters 3 and 4 to handle the yes/no
response from the user. Use the next() method of the Scanner class to
read a word from the console.
3. You won't be able to
reproduce the sample output below, since the program uses psuedo-random
numbers. You must reproduce the format of the sample output, and the
calculations for overall statistics must be correct.
4. Check your program
with different values of the class constant to make sure it's working
correctly. E.g., test your game with the class constant set to 5.
As usual, you are
required to use good programming style, including useful comments,
appropriate variable and method names, appropriate choices about
variable types, which if/else constructs to use, what parameters to
pass, etc.
Sample Run:
This is not an easy program, mostly due to the size of the program. The individual steps are not too difficult, but there are quite a few steps!! The program description above gives you a rough idea of how to break the program up into parts.
Approach the problem in
steps as we have done in class. Have a high level structure and then
implement parts of that structure (the individual methods) one at a
time, testing to make sure they work before going on. You may have to
write some testing code that will not be part of the final program.
Write psuedocode before you start writing Java code!
When finished turn in your Guess.java program using the
turnin program.