Java Reference
In-Depth Information
SELF-REVIEW QUESTIONS
(see answers in Appendix N)
SR 6.12
When would we use a
for
loop instead of a
while
loop?
SR 6.13
What output is produced by the following code fragment?
int
value = 0;
for
(
int
num = 10; num <= 40; num += 10)
{
value = value + num;
}
System.out.println (value);
SR 6.14
What output is produced by the following code fragment?
int
value = 0;
for
(
int
num = 10; num < 40; num += 10)
{
value = value + num;
}
System.out.println (value);
SR 6.15
What output is produced by the following code fragment?
int
value = 6;
for
(
int
num = 1; num <= value; num ++)
{
for
(
int
i = 1; i <= (value — num); i++)
System.out.print (" ");
for
(
int
i = 1; i <= ((2 * num) — 1); i++)
System.out.print ("*");
System.out.println ();
}
SR 6.16
Assume
die
is a
Die
object (as defined in Section 4.2). Write a code frag-
ment that will roll
die
100 times and output the average value rolled.
Conditionals and loops greatly enhance our ability to generate interesting graphics.
The
Bullseye
program shown in Listing 6.5 draws a target. The drawing actu-
ally occurs in the
BullseyePanel
class, shown in Listing 6.6. The
paintComponent
of the
BullseyePanel
class uses an
if
statement to alternate the colors between
black and white.
Note that each ring is actually drawn as a filled circle (an oval of equal width
and length). Because we draw the circles on top of each other, the inner circles
Search WWH ::
Custom Search