• No results found

Tutorial 18 – Microwave Oven Application

N/A
N/A
Protected

Academic year: 2021

Share "Tutorial 18 – Microwave Oven Application"

Copied!
75
0
0

Loading.... (view fulltext now)

Full text

(1)

Outline

18.1 Test-Driving the Microwave Oven Application 18.2 Designing the Microwave Oven Application 18.3 Initializing Objects: Constructors

18.4 Get and Set Methods

18.5 Completing the Microwave Oven Application 18.6 Controlling Access to Members

18.7 The main Method

18.8 Using the Debugger: The watch Command 18.9 Wrap-Up

Tutorial 18 – Microwave Oven Application

Building Your Own Classes and Objects

(2)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

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.

(3)

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.

(4)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Application (Cont.)

• Running the completed application

– Open a Command Prompt

• Change to MircowaveOven directory

• Type java MircowaveOven

(5)

Application (Cont.)

Figure 18.1 Microwave Oven application GUI displayed when your application is executed.

Microwave’s glass window

Numeric Keypad

(6)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Application (Cont.)

Figure 18.2 Microwave Oven application accepts only four digits.

• Enter test data into application

(7)

Application (Cont.)

Figure 18.3 Microwave Oven application with invalid input.

• Microwave doesn’t accept times greater than 60 minutes

(8)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Application (Cont.)

Figure 18.4 Microwave Oven application after invalid input has been entered and the Start JButton clicked.

(9)

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

(10)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Application (Cont.)

Figure 18.6 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

(11)

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

(12)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

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

(13)

Application (Cont.)

Action Component/Object Event/Method

Display the formatted time displayJTextField User clicks a numeric

JButton

Display the formatted time displayJTextField User clicks the Start JButton

Store the minutes and seconds microwaveTime

Turn the microwave light on windowJPanel

Decrease the time by one second microwaveTime Timer interval expires If the new time is not zero

Display the new time MicrowaveTime, displayJTextField

Else

Stop the countdown clockTimer

Figure 18.7 ACE table for the Microwave Oven application (Part 1 of 2)

(14)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Application (Cont.)

Action Component/Object Event/Method

Display the text “Done!” displayJTextField

Turn the microwave light off windowJPanel

Stop the countdown clockTimer User clicks Clear JButton

Display the text “Microwave Oven” displayJTextField

Turn the microwave light off windowJPanel

Figure 18.7 ACE table for the Microwave Oven application (Part 2 of 2)

(15)

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

(16)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Application (Cont.)

Figure 18.9 CookingTime’s instance variables.

Instance variables store minute and second information

