Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Program Specification: There is a \"fun\" children\'s game where one child think

ID: 3641169 • Letter: P

Question

Program Specification:
There is a "fun" children's game where one child thinks of a "secret" number between zero and two hundred and fifty five, then the second child repeatedly is asked to guess this "secret" number. Each guess is responded to with one of three responses:

Low - the guess is less than the "secret" number.
Correct - the guess is equal to the "secret" number.
High - the guess is greater than the "secret" number.

If the guessing child uses their information correctly, they will never need to make more than 10 guesses. This is however not any form of requirement - you the programmer can not make the user behave intelligently - it is merely an interesting side note

You are to write a Java program that:

Generates (and stores in a variable) a random "secret" number, in the range of [0, 1023], using the following line of Java code
int secret = (int)(Math.random() * 1024);

Repeats the following until the user guesses the "secret" number correctly:

Uses a user input validation loop to repeatedly prompt its user to enter their guess of the "secret" number and storing this value into a variable, until a valid guess is entered.

Determine the relative value of the guess compared to the "secret" number, and display the corresponding response to the user.

Display some kind of congratulations message, which includes the "secret" number and the number of guesses that it took the user to guess the "secret" number.
Sample run(s):

Please wait while the computer picks a number between 0 and 1023 ...

Please enter a guess [0, 1023] : 25
Too low!

Please enter a guess [0, 1023] : 50
Too low!

Please enter a guess [0, 1023] : 128
Too low!

Please enter a guess [0, 1023] : 180
Too low!

Please enter a guess [0, 1023] : 220
You got it!

You guessed the secret number 220 in 5 guesses
The worst you should have done was 10 guesses

Explanation / Answer

please rate - thanks

import java.util.*;
public class GuessingGame
{
public static void main(String [] args)
{Scanner in=new Scanner(System.in);
Random r = new Random();
int num;
int guess=0;
int guesses=0;
System.out.println("Please wait while the computer picks a number between 0 and 1023 ");
int secret = (int)(Math.random() * 1024);

System.out.print("Please enter a guess [0, 1023]: ");
guess=in.nextInt();
guesses++;
while(guess!=secret)
{guesses++;
if(guess>secret)
     System.out.println("Too high!");
else if(guess<secret)
     System.out.println("Too low!");
System.out.print("Please enter a guess [0, 1023]: ");
guess=in.nextInt();
   
}
System.out.println("You got it!");
System.out.println("You guessed the secret number "+secret+" in " + guesses + " guesses");
System.out.println("The worst you should have done was 10 guesses");
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote