MicrowaveOven.java

Download Report

Transcript MicrowaveOven.java

1
Tutorial 18 – Microwave Oven
Application
Building Your Own Classes and Objects
Outline
18.1
18.2
18.3
18.4
18.5
18.6
18.7
18.8
18.9
Test-Driving the Microwave Oven Application
Designing the Microwave Oven Application
Initializing Objects: Constructors
Get and Set Methods
Completing the Microwave Oven Application
Controlling Access to Members
The main Method
Using the Debugger: The watch Command
Wrap-Up
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
2
Objectives
• In this tutorial, you will learn to:
–
–
–
–
–
Declare your own classes.
Create and use objects of your own classes.
Control access to instance variables.
Use keyword private.
Declare your own get and set methods.
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
3
18.1
Test-Driving the Microwave Oven
Application
Application Requirements
An electronics company is considering building microwave ovens. The
company has asked you to develop an application that simulates a microwave
oven. The oven will contain a keypad that allows the user to specify the
microwave cooking time, which is displayed for the user. Once a time is
entered, the user clicks the Start JButton to begin the cooking process. The
microwave’s glass window changes color (from gray to yellow) to simulate the
oven’s light that remains on while the food cooks, and a timer counts down
one second at a time. Once the time expires, the color of the microwave’s glass
window returns to gray (indicating that the microwave’s light is now off) and
the microwave displays the text Done!. The user can click the Clear JButton at
any time to stop the microwave and enter a new time. The user should be able
to enter a number of minutes no greater than 59 and a number of seconds no
greater than 59; otherwise, the invalid cooking time will be set to zero.
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
4
18.1
Test-Driving the Microwave Oven
Application (Cont.)
• Running the completed application
– Open a Command Prompt
• Change to MircowaveOven directory
• Type java MircowaveOven
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
5
18.1
Test-Driving the Microwave Oven
Application (Cont.)
Figure 18.1 Microwave Oven application GUI displayed
when your application is executed.
Microwave’s
glass window
Numeric Keypad
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
6
18.1
Test-Driving the Microwave Oven
Application (Cont.)
Figure 18.2
Microwave Oven application accepts only four digits.
• Enter test data into application
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
7
18.1
Test-Driving the Microwave Oven
Application (Cont.)
Figure 18.3
Microwave Oven application with invalid input.
• Microwave doesn’t accept times greater than 60 minutes
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
8
18.1
Test-Driving the Microwave Oven
Application (Cont.)
Figure 18.4 Microwave Oven application after invalid input
has been entered and the Start JButton clicked.
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
9
18.1
Test-Driving the Microwave Oven
Application (Cont.)
Figure 18.5 Microwave Oven application with valid time
entered and inside light turned on (it’s now cooking).
Color yellow simulates
microwave light
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
10
18.1
Figure 18.6
Test-Driving the Microwave Oven
Application (Cont.)
Microwave Oven application after the cooking time has elapsed.
JTextField displays Done!
when cooking has finished
Color returns to default
color to simulate that
cooking has finished
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
11
18.2
Designing the Microwave Oven
Application
When the user clicks a numeric JButton
Display the formatted time
When the user clicks the Start JButton
Display the formatted time
Store the minutes and seconds
Begin the countdown
Turn the microwave light on
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
12
18.2
Designing the Microwave Oven
Application (Cont.)
When the timer ticks (once per second)
Decrease the time by one second
If the new time is not zero
Display the new time
Else
Stop timer
Display the text “Done!”
Turn the microwave light off
When the user clicks the Clear JButton
Stop the countdown
Display the text “Microwave Oven”
Turn the microwave light off
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
13
18.2
Designing the Microwave Oven
Application (Cont.)
Action
Display the formatted time
Component/Object
displayJTextField
Event/Method
User clicks a numeric
JButton
Display the formatted time
displayJTextField
User clicks the Start
JButton
Store the minutes and seconds
Turn the microwave light on
microwaveTime
windowJPanel
Decrease the time by one second microwaveTime
Timer interval expires
MicrowaveTime,
If the new time is not zero
displayJTextField
Display the new time
clockTimer
Else
Stop the countdown
Figure 18.7 ACE table for the Microwave Oven application (Part 1 of 2)
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
14
18.2
Designing the Microwave Oven
Application (Cont.)
Action
Component/Object Event/Method
displayJTextField
Display the text “Done!”
windowJPanel
Turn the microwave light off
clockTimer
User clicks Clear JButton
Stop the countdown
Display the text “Microwave Oven” displayJTextField
windowJPanel
Turn the microwave light off
Figure 18.7 ACE table for the Microwave Oven application (Part 2 of 2)
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
15
18.2
Designing the Microwave Oven
Application (Cont.)
Figure 18.8
Empty class declaration.
Empty class declaration.
• Keyword class indicates that a class declaration follows
• Members of a class
• Methods or variables declared within the body of a class
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
16
18.2
Designing the Microwave Oven
Application (Cont.)
Figure 18.9
Instance variables store minute
and second information
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
CookingTime’s instance variables.
17
18.3
Initializing Objects: Constructors
• Constructors
– Used to initialize instance variables
– Has the same name as the class that contains it
– Similar to a method
• Cannot return a value
• Can take arguments
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
18
18.3
Initializing Objects: Constructors (Cont.)
Figure 18.10
Constructor
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
Declaring an empty constructor.
19
18.3
Initializing Objects: Constructors (Cont.)
Figure 18.11
Constructor initializing instance variables.
Constructor arguments
assigned to instance
variables
•Values will be specified for the object in the client of the
class.
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
20
18.3
Initializing Objects: Constructors (Cont.)
Figure 18.12
Declaring an object of type CookingTime.
Declaring an instance of
class CookingTime
• Java is an extensible language.
• Can be extended with new classes.
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
21
18.4
Get and Set Methods
• Access and modify instance variables
– Get methods
• Obtain the value of an instance variable
• Also called accessors
– Set methods
• Set the value of an instance variable
• Also called mutators
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
22
18.4
Get and Set Methods (Cont.)
• Maintaining data in a consistent state
– Set methods ensure that each instance variable contains a
proper value
• Instance variable minute should only contain values between
0 and 59
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
23
18.4
Get and Set Methods (Cont.)
Figure 18.13 getMinute declaration.
Returning a value
from a get method
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
24
18.4
Get and Set Methods (Cont.)
Figure 18.14 setMinute declaration.
Code used to validate data
• setMinute accepts positive values less than 60.
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
25
18.4
Get and Set Methods (Cont.)
Figure 18.15
Code to return the
second value
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
getSecond declaration.
26
18.4
Get and Set Methods (Cont.)
Figure 18.16
setSecond declaration.
Method setSecond is
similar to setMinute
• setSecond accepts positive values less than 60.
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
27
18.4
Get and Set Methods (Cont.)
Figure 18.17
isDone declaration.
Testing whether both minute
and second are equal to 0
• Returns true if microwave is done cooking, false otherwise
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
28
18.4
Get and Set Methods (Cont.)
Figure 18.18
Constructor using set methods to initialize variables.
Assign data by calling
methods setMinute
and setSecond
• Calling set methods ensure that only valid data will be
assigned to minute and second.
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
29
18.4
Get and Set Methods (Cont.)
• The this reference
– Refer to the current object of the class with the keyword
this.
– this is automatically added to the method call
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
30
18.4
Get and Set Methods (Cont.)
Figure 18.19
Decrementing the time in the tick method.
Decrementing second
Decrementing
minute and setting
second to 59
• Decrements the time by one second.
• Save your source code file.
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
31
18.5
Figure 18.20
Completing the Microwave Oven
Application
Creating clockTimer with a delay of 1000 milliseconds.
Setting
clockTimer’s delay
• Time is measured in milliseconds.
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
32
18.5
Figure 18.21
Completing the Microwave Oven
Application (Cont.)
Declaring currentTime to hold timeToDisplay value.
Copy timeToDisplay
into currentTime
• Make the time output 4 characters long.
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
33
18.5
Completing the Microwave Oven
Application (Cont.)
Figure 18.22
Extracting minutes and seconds from currentTime.
Calling two different
versions of String
method substring
• Format output as two digits, followed by a colon, followed by
two digits.
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
34
18.5
Completing the Microwave Oven
Application (Cont.)
Figure 18.23 Starting the Microwave Oven countdown.
Start timer and turn
“light” on to indicate
microwave oven is
cooking
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
35
18.5
Completing the Microwave Oven
Application (Cont.)
Figure 18.24
Code that turns off
microwave and
resets variables
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
Clearing the Microwave Oven input.
36
18.5
Completing the Microwave Oven
Application (Cont.)
Figure 18.25
Appending digit and formatting timeToDisplay.
Appending digit
to instance variable
timeToDisplay
• Display the data as it is being input
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
37
18.5
Completing the Microwave Oven
Application (Cont.)
Figure 18.26
Decrements time
during countdown
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
Modifying the display during countdown.
38
18.5
Completing the Microwave Oven
Application (Cont.)
Figure 18.27
Turning off the microwave when the timer runs out.
Stopping clockTimer
• stop method turns off the clockTimer.
• Reset background color to gray.
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
39
18.5
Completing the Microwave Oven
Application (Cont.)
Figure 18.28
Running the Microwave Oven application.
• Save the changes to your code
• Compile and run in the Command Prompt
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
40
18.6
Figure 18.29
Controlling Access to Members
Declaring CookingTime’s instance variables as private.
Declaring instance
variables of class
CookingTime, private
• private data is only accessible to members of the class.
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
41
18.6
Controlling Access to Members (Cont.)
Figure 18.30
Declaring MicrowaveOven’s instance variables as private.
Declaring instance
variables of class
MicrowaveOven,
private
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
42
18.6
Controlling Access to Members (Cont.)
Figure 18.31
Declaring the displayTime method as private.
Declaring method
displayTime of class
MicrowaveOven, private
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Tutorial 18: MicrowaveOven.java
// Mimics the behavior of a microwave oven.
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import javax.swing.*;
import javax.swing.border.*;
43
Outline
MicrowaveOven.java
(1 of 22)
public class MicrowaveOven extends JFrame
{
// JPanel for microwave window
private JPanel windowJPanel;
// JPanel for microwave controls
private JPanel controlJPanel;
// JTextField for cooking time
private JTextField displayJTextField;
// JButtons to set cooking time
private JButton oneJButton;
private JButton twoJButton;
private JButton threeJButton;
private JButton fourJButton;
private JButton fiveJButton;
 2004 Prentice Hall, Inc.
All rights reserved.
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
private
private
private
private
private
JButton
JButton
JButton
JButton
JButton
sixJButton;
sevenJButton;
eightJButton;
nineJButton;
zeroJButton;
44
Outline
MicrowaveOven.java
(2 of 22)
// JButtons to start and clear timer
private JButton startJButton;
private JButton clearJButton;
// Timer to count down seconds
private Timer clockTimer;
// String for storing digits entered by user
private String timeToDisplay = "";
Declaring instance variable
// CookingTime instance for storing the current time
private CookingTime time = new CookingTime( 0, 0 );
Declaring instance variable
timeToDisplay private
cookingTime private
// DecimalFormat to format time output
private DecimalFormat timeFormat = new DecimalFormat( "00" );
 2004 Prentice Hall, Inc.
All rights reserved.
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
45
// no-argument constructor
public MicrowaveOven()
{
createUserInterface();
}
Outline
MicrowaveOven.java
(3 of 22)
// create and position GUI components; register event handlers
private void createUserInterface()
{
// get content pane for attaching GUI components
Container contentPane = getContentPane();
// enable explicit positioning of GUI components
contentPane.setLayout( null );
// set up windowJPanel
windowJPanel = new JPanel();
windowJPanel.setBounds( 16, 16, 328, 205 );
windowJPanel.setBorder( new LineBorder( Color.BLACK ) );
contentPane.add( windowJPanel );
 2004 Prentice Hall, Inc.
All rights reserved.
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// set up controlJPanel
controlJPanel = new JPanel();
controlJPanel.setBounds( 368, 16, 149, 205 );
controlJPanel.setBorder( new LineBorder( Color.BLACK ) );
controlJPanel.setLayout( null );
contentPane.add( controlJPanel );
46
Outline
MicrowaveOven.java
(4 of 22)
// set up displayJTextField
displayJTextField = new JTextField();
displayJTextField.setBounds( 7, 5, 135, 42 );
displayJTextField.setText( "Microwave Oven" );
displayJTextField.setHorizontalAlignment( JTextField.CENTER );
displayJTextField.setEditable( false );
controlJPanel.add( displayJTextField );
// set up oneJButton
oneJButton = new JButton();
oneJButton.setBounds( 13, 59, 41, 24 );
oneJButton.setText( "1" );
controlJPanel.add( oneJButton );
oneJButton.addActionListener(
 2004 Prentice Hall, Inc.
All rights reserved.
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
new ActionListener() // anonymous inner class
{
// event handler called when oneJButton is pressed
public void actionPerformed( ActionEvent event )
{
oneJButtonActionPerformed( event );
}
47
Outline
MicrowaveOven.java
(5 of 22)
} // end anonymous inner class
); // end call to addActionListener
// set up twoJButton
twoJButton = new JButton();
twoJButton.setBounds( 54, 59, 41, 24 );
twoJButton.setText( "2" );
controlJPanel.add( twoJButton );
twoJButton.addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when twoJButton is pressed
 2004 Prentice Hall, Inc.
All rights reserved.
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
public void actionPerformed( ActionEvent event )
{
twoJButtonActionPerformed( event );
}
} // end anonymous inner class
48
Outline
MicrowaveOven.java
(6 of 22)
); // end call to addActionListener
// set up threeJButton
threeJButton = new JButton();
threeJButton.setBounds( 95, 59, 41, 24 );
threeJButton.setText( "3" );
controlJPanel.add( threeJButton );
threeJButton.addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when threeJButton is pressed
public void actionPerformed( ActionEvent event )
{
threeJButtonActionPerformed( event );
}
} // end anonymous inner class
 2004 Prentice Hall, Inc.
