The student has probably observed that there are certain tasks that are
repetitive in nature. Java, like all other programming languages, has
constructs to deal with repetitive tasks. This activity will deal with for
loops. Here's an example:
for(int x=0; x < 10; x++){ g.drawString(""+(x+1), 10, 20+x*20); }This example prints out the numbers one through ten. Here's how it works:
EXAMPLE: The student will create an applet like the one shown here. This activity provides the student with practice using for loops. |
x |
ASSIGNMENT:
The student may need some assistance on this activity. Here are the two
major steps:
Step One: Create a method to draw a row of filled rectangles:
public void rowOfRects(Graphics g, int y){ for(int x = 10; x < 190; x+=15){ g.fillRect(x,y,10,10); } }
for(int y=10; y < 190; y+=15){ rowOfRects(g, y); }