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

***ON NETBEANS*** Create a Guess a number game exercise (not from the textbook)

ID: 3693743 • Letter: #

Question

***ON NETBEANS***

Create a Guess a number game exercise (not from the textbook) as follows:

**At the beginning of the start() method the application generates and stores a random number between 1 and 1000

**Design a form with (1) a TextField in which the user enters a number that is his/her guess of what the random number is (in one way or another prompt the user to enter their guess of this number into the TextField); (2) a Button which the user clicks to evaluate the guess; and (3) a Label that displays if the user's guess was "Too low" or "Too high" or "Correct"

**Use one or more layout pane's of your own choosing to create the structure of the window that looks professional and appealing, so additionally pay attention to the usage of constants from the Pos class (one-half point deduction for "substandard" design layout)

**The EventHandler should evaluate the user's input each time the Button is clicked; the user can click over and over again until the "guess" is correct

**For full credit add a second Button which the user can click after guessing the number correctly that generates a new random number between 1 and 1000 so the game can be played again (one-half point deduction or a maximum of points if this element is missing)

**Also for full credit include all appropriate exception handling for user input for an int as well as a last "catch all" Exception (one-half point deduction or a maximum of points if this element is missing)

*(A full point deduction or a maximum grade of 9 points if both of these last two elements are missing)

Explanation / Answer

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( "Can you guess my number? Enter your Guess:" ); // 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);

                            

                                

                                

                        }

                            

                    

                    

                }

            }

Actual code of exection starts here.

import javax.swing.JFrame;

public class GuessgameTest{

    public static void main(String args[]) throws Exception{

        Test guessgame = new Test();

        guessgame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

        guessgame.setSize(550, 150);

        guessgame.setVisible(true);

        }

    }

Using java Script