All rights reserved.
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
49
); // end call to addActionListener
// set up fourJButton
fourJButton = new JButton();
fourJButton.setBounds( 13, 83, 41, 24 );
fourJButton.setText( "4" );
controlJPanel.add( fourJButton );
fourJButton.addActionListener(
Outline
MicrowaveOven.java
(7 of 22)
new ActionListener() // anonymous inner class
{
// event handler called when fourJButton is pressed
public void actionPerformed( ActionEvent event )
{
fourJButtonActionPerformed( event );
}
} // end anonymous inner class
); // end call to addActionListener
 2004 Prentice Hall, Inc.
All rights reserved.
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// set up fiveJButton
fiveJButton = new JButton();
fiveJButton.setBounds( 54, 83, 41, 24 );
fiveJButton.setText( "5" );
controlJPanel.add( fiveJButton );
fiveJButton.addActionListener(
50
Outline
MicrowaveOven.java
(8 of 22)
new ActionListener() // anonymous inner class
{
// event handler called when fiveJButton is pressed
public void actionPerformed( ActionEvent event )
{
fiveJButtonActionPerformed( event );
}
} // end anonymous inner class
); // end call to addActionListener
// set up sixJButton
sixJButton = new JButton();
sixJButton.setBounds( 95, 83, 41, 24 );
sixJButton.setText( "6" );
controlJPanel.add( sixJButton );
sixJButton.addActionListener(
 2004 Prentice Hall, Inc.
All rights reserved.
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
51
new ActionListener() // anonymous inner class
{
// event handler called when sixJButton is pressed
public void actionPerformed( ActionEvent event )
{
sixJButtonActionPerformed( event );
}
Outline
MicrowaveOven.java
(9 of 22)
} // end anonymous inner class
); // end call to addActionListener
// set up sevenJButton
sevenJButton = new JButton();
sevenJButton.setBounds( 13, 107, 41, 24 );
sevenJButton.setText( "7" );
controlJPanel.add( sevenJButton );
sevenJButton.addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when sevenJButton is pressed
 2004 Prentice Hall, Inc.
