Java Reference
In-Depth Information
12.11
Create the following GUI. You do not have to provide any functionality.
12.12 (Temperature Conversion) Write a temperature-conversion application that converts from
Fahrenheit to Celsius. The Fahrenheit temperature should be entered from the keyboard (via a
JTextField ). A JLabel should be used to display the converted temperature. Use the following for-
mula for the conversion:
Celsius = × ( Fahrenheit - 32 )
12.13 (Temperature-Conversion Modification) Enhance the temperature-conversion application
of Exercise 12.12 by adding the Kelvin temperature scale. The application should also allow the user
to make conversions between any two scales. Use the following formula for the conversion between
Kelvin and Celsius (in addition to the formula in Exercise 12.12):
Kelvin = Celsius + 273.15
12.14 (Guess-the-Number Game) Write an application that plays “guess the number” as follows:
Your application chooses the number to be guessed by selecting an integer at random in the range
1-1000. The application then displays the following in a label:
I have a number between 1 and 1000. Can you guess my number?
Please enter your first guess.
A JTextField should be used to input the guess. As each guess is input, the background color
should change to either red or blue. Red indicates that the user is getting “warmer,” and blue,
“colder.” A JLabel should display either "Too High" or "Too Low" to help the user zero in. When
the user gets the correct answer, "Correct!" should be displayed, and the JTextField used for
input should be changed to be uneditable. A JButton should be provided to allow the user to play
the game again. When the JButton is clicked, a new random number should be generated and the
input JTextField changed to be editable.
5
---
12.15 (Displaying Events) It's often useful to display the events that occur during the execution
of an application. This can help you understand when the events occur and how they're generated.
Write an application that enables the user to generate and process every event discussed in this chap-
ter. The application should provide methods from the ActionListener , ItemListener , ListSelec-
tionListener , MouseListener , MouseMotionListener and KeyListener interfaces to display
messages when the events occur. Use method toString to convert the event objects received in each
event handler into String s that can be displayed. Method toString creates a String containing all
the information in the event object.
12.16 (GUI-Based Craps Game) Modify the application of Section 6.10 to provide a GUI that
enables the user to click a JButton to roll the dice. The application should also display four JLabel s
and four JTextField s, with one JLabel for each JTextField . The JTextField s should be used to
display the values of each die and the sum of the dice after each roll. The point should be displayed
in the fourth JTextField when the user does not win or lose on the first roll and should continue
to be displayed until the game is lost.
 
 
Search WWH ::




Custom Search