1) Develop a Java application that plays a \"guess the number\" game as describe
ID: 3696533 • Letter: 1
Question
1) Develop a Java application that plays a "guess the number" game as described below. a) The user interface is displayed and the user clicks the “Start Game” button to begin the game. b) Your application then gets a random number in the range 1-1000 inclusive (you might want to use Math.random or the Random class). c) The application then displays the following prompt (probably via a JLabel): I have a number between 1 and 1000 can you guess my number? Please enter a number for your first guess and then hit Enter. Post a textbox for the user to enter a number and post a message telling the user to hit 'Enter' after entering a guess in a textbox (probably using a JTextField). d) Input the user's guess in the code for a previously-registered event-handler method (consider using the event-handling approach discussed in the text, or the actionPerformed method of class based on the ActionListener interface, which will require some additional research outside the text). e) For the first guess color the entire background red, meaning that they are getting warmer (you might want to use the setBackground method for a container). If this is the second or later guess, and they are further from the correct number than the last guess, then color the entire background blue. If they get the correct number then color the background some other color than red or blue. f) If the user guessed the number correctly, respond with their number, post a congratulatory message, get a new random number, and display a JButton to start a new game. Otherwise, to help the user close in on the correct number, post a message, with their guessed number, whether they are "TOO HIGH" or "TOO LOW" from the correct number, and whether they are "WARMER" or "COLDER" (this should match the background color). Also report the guess number of the next guess (e.g. "Enter guess number nnn"). You might want to use a concatenated string in JLabel for these incorrect guess messages. g) The process is repeated each game until the user guesses the correct number. Be sure that you erase obsolete status messages. import java.awt.Color; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.util.Random; import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.JLabel; import javax.swing.JButton; import javax.swing.SwingUtilities; public class Test extends JFrame { private int GuessOld = 0; private int number; // application's number private JTextField guessInputJTextField; // user input field private JLabel prompt1JLabel; // first line of instruction private JLabel prompt2JLabel; // secon line of instructions private JLabel messageJLabel; // displays message of game status private JButton newGameJButton; // creates new game private Color background; // background color of application // set up GUI and initialize values public Test() { super( "Guessing Game" ); setLayout(new FlowLayout()); background = Color.LIGHT_GRAY; // set background to light gray prompt1JLabel = new JLabel( "I have a number between 1 and 1000." ); // describe game add(prompt1JLabel); prompt2JLabel = new JLabel( "Please enter a number for your first guess and then hit Enter." ); // prompt user add(prompt2JLabel); guessInputJTextField = new JTextField( 5 ); // to enter guesses guessInputJTextField.addActionListener( new GuessHandler( ) ); add(guessInputJTextField); messageJLabel = new JLabel( "" ); add(messageJLabel); newGameJButton = new JButton( "New Game" ); // create "New Game" button add ( newGameJButton ); // add newGame button to JFrame Random generator = new Random(); int number = generator.nextInt(1001);//create random number newGameJButton.addActionListener( new ActionListener() // anonymous inner class { public void actionPerformed( ActionEvent e ) { guessInputJTextField.setText(""); Random generator = new Random(); messageJLabel.setText(""); guessInputJTextField.setEditable(true); } // end method actionPerformed } // end anonymous inner class ); // end call to addActionListener theGame(); // start new game } // end GuessGameFrame constructor // choose a new random number public void theGame() { } // end method theGame // change background color public void paint( Graphics g ) { super.paint( g ); getContentPane().setBackground( background ); // set background } // end method paint class GuessHandler implements ActionListener{ public void actionPerformed( ActionEvent e ) { int Guess; Guess = Integer.parseInt(guessInputJTextField.getText()); if ( Math.abs( number - Guess ) < Math.abs( number - GuessOld) ){ // Hotter getContentPane().setBackground(Color.RED); } else{ // Colder getContentPane().setBackground(Color.BLUE); } GuessOld = Guess; if ( Guess >= number ) { messageJLabel.setText( "Too High." ); SwingUtilities.updateComponentTreeUI(messageJLabel); } if( Guess <= number ) { messageJLabel.setText( "Too Low." ); SwingUtilities.updateComponentTreeUI(messageJLabel); } // end if if ( Guess < number + 1 && Guess > number-1 ) // guess is too low { messageJLabel.setText( "Correct!" ); SwingUtilities.updateComponentTreeUI(messageJLabel); guessInputJTextField.setEditable(false); } } } I keep getting an error message: guessingGameLukeStamper.java:141: error: reached end of file while parsing } ^ 1 error
Explanation / Answer
/** GuessGame.java **/
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
// Main Class
public class GuessGame 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 GuessGame() {
super("Guessing Game");
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 1000.");
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(1000) + 1;
}
// GuessButton inner class
private class GuessButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
// Declare class variables
int getUserInput;
int diff;
int Difference;
// Validate input and if statements for user input
try {
getUserInput = Integer.parseInt(userInput.getText().trim());
counter++;
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 too HIGH. Try a lower number.");
comment2.setForeground(Color.WHITE);
diff = getUserInput - randomNumber;
Difference = Math.abs(diff);
} else {
comment2.setText("The guess was too LOW. Try a higher number.");
comment2.setForeground(Color.WHITE);
diff = randomNumber - getUserInput;
Difference = Math.abs(diff);
}
if (Difference >= 30) {
comment.setText("You are Cold. ");
comment.setForeground(Color.WHITE);
GuessGame.this.setBackgroundColor(Color.BLUE);
}
if (Difference <= 15) {
comment.setText("You are getting Warm");
comment.setForeground(Color.WHITE);
GuessGame.this.setBackgroundColor(Color.RED);
}
} catch (NumberFormatException ex) {
comment.setText("Enter a VALID number!");
}
}
} // End GuessButtonHandler
// NewButton inner class
private class NewButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
GuessGame app = new GuessGame();
}
} // 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
GuessGame app = new GuessGame();
}// End main method
}// End main class
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.