Question: Write a program that thinks of a random number from 1 to 50. The user must guess the number. If the guess is too high

Write a program that thinks of a random number from 1 to 50. The user must guess the number. If the guess is too high the program says too high, if the guess is too low the program says too low, and if the guess is correct the program says correct.

It is a GUI. Here is what it looks like. It starts like this: The user guessed a number: Then another number: And finally: At this point the program has thought of a new random number, and the user can type another guess. At any time, if the user types 0 the program will exit. Calculation Description You will need an int instance variable in your class to hold the target number. Lets call it target. In the constructor you will have to initialize target to a random number from 1 to 50. The method is: import java.util.Random at the top of your program Create a new Random object, lets call it rand right now. It should be an instance variable in the class, so you can use rand again later. Random rand = new Random(); Call rand.nextInt(50) method. It will return a random number from 0 to 49. Then add 1. target = rand.nextInt(50) + 1; The actionPerformed method works like this: Read the guess number from the GUI textfield. Recall that you read it as a String, and convert it to an int using Integer.parseInt. Use several if and else if statements compare the guess number to the target and to 0. If too high or too low, put the appropriate too high or too low in the result label. If they are equal: put correct, try another in the result label. Then use rand to make a new random target number. If the guess is 0, call System.exit(0) to exit the program immediately. GUI Description The JFrame is 500 x 75 in size. From the image you can see it has 4 components: a label with instructions, a text field for input, a button to signal a new guess, and a label for the result. The instruction label has a lowered bevel border. The result label has an etched border, it starts out with some dots ..... as the text. You can put 12 pixel Box-es between many of the components and the edges for readability.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!