Question: Create a class called GuessTheNumber that generates a random magic number between 1 and 10 inclusive. It will then ask the user to guess the

Create a class called GuessTheNumber that generates a random magic number between 1 and 10 inclusive. It will then ask the user to guess the magic number, and display one of the following messages:

Your guess is too high

Your guess is too low

You guessed correctly in nn guesses!

If the user guesses incorrectly, indicate if the guess is too high or too low, and continue asking for a guess until the user guesses the magic number. The class should count the number of guesses it took the user, and display that value for nn.

The class should have at least the following two methods:

public void play() Which plays the game. The method should create the magic number the user is trying to guess. It should call the askForGuess method to obtain the users guess and then check if the user guessed the magic number. If the user didnt guess the magic number this method should display to the user if the guess was too high or too low, and then ask for a new guess. When the user finally guess the magic number, it should display to the user that they guessed the correct number and indicate how many guesses it took.

private int askForGuess() Which asks the user for their guess. This method should check that the users guess is between 1 and 10 inclusive, and if it is, return the guess. If the users guess is not within range, the method should indicate the users guess was out of range and continue asking for a guess.

Create a second class called GuessTheNumberTest that contains the main method. The method should create a GuessTheNumber object and call the play method. The class should play the game at least once, and then ask the user if they would like to play the game again. Keep playing the game until the user decides they want to stop.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!