Loopy Shapes       CPS 004.1, 11 July 2003

The key to writing a loop is to determine what one thing needs to be done repeatedly. Then you need to figure out how to factor out the differences in what needs to be repeated into some calculation, usually based on the current iteration of the loop.

This exercise is meant to familiarize you with the following concepts discussed in class and your reading:

Specifications

Complete the following exercises:
Target. Make a class that represents a target with a given number of circles, each one smaller than the previous one. For example, if the target had three circles, the first should be the full size, the next two-thirds that size, and the last one, one-third that size. Similarly, if the target has five circles, the first should be full size, the next four-fifths that size, the next three-fifths, then two-fifths, and finally one-fifth the size. You should also create an Applet class within which you create an instance of your Target class.

Extra credit. Make your target able to appear in any position and at any size, regardless of the number of rings in the target.

Grid. Make a class that represents a square grid, like graph paper, with a given distance between the lines. For example, the image on the left was created with a distance of twenty pixels between the lines, meaning that nineteen horizontal lines and nineteen vertical lines are drawn, each twenty pixels apart. If the the distance given was two hundred pixels, then only one of each line would be drawn - making it look like the Cartesian axes. You should also create an Applet class within which you create an instance of your Grid class.

Extra credit. Make a class that represents polar graph paper, with lines radiating at a given interval from the center of the applet.

Checkerboard. Make a class that represents a checkerboard with a given number of squares that alternate between two colors, like red and blue, arranged in the same number of rows and columns. For example, a standard checkerboard has sixty-four squares arranged in eight rows of eight squares each. A tic-tac-toe board has nine squares arranged in three rows of three squares each. You should also create an Applet class within which you create an instance of your CheckerBoard class.

Extra credit. Make a class that represents a grid based on a shape other than a square, e.g., a hexagon or triangle.

To make your code easier to understand and change, you should separate the work of your loops into a separate function so that all of your loops are based on a similar template. For an example, see the last section of the loops tutorial.

What to Submit

You should start a new project for each shape in this exercise. You should feel free to copy your code from previous exercises, but you will be making many changes.

When you are finished, you and your partner should transfer the entire project folder created by Visual J++ to your respective public_html/cps4 folder in the acpub system and update your course web page to link to the newly transferred project using the guidelines given here.