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

Please for java programming: #4 Guess a Number There is a childhood game where o

ID: 3913391 • Letter: P

Question

Please for java programming: #4 Guess a Number There is a childhood game where one person selects a number and the rest try to guess it. The only hints they are given is whether their guess is higher or lower than the selected number. Write a program that randomly picks a number between 0 and 999 inclusive. Ask the user to guess the number and tell them if their guess is too high or too low. Keep track of how many guesses they make. It is possible to guess the number within ten tries, so give the following responses: · If they guess the number on the first try, accuse them of cheating. · If they guess the number within ten tries, tell them they did a great job. · If they take more than ten tries, tell them you could do better than that. Allow the user to play the game, with a new number to guess, as many times as they want. Hint: While testing your code, print out the selected number so you can control the number of “guesses.” Be sure to remove the print out before turning in your program. Hint: This is basically Exercise 4-3 from the book, but you don't have to start with the code the author has given you. You do, however, have to remove the breaks and continues if you do use his code as a base.

Explanation / Answer

import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;


public class GuessNumber {
// Guess limits
private static final int MIN_NUM = 0;
private static final int MAX_NUM = 999;
  
public static void main(String[] args) {
GuessNumber guess = new GuessNumber();
guess.guessStart();
}
  
/**
* Play the guess number.
*/
public void guessStart() {
boolean isGuessCorrect = false;
int number_Guesses = 0;
// computer thinks a number
int computerNumber = getNumberByComputer();
  
// Program continues till user guesses the number matched with the computer number
while(!isGuessCorrect) {
int userNumber = getUserGuessedNumber();
if(userNumber > computerNumber) {
System.out.println("Sorry, the number you guessed is too high");
}else if(userNumber < computerNumber) {
System.out.println("Sorry, the number you guessed is too low");
}else if(userNumber == computerNumber) {
System.out.println("Congratulations! Your guess is correct!");
isGuessCorrect = true;
}
number_Guesses++;
}
if(number_Guesses == 1)
System.out.println("You found the number in first guess, did you cheat ?");
else if (number_Guesses <= 10)
System.out.println("You found the number in less than 10 guesses, you did a great job ");
else if (number_Guesses > 10)
System.out.println("you could do better than that");
}
  
/**
* Returns a random number between 0 and 999 inclusive.
* @return
*/
public int getNumberByComputer() {
return ThreadLocalRandom.current().nextInt(MIN_NUM, MAX_NUM);
}
  
/**
* Returns the number guessed by user
* @return
*/
public int getUserGuessedNumber() {
Scanner sn = new Scanner(System.in);
System.out.println("Please guess the number: ");
return sn.nextInt();
}
}