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
A Carnot cooler operates with COP = 11, whose ambient temperature is 300K. Determine the temperature at which the refrigerator a
SashulF [63]

Answer:

275 Kelvin

Explanation:

Coefficient of Performance=11

T_H=\text {Absolute Temperature of high temperature reservoir=300 K}

T_L=\text {Absolute Temperature of low temperature reservoir}

\text {Coefficient of performance for carnot cooler}\\=\frac {T_L}{T_H-T_L}\\\Rightarrow 11=\frac{T_L}{300-T_L}\\\Rightarrow 11(300-T_L)=T_L\\\Rightarrow 3300-11T_L=T_L\\\Rightarrow 3300=T_L+11T_L\\\Rightarrow 3300=12T_L\\\Rightarrow T_L=\frac {3300}{12}\\\Rightarrow T_L=275\ K\\\Therefore \text{Temperature at which the refrigerator absorbs heat=275 Kelvin}

8 0
3 years ago
If you were to plot the voltage versus the current for a given circuit, what would you expect the slope of the line to be? If no
Brut [27]

Answer:

Part 1: It would be a straight line, current will be directly proportional to the voltage.

Part 2: The current would taper off and will have negligible increase after the voltage  reaches a certain  value. Graph attached.

Explanation:

For the first part, voltage and current have a linear relationship as dictated by the Ohm's law.

V=I*R

where V is the voltage, I is the current, and R is the resistance. As the Voltage increase, current is bound to increase too, given that the resistance remains constant.

In the second part, resistance is not constant. As an element heats up, it consumes more current because the free sea of electrons inside are moving more rapidly, disrupting the flow of charge. So, as the voltage increase, the current does increase, but so does the resistance. Leaving less room for the current to increase. This rise in temperature is shown in the graph attached, as current tapers.

7 0
3 years ago
A 3 m aluminum pole is kept at a residential site for construction
Aliun [14]

Answer:

I don't know sorry

Explanation:

5 0
3 years ago
If anyone knows manufacturing plz help
sveta [45]

Answer:

I don't know ask my dad he would

Explanation:

but I can't ask him because he went to get milk and forgot to come back

8 0
3 years ago
What type of degree is needed to be an architect?
Rina8888 [55]
Architects must have a professional bachelor's or master's degree in architecture from a program that has been accredited by the National Architectural Accrediting Board, and a state license.
3 0
3 years ago
Read 2 more answers
Other questions:
  • (Practice work, not graded)
    11·1 answer
  • The design for a new cementless hip implant is to be studied using an instrumented implant and a fixed simulated femur.
    11·1 answer
  • Why do many sources of water need treatment
    10·1 answer
  • In part A you are asked to write the pseudocode for the program. In part B you are asked to write the syntax of the code for the
    11·1 answer
  • What is the difference between filler and electrode in Welding? Can a filler be an electrode? Can an electrode be a filler? Why?
    6·1 answer
  • A kernel-level thread wishes to acquire a mutex lock declared as global in the process. True or False: the function call used be
    6·1 answer
  • What structure was created to help prevent shipwrecks?
    9·1 answer
  • After adjusting your seat, your___ should be as closest possible to the back rest.
    14·1 answer
  • Hi all, could you solve this please?<br> What is the value of the resistance R
    14·1 answer
  • Determine the slopes and deflections at points B and C for the beam shown below by the moment-area method. E=constant=70Gpa I=50
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!