(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

(18)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

18.3 Initializing Objects: Constructors (Cont.)

Figure 18.10 Declaring an empty constructor.

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.

(20)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

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.

(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

(22)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

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

(23)

18.4 Get and Set Methods (Cont.)

Figure 18.13 getMinute declaration.

Returning a value from a get method

(24)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

18.4 Get and Set Methods (Cont.)

Figure 18.14   setMinute declaration.

Code used to validate data

setMinute accepts positive values less than 60.

(25)

18.4 Get and Set Methods (Cont.)

Figure 18.15 getSecond declaration.

Code to return the

second value

(26)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

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.

(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

(28)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

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.

(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

(30)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

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.

(31)

Application

Figure 18.20 Creating clockTimer with a delay of 1000 milliseconds.

Setting

clockTimer’s delay

• Time is measured in milliseconds.

(32)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Application (Cont.)

Figure 18.21 Declaring currentTime to hold timeToDisplay value.

Copy timeToDisplay

into currentTime

• Make the time output 4 characters long.

(33)

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

(34)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Application (Cont.)

Figure 18.23 Starting the Microwave Oven countdown.

Start timer and turn

“light” on to indicate microwave oven is

cooking

(35)

Application (Cont.)

Figure 18.24 Clearing the Microwave Oven input.

Code that turns off microwave and resets variables

(36)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Application (Cont.)

Figure 18.25 Appending digit and formatting timeToDisplay.

Appending digit

to instance variable

timeToDisplay

• Display the data as it is being input

(37)

Application (Cont.)

Figure 18.26 Modifying the display during countdown.

Decrements time

during countdown

(38)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

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.

(39)

Application (Cont.)

Figure 18.28 Running the Microwave Oven application.

• Save the changes to your code

(40)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

18.6 Controlling Access to Members

Figure 18.29 Declaring CookingTime’s instance variables as private.

Declaring instance variables of class

CookingTime, private

private data is only accessible to members of the class.

(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

(42)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

18.6 Controlling Access to Members (Cont.)

Figure 18.31 Declaring the displayTime method as private.

Declaring method

displayTime of class

MicrowaveOven, private

(43)

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;

MicrowaveOven.java (1 of 22)

(44)

 2004 Prentice Hall, Inc.

All rights reserved.

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 // CookingTime instance for storing the current time 43 private CookingTime time = new CookingTime( 0, 0 );

44

45 // DecimalFormat to format time output

46 private DecimalFormat timeFormat = new DecimalFormat( "00" );

MicrowaveOven.java (2 of 22)

Declaring instance variable

timeToDisplayprivate

Declaring instance variable

cookingTimeprivate

(45)

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

MicrowaveOven.java (3 of 22)

(46)

 2004 Prentice Hall, Inc.

All rights reserved.

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

MicrowaveOven.java (4 of 22)

(47)

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

MicrowaveOven.java (5 of 22)

(48)

 2004 Prentice Hall, Inc.

All rights reserved.

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

MicrowaveOven.java (6 of 22)

(49)

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

MicrowaveOven.java (7 of 22)

(50)

 2004 Prentice Hall, Inc.

All rights reserved.

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(

MicrowaveOven.java (8 of 22)

(51)

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

MicrowaveOven.java (9 of 22)

(52)

 2004 Prentice Hall, Inc.

All rights reserved.

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

MicrowaveOven.java (10 of 22)

(53)

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

MicrowaveOven.java (11 of 22)

(54)

 2004 Prentice Hall, Inc.

All rights reserved.

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 {

267 zeroJButtonActionPerformed( event );

268 } 269

270 } // end anonymous inner class 271

272 ); // end call to addActionListener 273

274 // set up startJButton

275 startJButton = new JButton();

276 startJButton.setBounds( 7, 171, 64, 24 );

277 startJButton.setText( "Start" );

278 controlJPanel.add( startJButton );

279 startJButton.addActionListener(

MicrowaveOven.java (12 of 22)

(55)

282 {

283 // event handler called when startJButton is pressed 284 public void actionPerformed( ActionEvent event ) 285 {

286 startJButtonActionPerformed( event );

287 } 288

289 } // end anonymous inner class 290

291 ); // end call to addActionListener 292

293 // set up clearJButton

294 clearJButton = new JButton();

295 clearJButton.setBounds( 79, 171, 64, 24 );

296 clearJButton.setText( "Clear" );

297 controlJPanel.add( clearJButton );

298 clearJButton.addActionListener(

299

300 new ActionListener() // anonymous inner class 301 {

302 // event handler called when clearJButton is pressed

MicrowaveOven.java (13 of 22)

(56)

 2004 Prentice Hall, Inc.

All rights reserved.

305 clearJButtonActionPerformed( event );

306 } 307

308 } // end anonymous inner class 309

310 ); // end call to addActionListener 311

312 // set up timerActionListener 313 ActionListener timerListener = 314

315 new ActionListener() // anonymous inner class 316 {

317 // event handler called every 1000 milliseconds 318 public void actionPerformed( ActionEvent event ) 319 {

320 clockTimerActionPerformed( event );

321 } 322

323 }; // end anonymous inner class 324

325 // set up clockTimer

326 clockTimer = new Timer( 1000, timerActionListener );

327

MicrowaveOven.java (14 of 22)

(57)

330 setSize( 536, 261 ); // set window size 331 setVisible( true ); // display window 332

333 } // end method createUserInterface 334

335 // add digit 1 to timeToDisplay

336 private void oneJButtonActionPerformed( ActionEvent event ) 337 {

338 displayTime( "1" ); // display time input properly 339

340 } // end method oneJButtonActionPerformed 341

342 // add digit 2 to timeToDisplay

343 private void twoJButtonActionPerformed( ActionEvent event ) 344 {

345 displayTime( "2" ); // display time input properly 346

347 } // end method twoJButtonActionPerformed 348

MicrowaveOven.java (15 of 22)

