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

JAVA Write a guessing game. Create a class called Secret. This class - has an in

ID: 3675224 • Letter: J

Question

JAVA

Write a guessing game. Create a class called Secret. This class
- has an instance variable, an int, that holds the solution to the game,
- and has another int instance variable for counting the number of guess the user has made.
- has one default constructor. The constructor assigns a random number between 10 and 20 to be the solution and initializes the guess counter to 0.
- has a mutator method
- int guess(int myTry) // that compares myTry with the solution.
   -If myTry is less than the solution, guess returns a positive int value (the specific value your method returns isn't important--only that it be positive).
   - If myTry is greater than the solution, guess returns a negative int value (the specific value that your method returns isn't important--only that it be negative).
- If myTry is equal to the solution, guess returns 0.
   - The guess method also updates the guess counter.
- has an accessor method
   - String toString() // that returns the solution and the guess counter.

Create a class GuessingGame with a main method. The main method creates a Secret object and interacts with the user, prompting for guesses and giving hints ("go higher" or "go lower") for incorrect guesses. When the correct solution is guessed, a "congratulations" message is printed, Secret's toString value is displayed, and the game exits.
Do not use any static data whatsoever. The only static method will be GuessingGame's main method. Remember to use appropriate data types, access modifiers, and follow Java's naming conventions.

Explanation / Answer


//Secret.java
public class Secret
{
  
   //private members of class
   private int solution;
   private int guessCounter;
  
   //Constructor of class that sets the random
   //value between 10 and 20
   public Secret()
   {
       solution = 10 + (int)(Math.random() * 20);
       //Set guess counter to zer0
       guessCounter=0;
   }
  
   /*The method guess that takes an integer vlaue and
   * increments the guess counter and returns 1
   * if the try is greater than solution
   * -1 if the try is less than solution
   * 0 if the try is equal to soltion*/
   int guess(int myTry)
   {
       guessCounter++;
      
       if(myTry>solution)
           return 1;
       if(myTry<solution)
           return -1;
       else
           return 0;
      
   }
  
   //Returns the string representation of solution
   //and guess counter
   @Override
   public String toString()
   {      
       return "solution :"+solution+" Guess Count :"+guessCounter;
   }  
}


------------------------------------------------------------------------------------------------------------------------------------------

/**The java program that prompts user to enter a number
* and keep displaying hints for the user to guess the number
* correctly. If user guess is correct, print congratulations
* otherwise keep running the program until user guess
* was correct*/
//GuessingGame.java
import java.util.Scanner;
public class GuessingGame
{
   public static void main(String[] args)
   {
      
       //Create an instance of Scanner class
       Scanner scanner=new Scanner(System.in);
       //set boolean repeat true
       boolean repeat=true;
       int myTry;
       //Create an instance of Secret class
       Secret secret=new Secret();
      
      
       System.out.println("Guessing Game");
      
       //Repeat the loop until user guess the correct value
       while(repeat)
       {
           System.out.println("Enter your guess ");
           //read string and parse to integer value
           myTry=Integer.parseInt(scanner.nextLine());
          
           //store the result calling guess method
           int result=secret.guess(myTry);
          
           //check if result is result is >0
           if(result>0)
               System.out.println("go lower");
           //check if result is result is <0
           else if(result<0)
               System.out.println("go higher");
           else
           {
               //set repeat to false
               repeat=false;
               //print message
               System.out.println("Congratulations!");
           }
       }
      
       //print the solution and number of guesses
       System.out.println(secret.toString());
      
   }
}

--------------------------------------------------------------------------------------------------------------------------------

Sample Output:

Guessing Game
Enter your guess
15
go lower
Enter your guess
14
go lower
Enter your guess
10
go higher
Enter your guess
11
go higher
Enter your guess
12
go higher
Enter your guess
13
Congratulations!
solution :13 Guess Count :6