Skip to main content
ICT
Lesson A12 - Iterations
 
Main Previous Next
Title Page >  
Summary >  
Lesson A1 >  
Lesson A2 >  
Lesson A3 >  
Lesson A4 >  
Lesson A5 >  
Lesson A6 >  
Lesson A7 >  
Lesson A8 >  
Lesson A9 >  
Lesson A10 >  
Lesson A11 >  
Lesson A12 >  
Lesson A13 >  
Lesson A14 >  
Lesson A15 >  
Lesson A16 >  
Lesson A17 >  
Lesson A18 >  
Lesson A19 >  
Lesson A20 >  
Lesson A21 >  
Lesson A22 >  
Lesson AB23 >  
Lesson AB24 >  
Lesson AB25 >  
Lesson AB26 >  
Lesson AB27 >  
Lesson AB28 >  
Lesson AB29 >  
Lesson AB30 >  
Lesson AB31 >  
Lesson AB32 >  
Lesson AB33 >  
Vocabulary >  
 

LAB ASSIGNMENT A12.6 page 17 of 18

ParallelLines

Background:

In this lab, you will practice using nested for loops to recreate this image using the DrawingTool class:

To do this efficiently, there are a few suggestions to consider. First, there are eight rows each containing seven filled boxes. This suggests a nested for loop, like this:

for (int row = 0; row < 8; row++){
  // calculate the start of the row of squares

  // calculate and add a horizontal offset

  for (int col = 0; col < 7; col++){
  // draw the square
  // calculate and position for the next square
  }

  // calculate the location and draw the line
}

Calculating the square position will take some work. You might want to make a quick sketch, with coordinates, to save frustration. Hint: there will be a negative x offset and a positive y offset to have the image start in the upper left corner as shown.

The DrawingTool class includes a method for drawing filled rectangles:

fillRect(double width, double height)

For instance, with a DrawingTool called pen, the call pen.fillRect(40, 40) would create a 40x40 filled square centered about the current drawing position.

Assignment:

  1. Using the information given in the Background section above, write a class using nested for loops to display the image shown above.

 

Main Previous Next
Contact
 © ICT 2006, All Rights Reserved.