(58)

 2004 Prentice Hall, Inc.

All rights reserved.

351 {

352 displayTime( "3" ); // display time input properly 353

354 } // end method threeJButtonActionPerformed 355

356 // add digit 4 to timeToDisplay

357 private void fourJButtonActionPerformed( ActionEvent event ) 358 {

359 displayTime( "4" ); // display time input properly 360

361 } // end method fourJButtonActionPerformed 362

363 // add digit 5 to timeToDisplay

364 private void fiveJButtonActionPerformed( ActionEvent event ) 365 {

366 displayTime( "5" ); // display time input properly 367

368 } // end method fiveJButtonActionPerformed 369

370 // add digit 6 to timeToDisplay

371 private void sixJButtonActionPerformed( ActionEvent event ) 372 {

373 displayTime( "6" ); // display time input properly

MicrowaveOven.java (16 of 22)

(59)

376

377 // add digit 7 to timeToDisplay

378 private void sevenJButtonActionPerformed( ActionEvent event ) 379 {

380 displayTime( "7" ); // display time input properly 381

382 } // end method sevenJButtonActionPerformed 383

384 // add digit 8 to timeToDisplay

385 private void eightJButtonActionPerformed( ActionEvent event ) 386 {

387 displayTime( "8" ); // display time input properly 388

389 } // end method eightJButtonActionPerformed 390

391 // add digit 9 to timeToDisplay

392 private void nineJButtonActionPerformed( ActionEvent event ) 393 {

394 displayTime( "9" ); // display time input properly 395

396 } // end method nineJButtonActionPerformed 397

MicrowaveOven.java (17 of 22)

(60)

 2004 Prentice Hall, Inc.

All rights reserved.

400 {

401 displayTime( "0" ); // display time input properly 402

403 } // end method zeroJButtonActionPerformed 404

405 // format the time so that it has exactly four digits 406 private String formatTime()

407 {

408 // declare String currentTime to manipulate output 409 String currentTime = timeToDisplay;

410

411 // add zeros to currentTime until it is 4 characters long 412 for ( int i = currentTime.length(); i < 4; i++ ) 413 {

414 currentTime = "0" + currentTime;

415 } 416

417 // if the length of currentTime is greater than four 418 if ( currentTime.length() > 4 ) 419 {

420 // shorten currentTime to the first four characters 421 currentTime = currentTime.substring( 0, 4 );

422 }

MicrowaveOven.java (18 of 22)

Lengthening the time, if necessary

Shortening the time, if necessary

(61)

425

426 } // end method formatTime 427

428 // start the microwave oven

429 private void startJButtonActionPerformed( ActionEvent event ) 430 {

431 // get the time as four digits 432 String fourDigitTime = formatTime();

433

434 // extract seconds and minutes 435 String minute = fourDigitTime.substring( 0, 2 );

436 String second = fourDigitTime.substring( 2 );

437

438 // initialize CookingTime object to time entered by user 439 microwaveTime.setMinute( Integer.parseInt( minute ) );

440 microwaveTime.setSecond( Integer.parseInt( second ) );

441

442 // display starting time in displayJTextField 443 displayJTextField.setText( timeFormat.format(

444 microwaveTime.getMinute() ) + ":" + timeFormat.format(

445 microwaveTime.getSecond() ) );

446

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

(62)

 2004 Prentice Hall, Inc.

All rights reserved.

450 windowJPanel.setBackground( Color.YELLOW ); // turn "light" on 451

452 } // end method startJButtonActionPerformed 453

454 // clear the microwave oven 455 private void clearJButtonActionPerformed( ActionEvent event ) 456 {

457 // stop Timer and reset variables to their initial settings 458 clockTimer.stop();

459 displayJTextField.setText( "Microwave Oven" );

460 timeToDisplay = "";

461 windowJPanel.setBackground( new Color( 204, 204, 204 ) );

462

463 } // end method clearJButtonActionPerformed 464

465 // display formatted time in displayJTextField 466 private void displayTime( String digit ) 467 {

468 // append digit to timeToDisplay 469 timeToDisplay += digit;

470

471 // get the time as four digits 472 String fourDigitTime = formatTime();

MicrowaveOven.java (20 of 22)

Starting the microwave

