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
4. The friction point is the point
MaRussiya [10]
D. Kelsi kanals Alabama’s Italian Oslo Goldie talisman Jacoby Allah gamma Ireland Hannah Lucy UCD tv RSA was was ex
4 0
3 years ago
TOPO NÀO CÓ CẤU HÌNH ĐA ĐIỂM
bixtya [17]

Answer:

Tout à fait les gens sont nuls

3 0
3 years ago
Read 2 more answers
Explain the four criteria for proving the correctness of a logical pretest loop construct of the form "while B do S end". And pr
evablogger [386]

Answer:

Check the explanation

Explanation:

The loop invariant has to satisfy some amount of requirements to be of good use. Another complex factor as to why a loop is the question of loop termination. A loop that doesn’t terminate can’t invariably be correct, and in fact the computation in whatever form amounts to nothing. The total axiomatic description of a while construct will have to involve all of the following to be true, in which I is the loop invariant:

P => I

{I and B} S {I}

(I and (not B)) => Q

Then the loop terminates

6 0
3 years ago
If Ori gives a friend three reasons for preferring soccer to basketball, that is an algorithm.
irinina [24]

Answer:

False I'm pretty sure sorry If its wrong

3 0
2 years ago
Read 2 more answers
When designing a car that runs on wind or Air car . can you tell me the details for the following points Compressed Air Engine:
BabaBlast [244]

Answer:

a)

The crack and connecting rod is used in the design of car.This mechanism is known as slider -crank mechanism.

Components:

1.Inlet tube

2. Wheel

3. Exhaust

4. Engine

5.Air tank

6.Pressure gauge

7.Stand

8. Gate valve

b)

The efficiency of air engine is less as compare to efficiency of electric engine and this is not ecofriendly because it produce green house gases.These gases affect the environment.

c)

it can run around 722 km when it is full charge.

                                                                                                                                                     

5 0
3 years ago
Other questions:
  • . Carly's Catering provides meals for parties and special events. In Chapter 2, you wrote an application that prompts the user f
    10·1 answer
  • Write a single statement that prints outsideTemperature with 2 digits in the fraction
    8·1 answer
  • The driver of a car traveling up a 2% grade at an initial speed V0 applied the brakes abruptly and the vehicle slid to a complet
    13·1 answer
  • Local technology is foundation for modern technology? justufy this statement with example.​
    12·1 answer
  • Consider a metal single crystal oriented such that the normal to the slip plane and the slip direction are at angles of 43.1 deg
    6·1 answer
  • List and explain the major features that the following building designs must have to relate directly to their functions.
    11·1 answer
  • Con que otro nombre se le conoce a los delitos informaticos
    5·1 answer
  • Define;<br>i) Voltage<br>ii) Current<br>iii) Electrical Power<br>iv) Electrical Energy​
    10·1 answer
  • If a tapered roller bearing is adjusted to loose, the bearing will bind and overheat.
    11·1 answer
  • What parts do all circuits have in common?
    9·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!