Develop an application that simulates rolling two six- sided dice. Your application should have a Roll JButton that, when clicked, displays two dice faces (images) corresponding to random numbers. It should also display the number of times each face has appeared. Your application should appear similar to Fig. 15.27. This application will help you see if dice rolling on your computer is really random. If it is, the number of 1s, 2s, 3s, 4s, 5s and 6s you roll should be about the same, at least for a large number of rolls.
a) Copying the template to your working directory. Copy the C:Examples Tutorial15ExercisesDiceSimulator directory to your C:SimplyJava direc- tory.
b) Opening the template file. Open the DiceSimulator.java file in your text editor.
c) Displaying the die image. After the rollJButtonActionPerformed method, in line
211, create a method named displayDie that takes a JLabel component as its argu- ment. This method should create a new random integer from 1 to 6 using the next- Int method of the Random object generator and assign that value to the variable face. Display the die image in the JLabel component that was passed as an argu- ment. The die image should correspond to the random number that was generated. To set the image, refer to the code presented in Fig. 15.25. Finally, this method should call the displayFrequency method to display the number of times each face has occurred.
d) Coding the rollJButtonActionPerformed method. Add code to th
```
1 // DiceSimulator.java
2 // This application rolls dice and counts the frequency of each side.
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.util.Random;
6 import javax.swing.*;
7
8 public class DiceSimulator extends JFrame
9 {
10 // JLabel and JTextField to display number of 1s
11 private JLabel side1JLabel;
12 private JTextField output1JTextField;
13
14 // JLabel and JTextField to display number of 2s
15 private JLabel side2JLabel;
16 private JTextField output2JTextField;
17
18 // JLabel and JTextField to display number of 3s
19 private JLabel side3JLabel;
20 private JTextField output3JTextField;
21
22 // JLabel and JTextField to display number of 4s
23 private JLabel side4JLabel;
24 private JTextField output4JTextField;
25
26 // JLabel and JTextField to display number of 5s
27 private JLabel side5JLabel;
28 private JTextField output5JTextField;
29
30 // JLabel and JTextField to display number of 6s
31 private JLabel side6JLabel;
32 private JTextField output6JTextField;
33
34 // JLabel and JTextField to display total rolls
35 private JLabel totalJLabel;
36 private JTextField totalJTextField;
37
38 // JLabels to display dice
39 private JLabel die1JLabel;
40 private JLabel die2JLabel;
41
42 // JButton to roll dice
43 private JButton rollJButton;
44
45 // dice face constants
46 private final int ONE = 1;
47 private final int TWO = 2;
48 private final int THREE = 3;
49 private final int FOUR = 4;
50 private final int FIVE = 5;
51 private final int SIX = 6;
52
53 // create a new random object
54 private Random generator = new Random();
55
56 // no-argument constructor
57 public DiceSimulator()
58 {
59 createUserInterface();
60 }
61
62 // create and position GUI components; register event handlers
63 private void createUserInterface()
64 {
65 // get content pane for attaching GUI components
66 Container contentPane = getContentPane();
67
68 // enable explicit positioning of GUI components
69 contentPane.setLayout( null );
70
71 // set up side1JLabel
72 side1JLabel = new JLabel();
73 side1JLabel.setBounds( 16, 16, 48, 23 );
74 side1JLabel.setText( "Side 1:" );
75 contentPane.add( side1JLabel );
76
77 // set up output1JTextField
78 output1JTextField = new JTextField();
79 output1JTextField.setBounds( 72, 16, 56, 23 );
80 output1JTextField.setText( "0" );
81 output1JTextField.setEditable( false );
82 output1JTextField.setHorizontalAlignment( JTextField.CENTER );
83 contentPane.add( output1JTextField );
84
85 // set up side2JLabel
86 side2JLabel = new JLabel();
87 side2JLabel.setBounds( 16, 48, 48, 23 );
88 side2JLabel.setText( "Side 2:" );
89 contentPane.add( side2JLabel );
90
91 // set up output2JTextField
92 output2JTextField = new JTextField();
93 output2JTextField.setBounds( 72, 48, 56, 23 );
94 output2JTextField.setText( "0" );
95 output2JTextField.setEditable( false );
96 output2JTextField.setHorizontalAlignment( JTextField.CENTER );
97 contentPane.add( output2JTextField );
98
99 // set up side3JLabel
100 side3JLabel = new JLabel();
101 side3JLabel.setBounds( 16, 80, 48, 23 );
102 side3JLabel.setText( "Side 3:" );
103 contentPane.add( side3JLabel );
104
105 // set up output3JTextField
106 output3JTextField = new JTextField();
107 output3JTextField.setBounds( 72, 80, 56, 23 );
108 output3JTextField.setText( "0" );
109 output3JTextField.setEditable( false );
110 output3JTextField.setHorizontalAlignment( JTextField.CENTER );
111 contentPane.add( output3JTextField );
112
113 // set up side4JLabel
114 side4JLabel = new JLabel();
115 side4JLabel.setBounds( 16, 112, 48, 23 );
116 side4JLabel.setText( "Side 4:" );
117 contentPane.add( side4JLabel );
118
119 // set up output4JTextField
120 output4JTextField = new JTextField();
121 output4JTextField.setBounds( 72, 111, 56, 23 );
122 output4JTextField.setText( "0" );
123 output4JTextField.setEditable( false );
124 output4JTextField.setHorizontalAlignment( JTextField.CENTER );
125 contentPane.add( output4JTextField );
126
127 // set up side5JLabel
128 side5JLabel = new JLabel();
129 side5JLabel.setBounds( 16, 144, 48, 23 );
130 side5JLabel.setText( "Side 5:" );
131 contentPane.add( side5JLabel );
132
133 // set up output5JTextField
134 output5JTextField = new JTextField();
135 output5JTextField.setBounds( 72, 144, 56, 23 );
136 output5JTextField.setText( "0" );
137 output5JTextField.setEditable( false );
138 output5JTextField.setHorizontalAlignment( JTextField.CENTER );
139 contentPane.add( output5JTextField );
140
141 // set up side6JLabel
142 side6JLabel = new JLabel();
143 side6JLabel.setBounds( 16, 176, 48, 23 );
144 side6JLabel.setText( "Side 6:" );
145 contentPane.add( side6JLabel );
146
147 // set up output6JTextField
148 output6JTextField = new JTextField();
149 output6JTextField.setBounds( 72, 176, 56, 23 );
150 output6JTextField.setText( "0" );
151 output6JTextField.setEditable( false );
152 output6JTextField.setHorizontalAlignment( JTextField.CENTER );
153 contentPane.add( output6JTextField );
154
155 // set up totalJLabel
156 totalJLabel = new JLabel();
157 totalJLabel.setBounds( 16, 208, 48, 23 );
158 totalJLabel.setText( "Total:" );
159 contentPane.add( totalJLabel );
160
161 // set up totalJTextField
162 totalJTextField = new JTextField();
163 totalJTextField.setBounds( 72, 208, 56, 23 );
164 totalJTextField.setText( "0" );
165 totalJTextField.setEditable( false );
166 totalJTextField.setHorizontalAlignment( JTextField.CENTER );
167 contentPane.add( totalJTextField );
168
169 // set up die1JLabel
170 die1JLabel = new JLabel();
171 die1JLabel.setBounds( 152, 32, 72, 64 );
172 contentPane.add( die1JLabel );
173
174 // set up die2JLabel
175 die2JLabel = new JLabel();
176 die2JLabel.setBounds( 208, 96, 72, 64 );
177 contentPane.add( die2JLabel );
178
179 // set up rollJButton
180 rollJButton = new JButton();
181 rollJButton.setBounds( 144, 208, 136, 24 );
182 rollJButton.setText( "Roll" );
183 contentPane.add( rollJButton );
184 rollJButton.addActionListener(
185
186 new ActionListener() // anonymous inner class
187 {
188 // event handler called when rollJButton is clicked
189 public void actionPerformed( ActionEvent event )
190 {
191 rollJButtonActionPerformed( event );
192 }
193
194 } // end anonymous inner class
195
196 ); // end call to addActionListener
197
198 // set properties of application’s window
199 setTitle( "Dice Simulator" ); // set title bar string
200 setSize( 300, 270 ); // set window size
201 setVisible( true ); // display window
202
203 } // end method createUserInterface
204
205 // roll dice; display frequency of each side
206 private void rollJButtonActionPerformed( ActionEvent event )
207 {
208 displayDie( die1JLabel ); // display first die
209 displayDie( die2JLabel ); // display second die
210
211 totalJTextField.setText( String.valueOf(
212 Integer.parseInt( totalJTextField.getText() ) + 2 ) );
213
214 } // end method rollJButtonActionPerformed
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240 case TWO:
241 output2JTextField.setText( String.valueOf(
242 Integer.parseInt(
243 output2JTextField.getText() ) + 1 ) );
244 break;
245
246 case THREE:
247 output3JTextField.setText( String.valueOf(
248 Integer.parseInt(
249 output3JTextField.getText() ) + 1 ) );
250 break;
251
252 case FOUR:
253 output4JTextField.setText( String.valueOf(
254 Integer.parseInt(
255 output4JTextField.getText() ) + 1 ) );
256 break;
257
258 case FIVE:
259 output5JTextField.setText( String.valueOf(
260 Integer.parseInt(
261 output5JTextField.getText() ) + 1 ) );
262 break;
263
264 case SIX:
265 output6JTextField.setText( String.valueOf(
266 Integer.parseInt(
267 output6JTextField.getText() ) + 1 ) );
268 break;
269
270 } // end switch
271
272 } // end method displayFrequency
273
274 // main method
275 public static void main( String[] args )
276 {
277 DiceSimulator application = new DiceSimulator();
278 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
279
280 } // end method main
281
282 } // end class DiceSimulator
```
You might also like to view...
Answer the following statements true (T) or false (F)
1. Integrity-related email security threats could result in unauthorized disclosure of sensitive information. 2. The MIME-Version field must have the parameter value 1.0 in order for the message to conform to RFCs 2045 and 2046. 3. For the text type of body no special software is required to get the full meaning of the text aside from support of the indicated character set. 4. The objective of MIME Transfer Encodings is to provide reliable delivery across the largest range of environments. 5. DKIM is designed to provide an email authentication technique that is not transparent to the end user.
The ________ tool allows the user to add fields to a report one at a time
A) Report Generator B) Designer Report C) Blank Report D) Report Maker
The PCAOB's standard No. 2 specifically requires auditors to understand transaction flows in designing their test of controls. What steps does this entail?
What will be an ideal response?
Case-Based Critical Thinking QuestionsCase 10-1Training unlimited is planning to create a page that will allow users to e-mail their requests for the training videos to add to their online training offerings. What should the Web designers worry about when making such a form?
A. browser versions B. e-mail harvesters C. spammers D. all of the above