26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 5 Control Structures: Part 2 217<br />

Fig. Fig. 5.9 5.9 5.9 Using the do/while repetition structure (part 2 of 2).<br />

In method paint (lines 13–26), line 18 declares control variable counter and initializes<br />

it <strong>to</strong> 1. Upon entering the do/while structure, lines 21–22 send the drawOval<br />

message <strong>to</strong> the Graphics object <strong>to</strong> which g refers. The four arguments that represent the<br />

upper left x-coordinate, upper left y-coordinate, width and height of the oval’s bounding<br />

box (an imaginary rectangle in which the oval <strong>to</strong>uches the center of all four sides of the rectangle)<br />

are calculated based on the value of counter. The program draws the innermost<br />

oval first. The bounding box’s upper left corner for each subsequent oval moves closer <strong>to</strong><br />

the upper left corner of the applet. At the same time, the width and height of the bounding<br />

box are increased, <strong>to</strong> ensure that each new oval contains all the previous ovals. Line 23<br />

increments counter. Then, the program evaluates the loop-continuation test at the<br />

bot<strong>to</strong>m of the loop. The do/while flowchart in Fig. 5.10 makes it clear that the program<br />

does not evaluate the loop-continuation condition until after the action executes once.<br />

action(s)<br />

condition<br />

false<br />

true<br />

Fig. Fig. Fig. 5.10 5.10 5.10 Flowcharting the do/while repetition structure.<br />

© Copyright 1992–2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!