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 six-lane freeway (three lanes in each direction) with a 5.5% uphill grade 1.5 miles long has 10-ft lanes and obstructions 5 ft
sertanlavr [38]
The answer would be:
A. LOS B
4 0
3 years ago
Read 2 more answers
A boiler is designed to work at 14bar and evaporate 8 kg/s of water. The inlet water to the boiler has a temperature of 400C and
bearhunter [10]

Answer:

Explanation: 2 is thy answer

7 0
3 years ago
A piece of corroded steel plate was found in a submerged ocean vessel. It was estimated that the original area of the plate was
shepuryov [24]

Answer:

Time of submersion in years = 7.71 years

Explanation:

Area of plate (A)= 16in²

Mass corroded away = Weight Loss (W) = 3.2 kg = 3.2 x 106

Corrosion Penetration Rate (CPR) = 200mpy

Density of steel (D) = 7.9g/cm³

Constant = 534

The expression for the corrosion penetration rate is

Corrosion Penetration Rate = Constant x Total Weight Loss/Time taken for Weight Loss x Exposed Surface Area x Density of the Metal

Re- arrange the equation for time taken

T = k x W/ A x CPR x D

T = (534 x 3.2 x 106)/(16 x 7.9 x 200)

T = 67594.93 hours

Convert hours into years by

T = 67594.93 x (1year/365 days x 24 hours x 1 day)

T = 7.71 years

3 0
4 years ago
How many kg / day of NaOH must be added to neutralize a waste stream generated by an industry producing 90,800 kg / day of sulfu
hram777 [196]

Answer:

74.12kg/day

Explanation:

Equation of reaction for the neutralization reaction of sodium hydroxide and sulfuric acid: 2NaOH + H2SO4 yields Na2SO4 + 2H2O

Mass of sulfuric acid produced per day = 90,800kg

Percentage of sulfuric acid in wastewater = 0.1%

Mass of sulfuric acid that ends up in wastewater per day = 0.1/100 × 90,800 = 90.8kg

From the equation of reaction, 2 moles of NaOH (80kg of NaOH) is required to neutralize 1 mole of H2SO4 (98kg of H2SO4)

80kg of NaOH is required to neutralize 98kg of sulfuric acid

90.8kg of sulfuric acid would be neutralized by (90.8×80)/98kg of NaOH = 74.14kg/day of NaOH

7 0
3 years ago
Read 2 more answers
Measures the power output of the machine
koban [17]
Watts I believe is the answer
4 0
3 years ago
Other questions:
  • Explain why failure of this garden hose occurred near its end and why the tear occurred along its length. Use numerical values t
    15·1 answer
  • Tadpoles raised in water with atrazine levels of 0.1 ppb should produce a higher percentage of male frogs with gonadal abnormali
    13·1 answer
  • Policy makers in the U.S. government have long tried to write laws that encourage growth in per capita real GDP. These laws typi
    6·1 answer
  • A spherical seed of 1 cm diameter is buried at a depth of 1 cm inside soil (thermal conductivity of 1 Wm-1K-1) in a sufficiently
    14·1 answer
  • the oscillation of rod oa about o is defined by the relation θ= (3/pi)*(sin*pi*t), where theta and t are expressed in radians an
    8·1 answer
  • A particular motor rotates at 3000 revolutions per minute. What is its speed in rad/sec, and how many seconds does it takes to m
    5·1 answer
  • An eddy current separator is to separate aluminum product from an input streamshredded MSW. The feed rate to the separator is 2,
    7·1 answer
  • A tensile load of 6.9 kN is applied on a copper rod with diameter of 12 mm. (a) What is the maximum normal stress in the rod? (b
    12·1 answer
  • U 4. Find 2 bridges in the US and answer the following:
    15·1 answer
  • Please help ASAP!!
    13·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!