Write a program GuessGame.java that plays the game “guess the number” as follows: Your program chooses the number to be guessed by selecting an integer at random in the range 1–1000. The program then displays the following in a label: I have a number between 1 and 1000 can you guess my number? Please enter your first guess. A JTextField should be used to input the guess. As each guess is input, the background color should change to either red or blue. Red indicates that the user is getting “warmer,” and blue indicates that the user is getting “colder.” A JLabel should display either “Too High” or “Too Low” to help the user zero in on the correct answer. When the user gets the correct answer, “Correct!” should be displayed, and the JTextField used for input should be changed to be uneditable. A JButton should be provided to allow the user to play the game again. When the JButton is clicked, a new random number should be generated and the input JTextField changed to be editable

In order to create the GuessGame program as described, we can start by creating a class called GuessGame which extends JFrame. This class will serve as the main frame for the application.

Inside the GuessGame class, we will declare and initialize the necessary components such as the JLabel for displaying the message, the JTextField for inputting the guesses, and the JButton for playing the game again.

To select a random number between 1 and 1000, we can use the Random class from the java.util package. We will create an instance of Random and call the nextInt method with the parameter 1000. This will generate a random integer in the range of 0 to 999. To map this range to 1-1000, we will add 1 to the generated number.

To detect the user’s guesses and perform the necessary actions, we will add an ActionListener to the JTextField. When the user presses the “Enter” key or clicks outside the JTextField, the actionPerformed method defined in the ActionListener will be invoked. In this method, we will compare the user’s guess with the randomly generated number and provide the appropriate feedback by changing the background color and displaying a message.

If the guess is too high or too low, we will update the JLabel with the corresponding message and change the background color to blue. Additionally, we will keep track of the previous guess and compare it with the current guess to determine if the user is getting “warmer” or “colder”. If the current guess is closer to the randomly generated number than the previous guess, we will change the background color to red.

If the user’s guess is correct, we will update the JLabel with the message “Correct!” and disable the JTextField by calling its setEditable method with the argument false.

To enable the user to play the game again, we will add an ActionListener to the JButton. When the button is clicked, the actionPerformed method of the ActionListener will be called. In this method, we will generate a new random number and reset the text of the JTextField to an empty string. We will also enable the JTextField by calling its setEditable method with the argument true and update the JLabel with the initial message.

To summarize, the GuessGame program will consist of the GuessGame class which extends JFrame and contains the necessary components such as JLabel, JTextField, and JButton. We will use the Random class to generate a random number between 1 and 1000. We will add an ActionListener to the JTextField to detect the user’s guesses and provide feedback. We will also add an ActionListener to the JButton to reset the game and generate a new random number.

Need your ASSIGNMENT done? Use our paper writing service to score better and meet your deadline.


Click Here to Make an Order Click Here to Hire a Writer