All rights reserved.
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
public void actionPerformed( ActionEvent event )
{
sevenJButtonActionPerformed( event );
}
} // end anonymous inner class
52
Outline
MicrowaveOven.java
(10 of 22)
); // end call to addActionListener
// set up eightJButton
eightJButton = new JButton();
eightJButton.setBounds( 54, 107, 41, 24 );
eightJButton.setText( "8" );
controlJPanel.add( eightJButton );
eightJButton.addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when eightJButton is pressed
public void actionPerformed( ActionEvent event )
{
eightJButtonActionPerformed( event );
}
} // end anonymous inner class
 2004 Prentice Hall, Inc.
All rights reserved.
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
53
); // end call to addActionListener
// set up nineJButton
nineJButton = new JButton();
nineJButton.setBounds( 95, 107, 41, 24 );
nineJButton.setText( "9" );
controlJPanel.add( nineJButton );
nineJButton.addActionListener(
Outline
MicrowaveOven.java
(11 of 22)
new ActionListener() // anonymous inner class
{
// event handler called when nineJButton is pressed
public void actionPerformed( ActionEvent event )
{
nineJButtonActionPerformed( event );
}
} // end anonymous inner class
); // end call to addActionListener
 2004 Prentice Hall, Inc.
All rights reserved.
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// set up zeroJButton
zeroJButton = new JButton();
zeroJButton.setBounds( 54, 131, 41, 24 );
zeroJButton.setText( "0" );
controlJPanel.add( zeroJButton );
zeroJButton.addActionListener(
54
Outline
MicrowaveOven.java
(12 of 22)
new ActionListener() // anonymous inner class
{
// event handler called when zeroJButton is pressed
public void actionPerformed( ActionEvent event )
{
zeroJButtonActionPerformed( event );
}
} // end anonymous inner class
); // end call to addActionListener
// set up startJButton
startJButton = new JButton();
startJButton.setBounds( 7, 171, 64, 24 );
startJButton.setText( "Start" );
controlJPanel.add( startJButton );
startJButton.addActionListener(
 2004 Prentice Hall, Inc.
All rights reserved.
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
55
new ActionListener() // anonymous inner class
{
// event handler called when startJButton is pressed
public void actionPerformed( ActionEvent event )
{
startJButtonActionPerformed( event );
}
Outline
MicrowaveOven.java
(13 of 22)
} // end anonymous inner class
); // end call to addActionListener
// set up clearJButton
clearJButton = new JButton();
clearJButton.setBounds( 79, 171, 64, 24 );
clearJButton.setText( "Clear" );
controlJPanel.add( clearJButton );
clearJButton.addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when clearJButton is pressed
 2004 Prentice Hall, Inc.
