logo CodeStepByStep logo

GuessingGame

Write a console program in a class named GuessingGame in which the program thinks of a random integer and the user tries to guess it. After each incorrect guess, tell the user whether the correct answer is higher or lower.

A series of guessing games is played. In each game, the computer chooses a random number between 1 and 100 inclusive. The game asks the user for guesses until the correct number is guessed. After each incorrect guess, the program gives a clue about whether the correct number is higher or lower than the guess. Once the user types the correct number, the game ends and the program reports how many guesses were needed.

After each game ends and the number of guesses is shown, the program asks the user if he/she would like to play again. Assume that the user will type a one-word string as the response to this question. A new game should begin if the user's response starts with a lower- or upper-case Y, such as "y", "Y", "yes", "YES", "Yes", or "yeehaw". Responses of "no", "No", "okay", "0", "certainly", and "hello" are all assumed to mean no.

Once the user chooses not to play again, the program prints overall statistics about all games. The total number of games, total guesses made in all games, average number of guesses per game (as a real number rounded to the nearest tenth), and best game (fewest guesses needed to solve any one game) are displayed.

This program allows you to guess random numbers.
I will think of a number between 1 and 100
and you will try to guess it.
After each guess, I will give you a clue about
whether the correct number is higher or lower.

I'm thinking of a number between 1 and 100...
Your guess? 50
It's lower.
Your guess? 25
It's higher.
Your guess? 35
It's lower.
Your guess? 30
It's higher.
Your guess? 32
It's lower.
Your guess? 31
You got it right in 6 guesses!
Do you want to play again? y

I'm thinking of a number between 1 and 100...
Your guess? 50
It's higher.
Your guess? 75
It's lower.
Your guess? 65
It's lower.
Your guess? 64
You got it right in 4 guesses!
Do you want to play again? YES

I'm thinking of a number between 1 and 100...
Your guess? 60
It's lower.
Your guess? 20
It's higher.
Your guess? 30
It's higher.
Your guess? 40
It's higher.
Your guess? 50
It's lower.
Your guess? 47
It's higher.
Your guess? 49
You got it right in 7 guesses!
Do you want to play again? no

Overall results:
Total games   = 3
Total guesses = 17
Guesses/game  = 5.7
Best game     = 4

You should handle the special case where the user guesses the correct number on the first try. Print a message as follows:

I'm thinking of a number between 1 and 100...
Your guess? 71
You got it right in 1 guess!

In order to match the expected outputs in our logs, use a Random object to produce random numbers. On each game, randomly choose the number from 0 to 99, then shift it upward by 1.

Assume valid user input. When prompted for numbers, the user will type integers only, and they will be in proper ranges.

You should break down your program into several methods that use parameters and returns to exchange data.

Define a class constant for the maximum number used in the games. The previous log shows games from 1 to 100, but you should be able to change the constant value to use other ranges such as from 1 to 50 or any maximum.

Complete program: Write an entire program that you could put into a file and run outside of CodeStepByStep.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.