Stopping the microwave and clearing the

cooking time

Adding a digit to a time Storing the time as exactly four digits

(63)

475 String minute = fourDigitTime.substring( 0, 2 );

476 String second = fourDigitTime.substring( 2 );

477

478 // display number of minutes, ":", and then number of seconds 479 displayJTextField.setText( minute + ":" + second );

480

481 } // end method displayTime 482

483 // decrement displayJTextField by one second

484 private void clockTimerActionPerformed( ActionEvent event ) 485 {

486 // decrement time by one second 487 time.tick();

488

489 // if time has not reached zero 490 if ( !time.isDone() ) 491 {

492 // display remaining cooking time in displayJTextField 493 displayJTextField.setText( timeFormat.format(

494 time.getMinute() ) + ":" + timeFormat.format(

495 time.getSecond() ) );

496

MicrowaveOven.java (21 of 22)

Obtaining the

minutes and seconds Outputting the

formatted time Decrementing the cooking time

Displaying the

remaining cooking time

(64)

 2004 Prentice Hall, Inc.

All rights reserved.

500 clockTimer.stop(); // stop timer 501

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

MicrowaveOven.java (22 of 22)

Stopping the microwave

Notifying the user that the microwave has stopped

(65)

3

4 public class CookingTime 5 {

6 // integers for storing minutes and seconds 7 private int minute;

8 private int second;

9

10 // CookingTime constructor, minute and second supplied 11 public CookingTime( int minuteValue, int secondValue ) 12 {

13 setMinute( minuteValue );

14 setSecond( secondValue );

15

16 } // end constructor 17

18 // return minute value 19 public int getMinute() 20 {

21 return minute;

22

23 } // end method getMinute 24

CookingTime.java (1 of 4)

Declaring instance variables of class

CookingTime private

Construcor of class

CookingTime

Returning value of

instance variable minute

(66)

 2004 Prentice Hall, Inc.

All rights reserved.

27 {

28 if ( value >= 0 && value < 60 ) 29 {

30 minute = value; // minute is valid 31 }

32 else 33 {

34 minute = 0; // set invalid input to 0 35 }

36

37 } // end method setMinute 38

39 // return second value 40 public int getSecond() 41 {

42 return second;

43

44 } // end method getSecond 45

CookingTime.java (2 of 4)

Validating user input for instance variable minute

Returning value of instance variable

second

(67)

48 {

49 if ( value >= 0 && value < 60 ) 50 {

51 second = value; // second is valid 52 }

53 else 54 {

55 second = 0; // set invalid input to 0 56 }

57

58 } // end method setSecond 59

60 // return whether or not the time has reached zero 61 public boolean isDone() 62 {

63 return ( minute == 0 && second == 0 );

64

65 } // end method isDone 66

CookingTime.java (3 of 4)

Validating user input for instance variable second

Testing whether

minute and second

are both equal to 0

(68)

 2004 Prentice Hall, Inc.

All rights reserved.

69 {

70 // handle case when seconds need to be decremented 71 if ( getSecond() > 0 ) 72 {

73 setSecond( getSecond() - 1 ); // subtract one second 74 }

75 // handle case when minutes must be decremented 76 else if ( getMinute() > 0 ) 77 {

78 setMinute( getMinute() - 1 ); // subtract one minute 79 setSecond( 59 ); // set seconds to 59 80 }

81

82 } // end method tick 83

84 } // end class CookingTime

CookingTime.java (4 of 4)

Decrementing

minute and setting

second to 59

Decrementing

second

(69)

18.7 The main Method

Figure 18.34 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

(70)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

18.7 The main Method (Cont.)

Figure 18.35 Constructor calling the createUserInterface method.

Calling method

createUserInterface

(71)

Command

Figure 18.36 Setting a watch on CookingTime’s instance variable second.

Setting a watch

(72)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Command (Cont.)

Figure 18.37 Microwave Oven application stops when microwaveTime is created.

Debugger notifying you when the value changes

(73)

Command (Cont.)

Figure 18.38 Changing the value of second by starting the microwave.

(74)

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Command (Cont.)

Figure 18.39 Notifying the user of a change in the value of second.

(75)

Command (Cont.)

Figure 18.40 Removing the watch.

Removing the watch from an instance variable

References

Related documents