Create a Java application with generated comments by creatin
Create a Java application (with generated comments) by creating a new class named “GuessGame”. Here is an example of how the game is played:
Your program should choose the number to be guessed by selecting an integer at random in the range 1 to 1000. The program then displays the following
I have a number between 1 and 1000.
Can you guess my number?
Please type your first guess.
The player then types a first guess. The program responds with one of the following
Excellent! You guessed the number!
Would you like to play again?
Too low – try again.
Too high – try again
If the player’s guess is incorrect, your program should loop until the player finally gets the number right.
Count the number of guesses the player makes. Display the count and each value guessed. If the number is 10 or fewer, display “Either you know the secret or you got lucky!” If the player guesses the number in 10 tries then print “Ahah! You know the secret!” If the player makes more than 10 guesses, then print “You should be able to do better!”
Solution
GuessGame.java
import java.util.Random;
 import java.util.Scanner;
public class GuessGame {
   public static void main(String[] args) {
        //Declaring variables
        int rand_number, guess_number,count=0;
       
        //Creating an integer array which stores the user guessed numbers
 int guessNos[]=new int[1000];
   
        // Scanner object is used to get the inputs entered by the user
        Scanner sc = new Scanner(System.in);
        //Creating the Random class object
        Random r = new Random();
       
        //generating the random number
        rand_number = r.nextInt(1000) + 1;
      
        System.out.println(\"I have a number between 1 and 1000.\");
        System.out.println(\"Can you guess my number?\");
        System.out.println(\"Please type your first guess.\");
      
        //This loop continues to execute until the user guessed the exact number
        while (true) {
            //Getting the user entered number
            guess_number = sc.nextInt();
           
            //populating the user entered number into an array
            guessNos[count]=guess_number;
           
            //Incrementing the count value
 count++;
   
 //Based on the value of the user guessed number display the message
            if (rand_number == guess_number) {
                System.out.println(\"Excellent! You guessed the number!\");
                break;
            } else if (guess_number < rand_number) {
                System.out.println(\"Too low – try again.\");
                continue;
            } else if (guess_number > rand_number) {
                System.out.println(\"Too high – try again.\");
                continue;
            }
        }
       
        //Displaying the value of the count
        System.out.println(\"No of Guesses you took to guess the number is : \"+count);
       
        //Displaying the user guessed values
        System.out.println(\"The Numbers you guessed are :\");
        for(int i=0;i<count;i++ )
        {
            System.out.println(guessNos[i]);
        }
       
       
        //Based on the count value displaying the message on the console
        if(count<10)
            System.out.println(\"Either you know the secret or you got lucky!\");
        else if(count==10)
            System.out.println(\"Ahah! You know the secret!\");
        else if(count>10)
            System.out.println(\"You should be able to do better!\");
}
}
________________________
Output:
I have a number between 1 and 1000.
 Can you guess my number?
 Please type your first guess.
 500
 Too low – try again.
 700
 Too low – try again.
 800
 Too low – try again.
 900
 Too high – try again.
 850
 Too high – try again.
 820
 Too low – try again.
 825
 Too low – try again.
 827
 Too low – try again.
 828
 Too low – try again.
 830
 Too low – try again.
 835
 Too low – try again.
 840
 Too low – try again.
 845
 Too high – try again.
 847
 Too high – try again.
 846
 Too high – try again.
 844
 Too high – try again.
 842
 Too high – try again.
 841
 Excellent! You guessed the number!
 No of Guesses you took to guess the number is : 18
 The Numbers you guessed are :
 500
 700
 800
 900
 850
 820
 825
 827
 828
 830
 835
 840
 845
 847
 846
 844
 842
 841
 You should be able to do better!
___________Thank You