All rights reserved.
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
public void actionPerformed( ActionEvent event )
{
clearJButtonActionPerformed( event );
}
} // end anonymous inner class
56
Outline
MicrowaveOven.java
(14 of 22)
); // end call to addActionListener
// set up timerActionListener
ActionListener timerListener =
new ActionListener() // anonymous inner class
{
// event handler called every 1000 milliseconds
public void actionPerformed( ActionEvent event )
{
clockTimerActionPerformed( event );
}
}; // end anonymous inner class
// set up clockTimer
clockTimer = new Timer( 1000, timerActionListener );
 2004 Prentice Hall, Inc.
All rights reserved.
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// set properties of application’s window
setTitle( "Microwave Oven" ); // set title-bar string
setSize( 536, 261 );
// set window size
setVisible( true );
// display window
} // end method createUserInterface
57
Outline
MicrowaveOven.java
(15 of 22)
// add digit 1 to timeToDisplay
private void oneJButtonActionPerformed( ActionEvent event )
{
displayTime( "1" ); // display time input properly
} // end method oneJButtonActionPerformed
// add digit 2 to timeToDisplay
private void twoJButtonActionPerformed( ActionEvent event )
{
displayTime( "2" ); // display time input properly
} // end method twoJButtonActionPerformed
 2004 Prentice Hall, Inc.
