Graphic by Keith Ohlfs

CS111, Wellesley College, Fall 1997

Lab #9
[CS111 Home Page] [Syllabus] [Students] [Lecture Notes] [Assignments] [Programs] [Documentation] [Software Installation] [FAQ] [CS Dept.] [CWIS]

CS111 Lab #9: Recursion and Iteration

You do not need to turn in this lab.

1. Turtles drawing polygons

Last week in lecture you were introduced to turtles, which could draw patterns to the applet window. In this lab you will program turtles to draw regular polygons. A regular polygon has sides of equal length and angles of equal length, as shown in the following images:

We will draw these polygons using:

To accomplish this, first download the Turtles folder from the CS111 download folder. Open the Project window. Drag PolygonWorld.java and PolygonWorld.html into the project window.

We will start by filling in the method: drawSides to draw a polygon using recursion. The polygon is defined by the number of sides and the length of a side. A turtle can draw a polygon by repeatedly drawing a side and then turning by an angle of (360.0/numSides), until it has drawn the correct number of sides.

Now we will write polygonWhile, and polygonFor--Two methods that will draw polygons using while and for loops, respectively.

2. Polygon Flowers

Now that you can draw a polygon, you can use this method to draw polygon flower. A polygon flower is defined by the number of petals and the number of sides of each petal. Each petal is a regular polygon, and the petals are rotated with respect to one another. The angle of rotation is equal to (360.0/numPetals). Some sample flowers are as follows:

As with the polygon, we will write methods to draw these flowers using tail recursion, while loops and for loops.

You do not need to turn in this lab.