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

Design a Swing application that implements \"guess a number\" play as follows. B

ID: 3581864 • Letter: D

Question

Design a Swing application that implements "guess a number" play as follows. By clicking the button, your program generates a random integer in the range from 1 to 20 inclusive. The objective of the player is to figure out this number. The player types a guess into the text field and presses the Enter key. If the player's guess is incorrect, the program displays "too high" or "too low", respectively. If the players guess is correct, the application displays "Congratulations" and makes the input text field inactive for further input. This input field will be activated again upon clicking the button for generating a new number. The player gets 4 attempts to figure out the number. If the guess is incorrect after the 4th attempt, the message "You lost" should be displayed and input field inactivated. Make sure to verify that every user input is in the range of generated random numbers and display "Invalid input" message if it is not without Incrementing the attempts count.

Explanation / Answer

//Swing application to implement “Guess a number”

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class Guess extends JFrame

{

    JTextField t1, t2, t3, t4;

    JLabel j4;

    ButtonListener bl1;

    ButtonListener2 bl2;

    ButtonListener3 bl3;   

    //set random number in rand variable

    int rand=(int) (Math.random()*100);

    int count=0;

    public Guess()

    {

        //Get the container

        Container c = getContentPane();

        //Set absolute layout       

       c.setLayout(null);  

        //Set Background Color

        c.setBackground(Color.WHITE);

        //Creating label Guess an integer in a range 1-20

        JLabel j=new JLabel("Guess my number game");

        j.setForeground(Color.RED);

        j.setFont(new Font("tunga",Font.BOLD,24));

        j.setSize(270,20);

        j.setLocation(300,35);

        //Creating label Enter a number.....

        JLabel j1=new JLabel("Enter a number b/w 1-20");

        j1.setFont(new Font("tunga",Font.PLAIN,17));

        j1.setSize(270,20);

        j1.setLocation(300,60);

        //Creating TextField for input guess

        t1=new JTextField(10);

        t1.setSize(50,30);

        t1.setLocation(350,80);

        //Creating Label for Display message        

        j4=new JLabel("Try and guess my number");

        j4.setFont(new Font("tunga",Font.PLAIN,17));

        j4.setSize(270,20);

        j4.setLocation(290,130);

        //Creating guess text field

        t3=new JTextField(10);

        t3.setSize(40,20);

        t3.setLocation(160,10);

        //Creating guess Label

        JLabel j6=new JLabel("Guesses");

        j6.setFont(new Font("tunga",Font.PLAIN,17));

        j6.setSize(270,20);

        j6.setLocation(210,10);

        //creating button

        JButton b1=new JButton("Guess");

        b1.setSize(150,40);

        b1.setLocation(260,250);

        bl1=new ButtonListener();       

        b1.addActionListener(bl1);

       

        //Place the components in the pane

        c.add(j4);   

        c.add(j);   

        c.add(j1);

        c.add(t1);

        c.add(t3);

        c.add(b1);   

        c.add(j6);    

        //Changing TextFields to UnEditable

        t2.setEditable(false);

        t3.setEditable(false);   

        //Set the title of the window

        setTitle("GUESS THE NUMBER");       

        //Set the size of the window and display it

        setSize(550,350);

        setVisible(true);

        setDefaultCloseOperation(EXIT_ON_CLOSE);

    }

    private class ButtonListener implements ActionListener

    {

           public void actionPerformed(ActionEvent e)

        {

            int a = Integer.parseInt(t1.getText());

            count=count+1;

            if(a<rand)

            {

                j4.setText(a+" is too low, try again!!");

            }   

            else if(a>rand)

            {

                j4.setText(a+" is too high, try again!!");

            }

            else

            {

                j4.setText("CORRECT, YOU WIN!!!!");   

}

            //setting focus to input guess text field

            t1.requestFocus();

            t1.selectAll();

            t3.setText(count+"");

        }

    }

    private class ButtonListener2 implements ActionListener

    {

        public void actionPerformed(ActionEvent e)

        {

            t3.setText("");

            j4.setText(rand+" is the answer!");

            t1.setText("");

            t1.setEditable(false);

        }

    }       

  private class ButtonListener3 implements ActionListener

    {

        public void actionPerformed(ActionEvent e)

        {

            rand=(int) (Math.random()*100);

            t1.setText("");

            t3.setText("");

            j4.setText("Try and guess the number");

            t3.setText("");

            count=0;

            t1.setEditable(true);   

            t1.requestFocus();

        }

    }

    public static void main(String[] args)

    {

        new Guess();

    }

}