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
Scientific research techniques are used to analyze the effectiveness of political advertising. False True
miv72 [106K]

Answer:

correct me if i'm wrong but i think it's false

Explanation:

5 0
3 years ago
You are driving on a roadway with multiple lanes of travel in the same direction, and are approaching an emergency vehicle parke
solmaris [256]

Answer: a. Leave the lane closest to the emergency as soon as it is safe to do so, or slow down to a speed of 20 MPH below the posted speed limit.

Explanation:

Giving a way to the law enforcement vehicle and a medical emergency vehicle is necessary. If one approaches an emergency vehicle parked along the roadway one should change the lane as the vehicle may not move and the driver may also waste his or her time also one should also slow down his or her speed while approaching the vehicle as most of the emergency vehicle are in rush to reach the hospital so the driver should maintain some distance with the medical emergency vehicle.

7 0
3 years ago
1. Two technicians are discussing tire rotation. Technician A says that you always follow the tire-rotation procedure outlined i
siniylev [52]

Answer:

don't know

Explanation:

huhuh

8 0
2 years ago
Electric current originates from which part of an atom? *
yanalaym [24]

Answer: Electric current originates from positively charged protons negatively charged electrons of an atom.

Explanation:

The movement of ions (positive or negative) from one point to another is called electric current.

An atom has three sub-atomic particles. These are protons, neutrons and electrons.

Protons are positively charged, neutrons have no charge and electrons are negatively charged. Protons and neutrons reside inside the nucleus of an atom whereas electrons revolve around the nucleus.

So, protons and electrons are responsible for originating electric current form an atom as these are the charged particles.

Thus, we can conclude that electric current originates from positively charged protons negatively charged electrons of an atom.

3 0
2 years ago
A ball bearing has been selected with the bore size specified in the catolog as 35.000 mm to 35.020 mm. Specify appropriate mini
Fofino [41]

Answer:

the minimum shaft diameter is 35.026 mm

the maximum shaft diameter is 35.042mm

Explanation:

Given data;

D-maximum = 35.020mm and d-minimum = 35.000mm

we have to go through Tables "Descriptions of preferred Fits using the Basic Hole System" so from the table, locational interference fits H7/p6

so From table, Selection of International Trade Grades metric series

the grade tolerance are;

ΔD = IT7(0.025 mm)

Δd = IT6(0.016 mm)

Also from Table "Fundamental Deviations for Shafts" metric series

Sf = 0.026

so  

D-maximum

Dmax = d + Sf + Δd

we substitute

Dmax = 35 + 0.026 + 0.016

Dmax = 35.042 mm

therefore the maximum diameter of shaft is 35.042mm

d-minimum

Dmin = d + Sf

Dmin = 35 + 0.026

Dmin = 35.026 mm

therefore the minimum diameter of shaft is 35.026 mm

8 0
2 years ago
Other questions:
  • All MOS devices are subject to damage from:________
    7·1 answer
  • Is a 68.75 and a 70 a pass in 5th grade?
    10·2 answers
  • The Emergency Stop Button icon on the Inputs toolbar can be used to press or release the Emergency Stop button on the CNC machin
    10·1 answer
  • You have designed a bone plate that is manufactured via rolling under cold working conditions, and tests show good biocompatibil
    14·1 answer
  • What type of drawing would civil engineers use if they needed to show an
    11·1 answer
  • Convert 25 mm into in.
    13·1 answer
  • PLEASE HELP!!! <br><br>I've included attachments. Can someone just check my answers pls??
    9·1 answer
  • potential difference is the work done in moving a unit positive charge from one point to another in an electric field. State Tru
    12·1 answer
  • A new approval process is being adapted by Ursa Major Solar. After an opportunity has been approved, the contract is sent to the
    9·1 answer
  • Where would the impossible amount of product 1 and 2 points intersect on the production possibility curve
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!