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 belt drive was designed to transmit the power of P=7.5 kW with the velocity v=10m/s. The tensile load of the tight side is twi
Leviafan [203]

Answer:

F₁ = 1500 N

F₂ = 750 N

F_{e} = 500 N

Explanation:

Given :

Power transmission, P = 7.5 kW

                                      = 7.5 x 1000 W

                                      = 7500 W

Belt velocity, V = 10 m/s

F₁ = 2 F₂

Now we know from power transmission equation

P = ( F₁ - F₂ ) x V

7500 = ( F₁ - F₂ ) x 10

750 =  F₁ - F₂

750 = 2 F₂ - F₂      ( ∵F₁ = 2 F₂ )

∴F₂  = 750 N

Now F₁ = 2 F₂

        F₁ = 2 x F₂

        F₁ = 2 x 750

        F₁ = 1500 N   ,   this is the maximum force.

Therefore we know,

F_{max} = 3 x F_{e}

where F_{e} is centrifugal force

 F_{e} = F_{max} / 3

                          = 1500 / 3

                         = 500 N

8 0
3 years ago
I am trying to create a line of code to calculate distance between two points. (distance=[tex]\sqrt{ (x2-x1)^2+(y2-y1)^2}) My li
k0ka [10]

Answer:

point_dist = math.sqrt((math.pow(x2 - x1, 2) + math.pow(y2 - y1, 2))

Explanation:

The distance formula is the difference of the x coordinates squared, plus the difference of the y coordinates squared, all square rooted.  For the general case, it appears you simply need to change how you have written the code.

point_dist = math.sqrt((math.pow(x2 - x1, 2) + math.pow(y2 - y1, 2))

Note, by moving the 2 inside of the pow function, you have provided the second argument that it is requesting.

You were close with your initial attempt, you just had a parenthesis after x1 and y1 when you should not have.

Cheers.

6 0
3 years ago
IM JI Suneou uo mm
Oksi-84 [34.3K]

Answer: g

Explanation:

5 0
3 years ago
Payment to beneficiaries who were named by<br> the insured person
Zanzabum
Death benefit from a Life insurance policy
4 0
4 years ago
How is engine power expressed?
mars1129 [50]

Answer: Engine power is the power that an engine can put out. It can be expressed in power units, most commonly kilowatt, pferdestärke (metric horsepower), or horsepower.

Explanation: (I hope this helped!! ^^)

5 0
3 years ago
Read 2 more answers
Other questions:
  • Nc3
    12·1 answer
  • The ratio of the weight of a substance to the weight of equal volume of water is known as a) Density b) specific gravity c) spec
    8·1 answer
  • Se requiere un permiso aprobación o restricción contaminante para todos los métodos comerciales de descarga de aguas residuales
    13·1 answer
  • Que es resistencia ?
    15·1 answer
  • Part A - Transmitted power A solid circular rod is used to transmit power from a motor to a machine. The diameter of the rod is
    8·1 answer
  • Air initially at 120 psia and 500o F is expanded by an adiabatic turbine to 15 psia and 200o F. Assuming air can be treated as a
    10·1 answer
  • The two shafts of a Hooke’s coupling have their axes inclined at 20°.The shaft A revolves at a uniform speed of 1000 rpm. The sh
    5·1 answer
  • Users say that the game is interesting to look at but the music gets annoying
    9·1 answer
  • What do you do if building doesn't have enough water pressure for sprinklers? a. Increase pipe size b. Adjust budget to accommod
    15·1 answer
  • Which level of acceleration should you use when accelerating on a short highway entry ramp?
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!