Develop an application that generates a random number and prompts the user to guess the number (Fig. 15.26). When the user clicks the New Game JButton, the application chooses a number in the range 1 to 100 at random. The user enters guesses into the Guess: JTextField and clicks the Enter JButton. If the guess is cor- Tutorial 15 Craps Game Application 317 rect, the game ends, and the user can start a new game. If the guess is not correct, the application should indicate if the guess is higher or lower than the correct number.
a) Copying the template to your working directory. Copy the C:Examples Tutorial15ExercisesGuessNumber directory to your C:SimplyJava directory.
b) Opening the template file. Open the GuessNumber.java file in your text editor.
c) Creating a Random object. In line 28, create two instance variables. The first variable should store a Random object and the second variable should store a randomly gener- ated number in the range 1 to 100 created using the Random object.
d) Adding code to the enterJButtonActionPerformed method. Add code starting in line 133 to the enterJButtonActionPerformed method that retrieves the value entered by the user in guessJTextField and compares that value to the randomly generated number. If the user’s guess is lower than the correct answer, display Too low... in resultJTextField. If the user’s guess is higher than the correct answer, dis-
play Too high... in resultJTextField. If the guess is correct, display Correct! in resultJTextField. Then disable
```
1 //GuessNumber.java
2 // This game asks the user to guess a number from 1 to 100.
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.util.Random;
6 import javax.swing.*;
7
8 public class GuessNumber extends JFrame
9 {
10 // JLabels to display question
11 private JLabel question1JLabel;
12 private JLabel question2JLabel;
13
14 // JLabel and JTextField to get user guess
15 private JLabel guessJLabel;
16 private JTextField guessJTextField;
17
18 // JButton to enter guess
19 private JButton enterJButton;
20
21 // JLabel and JTextField to display if user is correct
22 private JLabel resultJLabel;
23 private JTextField resultJTextField;
24
25 // JButton to start a new game
26 private JButton newGameJButton;
27
28 // declare instance variables
29 private Random generator = new Random();
30 private int number = 1 + generator.nextInt( 100 );
31
32 // no-argument constructor
33 public GuessNumber()
34 {
35 createUserInterface();
36 }
37
38 // create and position GUI components; register event handlers
39 private void createUserInterface()
40 {
41 // get content pane for attaching GUI components
42 Container contentPane = getContentPane();
43
44 // enable explicit positioning of GUI components
45 contentPane.setLayout( null );
46
47 // set up question1JLabel
48 question1JLabel = new JLabel();
49 question1JLabel.setText(
50 "I have a number between 1 and 100." );
51 question1JLabel.setBounds( 40, 10, 216, 34 );
52 contentPane.add( question1JLabel );
53
54 // set up question2JLabel
55 question2JLabel = new JLabel();
56 question2JLabel.setText( "Can you guess my number?" );
57 question2JLabel.setBounds( 40, 24, 216, 34 );
58 contentPane.add( question2JLabel );
59
60 // set up guessJLabel
61 guessJLabel = new JLabel();
62 guessJLabel.setText( "Guess:" );
63 guessJLabel.setBounds( 16, 74, 40, 20 );
64 contentPane.add( guessJLabel );
65
66 // set up guessJTextField
67 guessJTextField = new JTextField();
68 guessJTextField.setBounds( 64, 74, 128, 20 );
69 contentPane.add( guessJTextField );
70
71 // set up enterJButton
72 enterJButton = new JButton();
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128 } // end method createUserInterface
129
130 // compare guess to answer
131 private void enterJButtonActionPerformed( ActionEvent event )
132 {
133 // compare input to value
134 int guess =
135 Integer.parseInt( guessJTextField.getText() );
136
137 if ( guess < number ) // if guess is too low
138 {
139 resultJTextField.setText( "Too low..." );
140 }
141 else if ( guess > number ) // if guess is too high
142 {
143 resultJTextField.setText( "Too high..." );
144 }
145 else // if guess is right
146 {
147 resultJTextField.setText( "Correct!" );
148
149 // enable newGameJButton and disable enterJButton
150 newGameJButton.setEnabled( true );
151 enterJButton.setEnabled( false );
152 }
153
154 } // end method enterJButtonActionPerformed
155
156 //
157 private void newGameJButtonActionPerformed( ActionEvent event )
158 {
159 resultJTextField.setText( "" ); // clear resultJTextField
160
161 // generate new random number
162 number = 1 + generator.nextInt( 100 );
163
164 // enable enterJButton and disable newGameJButton
165 enterJButton.setEnabled( true );
166 newGameJButton.setEnabled( false );
167
168 } // end method newGameJButtonActionPerformed
169
170 //
171 public static void main( String[] args )
172 {
173 GuessNumber application = new GuessNumber();
174 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
175
176 } // end method main
177
178 } // end class GuessNumber
```
You might also like to view...
Given the following definition for a map, which code fragment will correctly iterate through the map and output each item?
A. for (std::map
You should use the ____ method to close a sequential access file as soon as you are finished using it.
A. Exit B. Close C. End D. Quit
In a(n) ________ column chart data appears as side by side columns.
Fill in the blank(s) with the appropriate word(s).
Animation combines two concepts we experience every moment of our lives--time and _______________.
Fill in the blank(s) with the appropriate word(s).