All rights reserved.
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// add digit 3 to timeToDisplay
private void threeJButtonActionPerformed( ActionEvent event )
{
displayTime( "3" ); // display time input properly
} // end method threeJButtonActionPerformed
58
Outline
MicrowaveOven.java
(16 of 22)
// add digit 4 to timeToDisplay
private void fourJButtonActionPerformed( ActionEvent event )
{
displayTime( "4" ); // display time input properly
} // end method fourJButtonActionPerformed
// add digit 5 to timeToDisplay
private void fiveJButtonActionPerformed( ActionEvent event )
{
displayTime( "5" ); // display time input properly
} // end method fiveJButtonActionPerformed
// add digit 6 to timeToDisplay
private void sixJButtonActionPerformed( ActionEvent event )
{
displayTime( "6" ); // display time input properly
 2004 Prentice Hall, Inc.
All rights reserved.
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
59
} // end method sixJButtonActionPerformed
// add digit 7 to timeToDisplay
private void sevenJButtonActionPerformed( ActionEvent event )
{
displayTime( "7" ); // display time input properly
Outline
MicrowaveOven.java
(17 of 22)
} // end method sevenJButtonActionPerformed
// add digit 8 to timeToDisplay
private void eightJButtonActionPerformed( ActionEvent event )
{
displayTime( "8" ); // display time input properly
} // end method eightJButtonActionPerformed
// add digit 9 to timeToDisplay
private void nineJButtonActionPerformed( ActionEvent event )
{
displayTime( "9" ); // display time input properly
} // end method nineJButtonActionPerformed
 2004 Prentice Hall, Inc.
