Question: Problem: The GuessNumber class The output should look something like this: $java GuessNumber $java GuessNumber $java GuessNumber $java GuessNumber Target: GuessNumber will create a secret
Problem: The GuessNumber class
The output should look something like this:
$java GuessNumber

$java GuessNumber

$java GuessNumber

$java GuessNumber

Target:
GuessNumber will create a secret number from 1 to 20, and then ask the user to guess that number by entering a number between 1 to 20.
Specifications
In addition to the main method, you must create two methods:
guess()
getInt()
main Method
a) The main method should create a Scanner object.
b) The main method should then call the guess method, passing it the Scanner object as an argument.
c) The guess method will return the number of tries it took the user to guess the secret number; it will return -1 if the user cannot successfully guess the number within a LIMIT number of times.
d) Based upon the return result from the guess method, the main method should prompt a corresponding message: the number of trials if successful, or Game Over if unsuccessful.
guess Method
a) The guess method must have the following header public static int guess(Scanner input)
b) The guess method should create a Random object which it will use to create a secret number. This secret number must be an integer between 1 and 20.
c) The guess method needs to keep asking the user for a guess and keep track of the number of guesses in a variable.
d) The guess method does not ask the user directly for input. Instead, it calls the getInt method to do this by passing it two arguments:
The Scanner object it got from the main method.
A String which getInt will use to prompt the user for a number
e) The guess method compares the number entered by the user to its secret number.
If the number entered by the user is greater than the secret number, prompt
a message saying that it is greater than the target secret number, and call the
getInt method again to get a new input.
If the number entered by the user is smaller than the secret number, prompt a
message saying that it is smaller than the target secret number, and call the getInt method again to get a new input.
Each time guess method calls the getInt method, it should increment a counter that keeps track of the number of trials.
f) If the number entered by the user is equal to the secret number, it means the guess is successful. If the number of trials is less than
LIMIT (which is a class constant that determines how many trials a user can have at most), the guess method will return the number of trials to the main method.
g) If the user cannot successfully guess the secret number within LIMIT times, the guess method will return a value as -1 to the main method, indicating that the guess is unsuccessful.
getInt Method
a) getInt should have the following method header public static int getInt(Scanner input, String prompt)
getInt must:
b) Prompt the user a message using the string received from the method call from guess as a prompt.
c) Check that what the user entered can be turned into an integer
d) If the number cannot be made into an integer, print an error message, and prompt again for a new input
e) If the number can be turned into an integer, return that integer to the method call.
Please enter an integer from 1 to 20 10 Your input is smaller than the secret number Please enter an integer from 1 to 20 18 Your input is greater than the secret number. Please enter an integer from 1 to 20 14 Your input is greater than the secret number. Please enter an integer from 1 to 20 12 Great! It took you 4 tries to guess the number
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
