CS 305j - Programming Project 6
Practice with: user input, parameters, while loops, and psuedo-random numbers


For this project you will write a program that allows the user to play a simple guessing game. Your program will be stored in the file Guess.java.

This is an individual assignment. All work on this project must be done by you alone, with help only from the instructor, TAs, and proctors for the class.

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 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? 80
higher
Your guess? 100
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? YEP

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.333333333334
         max guesses    =  12



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.