All rights reserved.
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// add digit 0 to timeToDisplay
private void zeroJButtonActionPerformed( ActionEvent event )
{
displayTime( "0" ); // display time input properly
} // end method zeroJButtonActionPerformed
60
Outline
MicrowaveOven.java
(18 of 22)
// format the time so that it has exactly four digits
private String formatTime()
{
// declare String currentTime to manipulate output
String currentTime = timeToDisplay;
// add zeros to currentTime until it is 4 characters long
for ( int i = currentTime.length(); i < 4; i++ )
{
currentTime = "0" + currentTime;
}
// if the length of currentTime is greater than four
if ( currentTime.length() > 4 )
{
// shorten currentTime to the first four characters
currentTime = currentTime.substring( 0, 4 );
}
Lengthening the
time, if necessary
Shortening the
time, if necessary
 2004 Prentice Hall, Inc.
All rights reserved.
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
61
return currentTime;
} // end method formatTime
// start the microwave oven
private void startJButtonActionPerformed( ActionEvent event )
{
// get the time as four digits
String fourDigitTime = formatTime();
// extract seconds and minutes
String minute = fourDigitTime.substring( 0, 2 );
String second = fourDigitTime.substring( 2 );
// initialize CookingTime object to time entered by user
microwaveTime.setMinute( Integer.parseInt( minute ) );
microwaveTime.setSecond( Integer.parseInt( second ) );
// display starting time in displayJTextField
displayJTextField.setText( timeFormat.format(
microwaveTime.getMinute() ) + ":" + timeFormat.format(
microwaveTime.getSecond() ) );
Outline
MicrowaveOven.java
(19 of 22)
Storing the time as
exactly four digits
Obtaining the
minutes and seconds
Setting the minutes
and seconds for the
cooking time
Displaying the
starting cooking time
timeToDisplay = 0; // clear timeToDisplay for future input
 2004 Prentice Hall, Inc.
All rights reserved.
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
62
clockTimer.start();
// start timer
windowJPanel.setBackground( Color.YELLOW ); // turn "light" on
} // end method startJButtonActionPerformed
// clear the microwave oven
private void clearJButtonActionPerformed( ActionEvent event )
{
// stop Timer and reset variables to their initial settings
clockTimer.stop();
displayJTextField.setText( "Microwave Oven" );
timeToDisplay = "";
windowJPanel.setBackground( new Color( 204, 204, 204 ) );
Outline
MicrowaveOven.java
(20 of 22)
Starting the microwave
Stopping the
microwave and clearing
the cooking time
} // end method clearJButtonActionPerformed
// display formatted time in displayJTextField
private void displayTime( String digit )
{
// append digit to timeToDisplay
timeToDisplay += digit;
// get the time as four digits
String fourDigitTime = formatTime();
Adding a digit to a time
Storing the time as
exactly four digits
 2004 Prentice Hall, Inc.
All rights reserved.
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
63
// extract seconds and minutes
String minute = fourDigitTime.substring( 0, 2 );
String second = fourDigitTime.substring( 2 );
// display number of minutes, ":", and then number of seconds
displayJTextField.setText( minute + ":" + second );
} // end method displayTime
// decrement displayJTextField by one second
private void clockTimerActionPerformed( ActionEvent event )
{
// decrement time by one second
time.tick();
// if time has not reached zero
if ( !time.isDone() )
{
// display remaining cooking time in displayJTextField
displayJTextField.setText( timeFormat.format(
time.getMinute() ) + ":" + timeFormat.format(
time.getSecond() ) );
Outline
MicrowaveOven.java
(21 of 22)
Obtaining the
minutes and seconds
Outputting the
formatted time
Decrementing the
cooking time
Displaying the
remaining cooking time
} // end if
 2004 Prentice Hall, Inc.
All rights reserved.
498
else // time has reached zero
499
{
500
clockTimer.stop(); // stop timer
Stopping the
501
microwave
502
// inform user timer is finished
503
displayJTextField.setText( "Done!" );
504
windowJPanel.setBackground( new Color( 204, 204, 204 ) );
505
506
} // end else
507
508
} // end method clockTimerActionPerformed
509
510
// main method
511
public static void main( String args[] )
512
{
513
MicrowaveOven application = new MicrowaveOven();
514
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
515
516
} // end method main
517
518 } // end class MicrowaveOven
64
Outline
MicrowaveOven.java
(22 of 22)
Notifying the user
that the microwave
has stopped
 2004 Prentice Hall, Inc.
