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

Create a class called GuessTheNumber that generates a random number between 1 and 10, inclusive. It will then ask the user to guess the number between 1 and 10, inclusive, 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 was too high or too low, and continue asking for a guess until the user guesses the 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: a public method named play which actually plays the game; and private method named askForNumber which asks the user for their guess. Make sure to perform error checking on the users input.

Create a second class called GuessTheNumberTest that plays the game. 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

For all both classes, comment all method, and at the start of any control statements.

GuessTheNumber.java

import java.security.SecureRandom;

import java.util.Scanner;

public class GuessTheNumber {

private Scanner input = new Scanner(System.in);

private SecureRandom randomNumber = new SecureRandom();

private int numberOfGuesses;

public void play() {

int theNumber = 1 + randomNumber.nextInt(10);

numberOfGuesses = 0;

int guess = askForNumber();

while(guess != theNumber) {

// deterime if guess is too low or too high

// tell the user

guess = askForNumber();

}

System.out.printf("You guessed correctly in %d guesses%n", numberOfGuesses);

}

private int askForNumber() {

int guess = 0;

// Prompt the user for a number between 1 and 10:

// Check that the user guess is between 1 and 10

// If not between 1 and 10, display error message

// and prompt the user again

numberOfGuesses++;

return guess;

}

}

GuessTheNumberTest.java

import java.util.Scanner;

public class GuessTheNumberTest {

static Scanner input = new Scanner(System.in);

public static void main(String args[]) {

int playAgain;

do {

GuessTheNumber game = new GuessTheNumber();

game.play();

System.out.print("Play again? 1-yes, 0-no");

playAgain = input.nextInt();

} while(playAgain == 1) ;

}

}

What is missing?

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!