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
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
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