All rights reserved.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Tutorial 18: CookingTime.java
// Represents time data and contains get and set methods.
public class CookingTime
{
// integers for storing minutes and seconds
private int minute;
private int second;
// CookingTime constructor, minute and second supplied
public CookingTime( int minuteValue, int secondValue )
{
setMinute( minuteValue );
setSecond( secondValue );
65
Outline
CookingTime.java
(1 of 4)
Declaring instance
variables of class
CookingTime private
Construcor of class
CookingTime
} // end constructor
// return minute value
public int getMinute()
{
return minute;
Returning value of
instance variable minute
} // end method getMinute
 2004 Prentice Hall, Inc.
All rights reserved.
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// set minute value
public void setMinute( int value )
{
if ( value >= 0 && value < 60 )
{
minute = value; // minute is valid
}
else
{
minute = 0; // set invalid input to 0
}
66
Outline
CookingTime.java
(2 of 4)
Validating user
input for instance
variable minute
} // end method setMinute
// return second value
public int getSecond()
{
return second;
Returning value of
instance variable
second
} // end method getSecond
 2004 Prentice Hall, Inc.
All rights reserved.
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// set second value
public void setSecond( int value )
{
if ( value >= 0 && value < 60 )
{
second = value; // second is valid
}
else
{
second = 0; // set invalid input to 0
}
67
Outline
CookingTime.java
(3 of 4)
Validating user
input for instance
variable second
} // end method setSecond
// return whether or not the time has reached zero
public boolean isDone()
{
return ( minute == 0 && second == 0 );
} // end method isDone
Testing whether
minute and second
are both equal to 0
 2004 Prentice Hall, Inc.
All rights reserved.
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// decrement the time by one second
public void tick()
{
// handle case when seconds need
if ( getSecond() > 0 )
{
setSecond( getSecond() - 1 );
}
// handle case when minutes must
else if ( getMinute() > 0 )
{
setMinute( getMinute() - 1 );
setSecond( 59 );
}
68
Outline
to be decremented
CookingTime.java
(4 of 4)
// subtract one second
Decrementing
be decremented
second
// subtract one minute
// set seconds to 59
Decrementing
minute and setting
second to 59
} // end method tick
} // end class CookingTime
 2004 Prentice Hall, Inc.
All rights reserved.
69
18.7
Figure 18.34
The main Method
Method header for the main method.
main is declared static
Calling MicrowaveOven’s
constructor
• main method is the entry point of the application
•always declared static
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
70
18.7
Figure 18.35
The main Method (Cont.)
Constructor calling the createUserInterface method.
Calling method
createUserInterface
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
18.8
Figure 18.36
Using the Debugger: The watch
Command
Setting a watch on CookingTime’s instance variable second.
Setting a watch
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
71
18.8
Figure 18.37
Using the Debugger: The watch
Command (Cont.)
Microwave Oven application stops when microwaveTime is created.
Debugger notifying you
when the value changes
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
72
18.8
Figure 18.38
Using the Debugger: The watch
Command (Cont.)
Changing the value of second by starting the microwave.
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
73
18.8
Figure 18.39
Using the Debugger: The watch
Command (Cont.)
Notifying the user of a change in the value of second.
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
74
18.8
Using the Debugger: The watch
Command (Cont.)
Figure 18.40
Removing the watch from
an instance variable
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
Removing the watch.
75