Question
Here is the question: Create a class called GuessTheNumber that generates a random magic number between 1 and 10 inclusive. It will then ask the
Here is the 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 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.
Here are two code samples, I don't know where to adjust.
GuessTheNumber
import java.security.SecureRandom; import java.util.Scanner;
public class GuessTheNumber { private Scanner input = new Scanner(System.in); private SecureRandom randomNumbers = new SecureRandom(); private int numberOfGuesses; public void play() { int magicNumber = 1 + randomNumbers.nextInt(10); numberOfGuesses = 0; int guess = askForGuess(); // is gthe guess equal to magicNumber or is it too high // or is it too low // maybe use a while loop } private int askForGuess() { int guess = 0; // asking for a guess and making sure it's 1 to 10 // maybe use a do...while loop numberOfGuesses++; return guess; } }
GuessTheNumberTest
import java.util.Scanner;
public class GuessTheNumberTest { public static void main(String [] args) { Scanner input = new Scanner(System.in); GuessTheNumber game = new GuessTheNumber(); game.play(); // ask to play again, maybe use a do...while loop } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started