Modify the tutorial’s Microwave Oven application to include an additional digit, which would represent the hour. Allow the user to enter up to 9 hours, 59 minutes and 59 seconds (Fig. 18.40).
a) Copying the template to your working directory. Copy the C:Examples Tutorial18ExercisesMicrowaveOven2 directory to your C:SimplyJava direc- tory.
b) Opening the template files. Open the MicrowaveOven.java and CookingTime.java
files in your text editor.
c) Adding the hour variable. To allow users to enter a cooking time that includes the hour digit, you will need to modify the CookingTime.java template file. Declare a new private instance variable hour (line 7). Change the CookingTime constructor starting on line 11, to take a third int named hourValue as its first parameter. The constructor header should be split into two lines for readability. Call method set- Hour from inside the constructor to set the hour instance variable.
d) Adding the getHour and setHour methods. Use the get and set methods already declared in class CookingTime as your template to create the get and set methods beginning on line 21 for the instance variable hour, in the CookingTime class.
```
1 // MicrowaveOven.java
2 // Adds an hours position to the microwave oven timer.
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.text.DecimalFormat;
6 import javax.swing.*;
7 import javax.swing.border.*;
8
9 public class MicrowaveOven extends JFrame
10 {
11 // JPanel for microwave window
12 private JPanel windowJPanel;
13
14 // JPanel for microwave controls
15 private JPanel controlJPanel;
16
17 // JTextField for cooking time
18 private JTextField displayJTextField;
19
20 // JButtons to set cooking time
21 private JButton oneJButton;
22 private JButton twoJButton;
23 private JButton threeJButton;
24 private JButton fourJButton;
25 private JButton fiveJButton;
26 private JButton sixJButton;
27 private JButton sevenJButton;
28 private JButton eightJButton;
29 private JButton nineJButton;
30 private JButton zeroJButton;
31
32 // JButtons to start and clear timer
33 private JButton startJButton;
34 private JButton clearJButton;
35
36 // Timer to count down seconds
37 private Timer clockTimer;
38
39 // String for storing digits entered by user
40 private String timeToDisplay = "";
41
42 // Time instance for storing the current time
43 private CookingTime microwaveTime = new CookingTime( 0, 0, 0 );
44
45 // DecimalFormat to format time output
46 private DecimalFormat timeFormat = new DecimalFormat( "00" );
47
48 // no-argument constructor
49 public MicrowaveOven()
50 {
51 createUserInterface();
52 }
53
54 // create and position GUI components; register event handlers
55 private void createUserInterface()
56 {
57 // get content pane for attaching GUI components
58 Container contentPane = getContentPane();
59
60 // enable explicit positioning of GUI components
61 contentPane.setLayout( null );
62
63 // set up windowJPanel
64 windowJPanel = new JPanel();
65 windowJPanel.setBounds( 16, 16, 328, 205 );
66 windowJPanel.setBorder( new LineBorder( Color.BLACK ) );
67 contentPane.add( windowJPanel );
68
69 // set up controlJPanel
70 controlJPanel = new JPanel();
71 controlJPanel.setBounds( 368, 16, 149, 205 );
72 controlJPanel.setBorder( new LineBorder( Color.BLACK ) );
73 controlJPanel.setLayout( null );
74 contentPane.add( controlJPanel );
75
76 // set up displayJTextField
77 displayJTextField = new JTextField();
78 displayJTextField.setBounds( 7, 5, 135, 42 );
79 displayJTextField.setText( "Microwave Oven" );
80 displayJTextField.setHorizontalAlignment( JTextField.CENTER );
81 displayJTextField.setEditable( false );
82 controlJPanel.add( displayJTextField );
83
84 // set up oneJButton
85 oneJButton = new JButton();
86 oneJButton.setBounds( 13, 59, 41, 24 );
87 oneJButton.setText( "1" );
88 controlJPanel.add( oneJButton );
89 oneJButton.addActionListener(
90
91 new ActionListener() // anonymous inner class
92 {
93 // event handler called when oneJButton is pressed
94 public void actionPerformed( ActionEvent event )
95 {
96 oneJButtonActionPerformed( event );
97 }
98
99 } // end anonymous inner class
100
101 ); // end call to addActionListener
102
103 // set up twoJButton
104 twoJButton = new JButton();
105 twoJButton.setBounds( 54, 59, 41, 24 );
106 twoJButton.setText( "2" );
107 controlJPanel.add( twoJButton );
108 twoJButton.addActionListener(
109
110 new ActionListener() // anonymous inner class
111 {
112 // event handler called when twoJButton is pressed
113 public void actionPerformed( ActionEvent event )
114 {
115 twoJButtonActionPerformed( event );
116 }
117
118 } // end anonymous inner class
119
120 ); // end call to addActionListener
121
122 // set up threeJButton
123 threeJButton = new JButton();
124 threeJButton.setBounds( 95, 59, 41, 24 );
125 threeJButton.setText( "3" );
126 controlJPanel.add( threeJButton );
127 threeJButton.addActionListener(
128
129 new ActionListener() // anonymous inner class
130 {
131 // event handler called when threeJButton is pressed
132 public void actionPerformed( ActionEvent event )
133 {
134 threeJButtonActionPerformed( event );
135 }
136
137 } // end anonymous inner class
138
139 ); // end call to addActionListener
140
141 // set up fourJButton
142 fourJButton = new JButton();
143 fourJButton.setBounds( 13, 83, 41, 24 );
144 fourJButton.setText( "4" );
145 controlJPanel.add( fourJButton );
146 fourJButton.addActionListener(
147
148 new ActionListener() // anonymous inner class
149 {
150 // event handler called when fourJButton is pressed
151 public void actionPerformed( ActionEvent event )
152 {
153 fourJButtonActionPerformed( event );
154 }
155
156 } // end anonymous inner class
157
158 ); // end call to addActionListener
159
160 // set up fiveJButton
161 fiveJButton = new JButton();
162 fiveJButton.setBounds( 54, 83, 41, 24 );
163 fiveJButton.setText( "5" );
164 controlJPanel.add( fiveJButton );
165 fiveJButton.addActionListener(
166
167 new ActionListener() // anonymous inner class
168 {
169 // event handler called when fiveJButton is pressed
170 public void actionPerformed( ActionEvent event )
171 {
172 fiveJButtonActionPerformed( event );
173 }
174
175 } // end anonymous inner class
176
177 ); // end call to addActionListener
178
179 // set up sixJButton
180 sixJButton = new JButton();
181 sixJButton.setBounds( 95, 83, 41, 24 );
182 sixJButton.setText( "6" );
183 controlJPanel.add( sixJButton );
184 sixJButton.addActionListener(
185
186 new ActionListener() // anonymous inner class
187 {
188 // event handler called when sixJButton is pressed
189 public void actionPerformed( ActionEvent event )
190 {
191 sixJButtonActionPerformed( event );
192 }
193
194 } // end anonymous inner class
195
196 ); // end call to addActionListener
197
198 // set up sevenJButton
199 sevenJButton = new JButton();
200 sevenJButton.setBounds( 13, 107, 41, 24 );
201 sevenJButton.setText( "7" );
202 controlJPanel.add( sevenJButton );
203 sevenJButton.addActionListener(
204
205 new ActionListener() // anonymous inner class
206 {
207 // event handler called when sevenJButton is pressed
208 public void actionPerformed( ActionEvent event )
209 {
210 sevenJButtonActionPerformed( event );
211 }
212
213 } // end anonymous inner class
214
215 ); // end call to addActionListener
216
217 // set up eightJButton
218 eightJButton = new JButton();
219 eightJButton.setBounds( 54, 107, 41, 24 );
220 eightJButton.setText( "8" );
221 controlJPanel.add( eightJButton );
222 eightJButton.addActionListener(
223
224 new ActionListener() // anonymous inner class
225 {
226 // event handler called when eightJButton is pressed
227 public void actionPerformed( ActionEvent event )
228 {
229 eightJButtonActionPerformed( event );
230 }
231
232 } // end anonymous inner class
233
234 ); // end call to addActionListener
235
236 // set up nineJButton
237 nineJButton = new JButton();
238 nineJButton.setBounds( 95, 107, 41, 24 );
239 nineJButton.setText( "9" );
240 controlJPanel.add( nineJButton );
241 nineJButton.addActionListener(
242
243 new ActionListener() // anonymous inner class
244 {
245 // event handler called when nineJButton is pressed
246 public void actionPerformed( ActionEvent event )
247 {
248 nineJButtonActionPerformed( event );
249 }
250
251 } // end anonymous inner class
252
253 ); // end call to addActionListener
254
255 // set up zeroJButton
256 zeroJButton = new JButton();
257 zeroJButton.setBounds( 54, 131, 41, 24 );
258 zeroJButton.setText( "0" );
259 controlJPanel.add( zeroJButton );
260 zeroJButton.addActionListener(
261
262 new ActionListener() // anonymous inner class
263 {
264 // event handler called when zeroJButton is pressed
265 public void actionPerformed( ActionEvent event )
266
You might also like to view...
The symbol <> is the Access comparison operator for ________
A) less than B) greater than C) equal to D) is not equal to
When a new row or column is inserted in a range of cells used in a function, the range in the function is not automatically adjusted to include that new row or column
Indicate whether the statement is true or false.