In Tutorial 15, your Lottery Picker application selected numbers for four different types of lotteries. In this exercise, you enhance the Lottery Picker to select four different sets of number for the five-number lottery and to pre- vent duplicate numbers from being selected (Fig. 17.34). Recall that the lottery is played as follows:

? Five-number lotteries require players to choose five unique numbers in the range of 0–39.

a) Copying the template to your work directory. Copy the C:ExamplesTutorial17 ExercisesEnhancedLotteryPicker directory to your C:SimplyJava directory.

b) Opening the template file. Open the LotteryPicker.java file in your text editor.

c) Iterating over the four lotteries. Add code on line 138 to begin a for statement in your application which will execute four times—once for each lottery. Use variable lottery as the for statement’s counter.

d) Initialize the boolean array. To generate unique numbers in each lottery, you will use a two-dimensional boolean array, uniqueNumber (declared for you in the tem- plate). When a number has been selected for a lottery, the value of that variable in the array (indexed by the lottery and the number selected) will be true. First, you must initialize the value for the lottery’s numbers to false. Add a nested for state-

ment to set the

```
1 // LotteryPicker.java
2 // This application picks randomly generated numbers for a lottery.
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.text.DecimalFormat;
6 import java.util.Random;
7 import javax.swing.*;
8
9 public class LotteryPicker extends JFrame
10 {
11 // JLabel and JTextField for first lottery
12 private JLabel oneJLabel;
13 private JTextField oneJTextField;
14
15 // JLabel and JTextField for second lottery
16 private JLabel twoJLabel;
17 private JTextField twoJTextField;
18
19 // JLabel and JTextField for third lottery
20 private JLabel threeJLabel;
21 private JTextField threeJTextField;
22
23 // JLabel and JTextField for fourth lottery
24 private JLabel fourJLabel;
25 private JTextField fourJTextField;
26
27 // JButton to generate lottery numbers
28 private JButton generateJButton;
29
30 // Random object to create random integers
31 private Random generator = new Random();
32
33 // two-dimensional array to maintain unique random numbers
34 private boolean[][] uniqueNumber = new boolean[ 4 ][ 40 ];
35
36 // one-dimensional array to store strings for output
37 private String[] output = new String[ 4 ];
38
39 // instance variable to hold the selected lottery number
40 private int selection;
41
42 // no-argument constructor
43 public LotteryPicker()
44 {
45 createUserInterface();
46 }
47
48 // create and position GUI components; register event handlers
49 private void createUserInterface()
50 {
51 // get content pane for attaching GUI components
52 Container contentPane = getContentPane();
53
54 // enable explicit positioning of GUI components
55 contentPane.setLayout( null );
56
57 // set up oneJLabel
58 oneJLabel = new JLabel();
59 oneJLabel.setBounds( 16, 18, 100, 16 );
60 oneJLabel.setText( "First lottery:" );
61 contentPane.add( oneJLabel );
62
63 // set up oneJTextField
64 oneJTextField = new JTextField();
65 oneJTextField.setBounds( 120, 16, 124, 23 );
66 oneJTextField.setHorizontalAlignment( JTextField.CENTER );
67 oneJTextField.setEditable( false );
68 contentPane.add( oneJTextField );
69
70 // set up twoJLabel
71 twoJLabel = new JLabel();
72 twoJLabel.setBounds( 16, 50, 100, 16 );
73 twoJLabel.setText( "Second lottery:" );
74 contentPane.add( twoJLabel );
75
76 // set up twoJTextField
77 twoJTextField = new JTextField();
78 twoJTextField.setBounds( 120, 48, 124, 23 );
79 twoJTextField.setHorizontalAlignment( JTextField.CENTER );
80 twoJTextField.setEditable( false );
81 contentPane.add( twoJTextField );
82
83 // set up threeJLabel
84 threeJLabel = new JLabel();
85 threeJLabel.setBounds( 16, 82, 100, 16 );
86 threeJLabel.setText( "Third lottery:" );
87 contentPane.add( threeJLabel );
88
89 // set up threeJTextField
90 threeJTextField = new JTextField();
91 threeJTextField.setBounds( 120, 80, 124, 23 );
92 threeJTextField.setHorizontalAlignment( JTextField.CENTER );
93 threeJTextField.setEditable( false );
94 contentPane.add( threeJTextField );
95
96 // set up fourJLabel
97 fourJLabel = new JLabel();
98 fourJLabel.setBounds( 16, 114, 100, 16 );
99 fourJLabel.setText( "Fourth lottery:" );
100 contentPane.add( fourJLabel );
101
102 // set up fourJTextField
103 fourJTextField = new JTextField();
104 fourJTextField.setBounds( 120, 112, 124, 23 );
105 fourJTextField.setHorizontalAlignment( JTextField.CENTER );
106 fourJTextField.setEditable( false );
107 contentPane.add( fourJTextField );
108
109 // set up generateJButton
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133 } // end method createUserInterface
134
135 // generate four random five-number lottery combinations
136 private void generateJButtonActionPerformed( ActionEvent event )
137 {
138 for ( int lottery = 0; lottery < 4; lottery++ )
139 {
140 // initialize boolean array for this lottery
141 for ( int numbers = 0; numbers < 40; numbers++ )
142 {
143 uniqueNumber[ lottery ][ numbers ] = false;
144
145 } // end inner for
146
147 // initialize string for the selections
148 output[ lottery ] = "";
149
150 // select five numbers
151 for ( int picks = 0; picks < 5; picks++ )
152 {
153 do
154 {
155 // select a random number
156 selection = generate( 0, 39 );
157 }
158 while ( uniqueNumber[ lottery ][ selection ] );
159
160 // set the index for the number selected to true
161 uniqueNumber[ lottery ][ selection ] = true;
162
163 // add the selection to the output string
164 output[ lottery ] += " " + selection;
165
166 } // end inner for
167
168 } // end outer for
169
169
170 oneJTextField.setText( output[ 0 ] );
171 twoJTextField.setText( output[ 1 ] );
172 threeJTextField.setText( output[ 2 ] );
173 fourJTextField.setText( output[ 3 ] );
174
175 } // end method generateJButtonActionPerformed
176
177 //
178 private int generate( int low, int high )
179 {
180 // generate random number in range from
181 return low + generator.nextInt( high -
182
183 } // end method generate
184
185 //
186 public static void main( String[] args )
187 {
188 LotteryPicker application = new LotteryPicker();
189 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
190
191 } // end method main
192
193 } // end class LotteryPicker
```

Computer Science & Information Technology

You might also like to view...

Column style reports organizes data in rows with headings at the beginning

Indicate whether the statement is true or false

Computer Science & Information Technology

Quick Start tables are ________

A) fonts that use HTML coding B) tables created from templates C) all objects created from templates D) used to store memos, lists, or notes

Computer Science & Information Technology