ITEC 120 LAB 18 Back to Lab index page

Guessing game, object oriented

Write a java program which allows the user to guess a number between 1 and some upper bound, input by the user. If they guess incorrectly, the program will tell them if they should guess higher or lower. Allow the user to guess until they get the number right. Report to the user the number of guesses it took them to get the number. Your program should then ask the user if they'd like to play again.

You'll have 2 files - a driver which will create and use one or more GuessingGame objects, and the other file defines the GuessingGame class.

A GuessingGame object will have 3 pieces of instance data:

The GuessingGame class will have these methods:

The driver will do all the i/o (intput/output), that is, prompt the user and read the user's input. The driver should prompt the user for the upper bound, which will be sent to the constructor. The driver will create a new GuessingGame each time the user wishes to play again.

NOTE : Use the Scanner method .next() to read the user's answer to "Want to play again?" to avoid a problem you will experience if you use .nextLine(). .next()returns a String.

So, a sample run of this game might look like this:

Program outputs: Let's play guessing game!
How many numbers would you like to choose from?
User inputs: 10
Program outputs: OK, guess a number between 1 and 10:
User inputs: 6
Program outputs: No, too high. Guess lower.
User inputs: 3
Program outputs: No, too low. Guess higher.
User inputs: 5
Program outputs:

That's it! You got it right after 3 guesses.

Want to play again? (Y or N)

User inputs: Y
Program outputs: How many numbers would you like to choose from?
User inputs: 6
Program outputs: OK, guess a number between 1 and 6:
User inputs: 3
Program outputs: YAHOOO! You got it in ONE guess!!!

Want to play again? (Y or N)
User inputs: N
Program outputs: OK, bye. Thanks for playing number guesser.