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
The town of Mustang, TX is concerned that waste heat discharged from a new up- stream power plant will decimate the minnow popul
vitfil [10]

Answer:

Yes the water will be safe at the point of cooling water discharge

Explanation:

Power losses in plant= 350- 350×0.35=227.5MW

Rate of heat rejection to stream= 0.75× 227.5= 170.625MW

Rate of heat rejection= rate of flow of water× c × ΔT

170625000= 150000000× 4.186 × (Final temperature- 20)

Final temperature= 20.3 ◦C

The final temperature of stream will be 20.3 ◦C. Thechange is very small so the minnows will be able to handle this temperature.

7 0
3 years ago
A strain gauge with a 5 mm gauge length gives a displacement reading of 1.25 um. Calculate the stress value given by this displa
KengaRu [80]

Answer:

stress  = 50MPa

Explanation:

given data:

Length of strain guage is 5mm

displacement\delta = 1.25 \mu m =\frac{1.25}{1000} =  0.00125 mm

stress due to displacement in structural steel can be determined by using following relation

E =\frac{stress}{strain}

stress = E \times strain

where E is young's modulus of elasticity

E for steel is 200 GPa

stress = 200\times 10^3 *\frac{1.25*10^{-3}}{5}

stress  = 50MPa

7 0
3 years ago
Say you have a random, unordered list containing 4096 four-digit numbers. Describe the most efficient way to: sort the list and
Debora [2.8K]

Answer:

Answer explained below

Explanation:

It is given that numbers are four-digit so maximum value of a number in this list could be 9999.

So we need to sort a list of integers, where each integer lies between [0,9999].

For these given constraints we can use counting sort which will run in linear time i.e. O(n).

--------------------------------------------------------------------------------

Psuedo Code:

countSort(int numList[]) {

int count[10000];

count[i] = 0; for all i;

for(int num in numList){

count[num]+= 1;

}

return count;

}

--------------------------------------------------------------------------------

Searching in this count array will be just O(1).

E.g. Lets say we want to search if 3 was present in the original list.

Case 1: it was present in the original list:

Then the count[3] would have been incremented by our sorting algorithm. so in case element exists then count value of that element will be greater than 0.

Case 2: it was not present:

In this case count[3] will remain at 0. so in case element does not exist then count of that element will be 0.

So to search for an element, say x, we just need to check if count[x]>0.

So search is O(1).

Run times:

Sorting: O(n)

Search: O(1)

6 0
3 years ago
Ages have been identified by the materials developed and used in those eras.
Westkost [7]
Ok what is the question
6 0
2 years ago
Read 2 more answers
Engineering controls are the physical changes that employers make to the work environment or to equipment that make it safer to
____ [38]

Answer:

Engineering Controls. The best engineering controls to prevent heat-related illness is to make the work environment cooler and to reduce manual workload with mechanization. A variety of engineering controls can reduce workers' exposure to heat: Air conditioning, Increased general ventilation , Cooling fans , Local exhaust ventilation at points of high heat production or moisture, Reflective shields to redirect radiant heat , Insulation of hot surfaces Elimination of steam leaks , Cooled seats or benches for rest breaks , Use of mechanical equipment to reduce manual work, Misting fans that produce a spray of fine water droplets.

Hope this helped you!

Explanation:

5 0
3 years ago
Other questions:
  • A reversible refrigerator operates between a low temperature reservoir at TL and a high temperature reservoir at TH . Its coeffi
    12·1 answer
  • 5) Two concentric spheres of diameter D1= 70 cm and D2= 120 cm are separated by an air space and have surface temperatures of T1
    13·1 answer
  • Give the approximate temperature at which creep deformation becomes an important consideration for each of the following metals:
    5·1 answer
  • If a person runs a distance of 0.7 km in 3 min, what is his average speed in kilometres/hour ​
    7·1 answer
  • The housing for a certain machinery product is made of two components, both aluminum castings. The larger component has the shap
    10·1 answer
  • Fill in the blank to output the quotient of dividing 100 by 42. print (100______42)​
    8·1 answer
  • What Number Am I?
    13·1 answer
  • 2. What is the most obvious elements of design?<br> O color<br> O shape<br> O line<br> O texture
    11·1 answer
  • What speeds did john j montgomerys gliders reach
    12·1 answer
  • Does anyone know how to fix this? It's a chromebook and project where I have to try to fix it​
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!