1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
a_sh-v [17]
3 years ago
13

Write a GUI-based program that plays a guess-the-number game in which the roles of the computer and the user are the reverse of

what they are in the Case Study of this chapter. In this version of the game, the computer guesses a number between 1 and 100 and the user provides the responses. The window should display the computer’s guesses with a label. The user enters a hint in response, by selecting one of a set of command buttons labeled Too small, Too large, and Correct. When the game is over, you should disable these buttons and wait for the user to click New game, as before.
Engineering
1 answer:
AURORKA [14]3 years ago
8 0

Answer:

import javax.swing.*;

import java.awt.*;

import java.util.Random;

import java.awt.event.*;

public class Guess extends JFrame

{

   private static final long serialVersionUID = 1L;

   private JButton newGame;

   private JButton enter;

   private JButton exit;

   private JTextField guess;

   private JLabel initialTextLabel;

   private JLabel enterLabel;

   private JLabel userMessageLabel;

   private int randNum;

   private int userInput;

   private int maxtries = 0;

   public Guess()

   {

       super("Guessing Game");

       newGame = new JButton("New Game");

       exit = new JButton("Exit Game");

       enter = new JButton("Enter");

       guess = new JTextField(4);

       initialTextLabel = new JLabel("I'm thinking of a number between 1 and 100. Guess it!");

       enterLabel = new JLabel("Enter your guess.");

       userMessageLabel = new JLabel("");

       randNum = new Random().nextInt(100) + 1;

       setLayout(new FlowLayout());

       add(initialTextLabel);

       add(enterLabel);

       add(guess);

       add(newGame);

       add(enter);

       add(exit);

       add(userMessageLabel);

   

       setSize(500, 300);

       addWindowListener(new WindowAdapter()

       {

           public void windowClosing(WindowEvent e)

           {

               System.exit(0);

           }

       });

       newGameButtonHandler nghandler = new newGameButtonHandler();

       newGame.addActionListener(nghandler);

       ExitButtonHandler exithandler = new ExitButtonHandler();

       exit.addActionListener(exithandler);

       enterButtonHandler enterhandler = new enterButtonHandler();

       enter.addActionListener(enterhandler);

   }

   class newGameButtonHandler implements ActionListener

   {

       public void actionPerformed(ActionEvent e)

       {

           setBackground(Color.ORANGE);

           guess.setEnabled(true);

           guess.setText("");

           enter.setEnabled(true);

           maxtries = 0;

           userMessageLabel.setText("");

           randNum = new Random().nextInt(100) + 1;

       }

   }

   class ExitButtonHandler implements ActionListener

   {

       public void actionPerformed(ActionEvent e)

       {

           System.exit(0);

       }

   }

   class enterButtonHandler implements ActionListener

   {

       public void actionPerformed(ActionEvent e)

       {

           userInput = Integer.parseInt(guess.getText());

           checkGuess(randNum);

      if(userInput > 100 )

          {

                               userMessageLabel.setText("invalid entry");

          }

       }

   }

   public void checkGuess(int randomNumber)

   {

       maxtries++;

     if(maxtries==10){

           userMessageLabel.setText("You Lose!!");

           guess.setEnabled(false);

           enter.setEnabled(false);

         

       }else if (userInput == randomNumber)

           {

               userMessageLabel.setText("Correct !");

           }

       else if (userInput > randomNumber)

           {

               userMessageLabel.setText("Too high");

           }

       else if (userInput < randomNumber)

           {

               userMessageLabel.setText("Too Low");

           }

   }

   public static void main(String[] args)

   {

       Guess game = new Guess();

       game.setVisible(true);

   }

}

You might be interested in
When converting liquid level units to sensor output signal units, you should first convert the liquid level units to _____ units
dangina [55]
The answer is 2 because I just took the test!
7 0
2 years ago
Users of E85 see a drop in gas mileage because ethanol can't produce the same
liraira [26]

Answer:

A

Explanation:

Due to ethanol's lower energy content, FFVs operating on E85 get roughly 15% to 27% fewer miles per gallon than when operating on regular gasoline, depending on the ethanol content.

7 0
3 years ago
25 In differential aeration corrosion, rich oxygenated parts are
sleet_krkn [62]

Answer:what are you trying to say

Explanation:

6 0
3 years ago
Market research is a good place to start the design process and usually involves asking questions about consumers.
ElenaW [278]
what is your question?
4 0
3 years ago
Military glorification through mosaics, relief carvings and triumphant arches are often associated with?
emmasim [6.3K]

Military glorification through mosaics, relief carvings and triumphant arches are often associated with roman architectural monument and is the correct choice.

<h3>What is a Monument?</h3>

This can be defined as a structure which is usually large and is used to commemorate the history of an influential person. This helps to serve as an example and also a reminder as to why the performance of good deeds are important

The use of military glorification through mosaics, relief carvings and triumphant arches were also often used by the Romans in other to commemorate war victories and the succession of a new ruler which is known as the emperor.

Read more about Roman monuments here brainly.com/question/15786258

#SPJ1

8 0
2 years ago
Other questions:
  • . A storm sewer is carrying snow melt containing 1.200 g/L of sodium chloride into a small stream. The stream has a naturally oc
    8·1 answer
  • Sed is a multipurpose tool that combines the work of several filters. sed performs noninteractive operations on a data stream. s
    12·1 answer
  • A six-lane freeway (three lanes in each direction) currently operates at maximum LOS C conditions. The lanes are 11 ft wide, the
    5·1 answer
  • For a steel alloy it has been determined that a carburizing heat treatment of 7 hour duration will raise the carbon concentratio
    12·1 answer
  • Two parts are to be assembled in a way that if one part fails, the entire assembly fails. Each of the parts have undergone exten
    15·1 answer
  • Design a digital integrator using the impulse invariance method. Find and give a rough sketch of the amplitude response, and com
    15·1 answer
  • ________ is the most theoretical computing discipline, focusing mostly on finding new and better ways for computers to work
    9·1 answer
  • 4.7 If the maximum tensile force in any of the truss members must be limited to 22 kN, and the maximum compressive force must be
    9·1 answer
  • In the car industry, clay models are used to visualize and test new car designs.
    5·2 answers
  • Orbit is to _____ as altitude is to _____.
    13·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!