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

Write a GUI program whose main class is named GuessThatNumber . The GUI should b

ID: 3553256 • Letter: W

Question

Write a GUI program whose main class is named GuessThatNumber. The GUI should be written by hand (i.e., not generated by an IDE). The program's purpose is to guess a number between 1 and 10, and then the user is to try to guess that number:



The user types their guess into a text field and presses the enter key.

The computer puts the user's guess into the JLabel to the right of the text field (and then clears the text field). Note that the game's status field tells them to guess higher next time. If the number guessed is lower than the computer's number, then the user is told to guess higher (if the user has guessed too high, then the computer will tell them to guess lower).

The status field indicates success. The input text field is disabled since the game is not active at the moment. The Next button is enabled so that the user can start a new game.

When the user presses Next, a new game begins, and the input text field is enabled and the Next field disabled.

And the game continues.

If the user types something besides an integer in the text field, then the program will throw an exception - you need to make sure that your program handles that gracefully. You should catch any and all exceptions, and tell the user (in a JOptionPane message dialog box) that they should only enter integers between 1 and 10 in the input box.

Explanation / Answer

package pqr;

import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.Random;


// Main Class

public class GuessThatNumber extends JFrame {


// Declare class variables

private static final long serialVersionUID = 1L;

public static Object prompt1;

private JTextField userInput;

private JLabel comment = new JLabel(" ");

private JLabel comment2 = new JLabel(" ");

private int randomNumber;

private int counter = 0;


// Constructor

public GuessThatNumber() {

super("Guess That Number!Rick");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


// Content pane

setLayout(new FlowLayout());

Container c = getContentPane();


// Create components

JButton guessButton = new JButton("Try the number");

JButton newGameButton = new JButton("New Game");

JButton quitButton = new JButton("Quit");

JLabel prompt1 = new JLabel("I have a number between 1 and 10.");

JLabel prompt2 = new JLabel("Can you guess the number?");

JLabel prompt3 = new JLabel("Please enter your guess: ");

comment = new JLabel(" ");

comment2 = new JLabel(" ");

userInput = new JTextField(5);


// Adding components to the pane

c.add(prompt1);

c.add(prompt2);

c.add(prompt3);

c.add(userInput);

c.add(guessButton);

c.add(newGameButton);

c.add(quitButton);

c.add(comment);

c.add(comment2);


// Set the mnemonic

guessButton.setMnemonic('T');

newGameButton.setMnemonic('N');

quitButton.setMnemonic('Q');


// Format pane

setSize(300, 200);

setLocationRelativeTo(null);

setVisible(true);

setResizable(false);


initializeNumber();


// Create the button handlers

GuessButtonHandler ghandler = new GuessButtonHandler(); // instantiate

// new object

guessButton.addActionListener(ghandler); // add event listener


NewButtonHandler nhandler = new NewButtonHandler();

newGameButton.addActionListener(nhandler);


QuitButtonHandler qhandler = new QuitButtonHandler();

quitButton.addActionListener(qhandler);

} // End constructor


//

private void initializeNumber() {

randomNumber = new Random().nextInt(10) + 1;

}


// GuessButton inner class

private class GuessButtonHandler implements ActionListener {

public void actionPerformed(ActionEvent e) {


// Declare class variables

int getUserInput;

int diff;

int Difference = 0;


// Validate input and if statements for user input

try {

getUserInput = Integer.parseInt(userInput.getText().trim());

counter++;

if (getUserInput > 10)

{

JOptionPane.showMessageDialog(null, "Enter a valid integer between 0 to 10");

}


else { if(getUserInput == randomNumber) {

JOptionPane.showMessageDialog(null, "Correct! It took you "

+ counter + " guesses", "Random Number: "

+ randomNumber, JOptionPane.INFORMATION_MESSAGE);

initializeNumber();

return;

}

if (getUserInput > randomNumber) {

comment2

.setText("The guess was HIGH. Try a lower number.");

comment2.setForeground(Color.WHITE);

diff = getUserInput - randomNumber;

Difference = Math.abs(diff);

} else {

comment2

.setText("The guess was LOW. Try a higher number.");

comment2.setForeground(Color.WHITE);

diff = randomNumber - getUserInput;

Difference = Math.abs(diff);

}

}


if (Difference >= 5) {

// comment.setText("Your Cold. ");

comment.setForeground(Color.WHITE);

GuessThatNumber.this.setBackgroundColor(Color.BLUE);

}


if (Difference <= 5) {

//comment.setText("Your getting Warm");

comment.setForeground(Color.WHITE);

GuessThatNumber.this.setBackgroundColor(Color.RED);

}

} catch (NumberFormatException ex) {

JOptionPane.showMessageDialog(null, "Enter a valid integer between 0 to 10");

}

}

} // End GuessButtonHandler


// NewButton inner class

private class NewButtonHandler implements ActionListener {

public void actionPerformed(ActionEvent e) {

GuessThatNumber app = new GuessThatNumber();


}

} // End NewButtonHandler


// QuitButton inner class

private class QuitButtonHandler implements ActionListener {

public void actionPerformed(ActionEvent e) {

System.exit(0);

}

} // End QuitButtonHandler


// Setting background color

private void setBackgroundColor(Color RED) {

getContentPane().setBackground(RED);

}


// Main method

public static void main(String args[]) {

// instantiate GuessGame object

GuessThatNumber app = new GuessThatNumber();


}// End main method

}// End main class

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote