Chapter 16
Iterators
16.1 While Loops
First ask then do loops:
int a= 180, b=216;
while( a!=b ){
if( a> b ){ a -= b; }
else { b -= a; }
}
The loop in the example computes the greatest common divisor (gcd) of the given values. At each iteration, the
value in the variable holding the larger value is decreased by the value of the other variable. After the first iteration,
‘a’ holds the value 180 and ‘b’ holds 36. In the second iteration, the value of ‘a’ decreases to 144 and
the values of ‘b’ remains unchanghed. The process is repeated as long as the variables hold different
values.
16.2 Do-While Loops
First do then ask loops:
do{
<body>
} while( <condition> ){
16.3 Increment-Based Loops
for( i=32; i<127; i=i+1 ){
System.out.println( i+": "+ (char) i );
}
for( i=1; i<10; i++ ){
for( j=0; j<i; j++ ){
System.out.print( '*' );
}
System.out.println();
}
Exercises
16.4 Assignment #21: Iterators
Due: We, June 4, midnight
Download the art.java program from the given web page. The program contains 23 methods, named pic1, ..., pic23, whose bodies are missing. The methods
task is to create figures, using commands of the form g.drawLine(...), g.drawRecte(...), and
g.fillRect(...).
Provide the bodies for at least 6 methods according to the corresponding descriptions in the list given below. For the
other methods, you are free to choose arbitrary figures of your liking, provided they rely on loops in their
code.
- A figure consisting of 10 lines between (10,10) and (210,10 +
), where
= 0,10,20,....
- A figure consisting of 17 lines between (10,10) and (250 -
,10 +
), where
= 0,15,30,....
- A figure consisting of 17 lines between (10,300 -
) and (250,10 +
), where
= 0,15,30,....
- A figure consisting of 17 lines between (10,300 -
/3) and (250,10 +
), where
= 0,15,30,....
- A figure consisting of all the lines between (10,10 + 30i) and (250,10 + 30j), where i,j = 0,1,...,5.
- A figure consisting of all the lines between (10 + 30i,10 + 30i) and (250 - 30j,10 + 30j), where i,j = 0,1,...,5.
- A figure consisting of 17 lines between (10 +
,10) and (250,10 +
), where
= 0,15,30,....
- The figure produced by the following code for i = 0,1,...,16.
g.drawLine(10,10,250,10+i*15);
g.drawLine(10,10+i*15,250,10);
g.drawLine(10,250,250,10+i*15);
g.drawLine(10,10+i*15,250,250);
- A figure that 17 times draws the following sequence of line segments: a rightward line of length 200, a downward line
of length 200, a leftward line of length 195, an upward line of length 195. Each new line segment starts at the end
point of its predecessor.
- A figure that 17 times draws the following sequence of line segments: a rightward line, a downward line, a
leftward line, an upward line. Each new line segment starts at the end point of its predecessor. The
initial line segment has length of 200, and the length is reduced by 5 after the drawing of each pair of
segments.
- A polyline that repeatedly traverses the points (i,0), (200,i), (200 - i,200), (0,200 - i) for ten times, with
i = 0,20,....
- A figure consisting of 10 squares with a common corner and relative sizes scaled by a factor of 0.8.
- A figure consisting of 10 squares with origins moved apart horizontally and vertically by 5, and relative sizes scaled
by a factor of 0.8.
- A figure consisting of 10 squares with a common origin and relative sizes scaled by a factor of 0.8.
- A figure like the following one.
- A figure like the following one.
- A 15 by 15 checker board.
- A skewed 15 by 15 checker board, in which neighboring squares differ in size by 2.
- Another version of the checker board. All squares are of the same dimension, but the sizes of their visible parts
gradually decrease by 1.
- A figure with 200 random dots within a square of dimensions 200 by 200.
- A figure with 200 random lines within a square of dimensions 200 by 200.
- A figure with 10 random triangles within a square of dimensions 200 by 200.
- A figure with 200 random stars at random places within a square of dimensions 200 by 200. Each star is made up of
8 crossing line segments of short random lengths.
Assume ‘lab21’ for the submit command, and submit your ‘art.java’ program.
Note: Files that fail to compile, and execute when applicable, will not be examined. They will be awarded a grade
of 0 points.
Q&A