All pastes #444766 Raw Edit

170407 StyX

public text v1 · immutable
·published 2007-04-17 14:24 UTC
rendered paste body
Here is the definition of a method, drawTarget, to draw a single ' target' consisting of two concentric
circles with the top left corner at coordinates (x,y).
private void drawTarget(Graphics g, int x, int y) {
g.drawOval(x,y,20,20);
g.drawOval(x+5,y+5,10,10);
}
(a) Tracing through carefully, what would the following code display?
for (int a = 0; a<5; a++)
drawTarget(g, 20+a*30, 40);
(The main point of this exercise is tracing very carefully through, making sure that the components
of the for loop are used in the correct way.)
(b) Rewrite the for loop in 1a as a while loop. Why is a for loop the “conventional” solution for
coding this repetition?
(c) What would the following code display?
for (int a = 1; a<=6; a++)
if (a%2 == 0)
drawTarget(g, 20+a*30, 40);
(More detailed tracing exercise!)
(d) Modify the for loop in 1c to display the same pattern, but without the if in the loop body. [Hint:
Consider what precise set of values of a you need to use in the call
drawTarget(g, 20+a*30, 40); , and customize the for loop control to cover that set of
values.]