Objective: To gain experience designing recursive methods.
Due date is the beginning of your lab section next week:
Lab prep: Review the slides from Lectures 20 and 21, and the examples from Lecture 21.
Note: Exercises 2-5 use Turtle graphics, as discussed in
Class 21.
Here is a contract for the Turtle Class.
Create a hw08 directory in your cs101 directory, and save
Turtle.class and
TurtleWorld.class
to it.
A SquareTurtle is a kind of Turtle that can draw squares as well as staircases made up of squares. Before making any changes, compile and run SquareWorld.java (it will automatically compile SquareTurtle.java, too). Click on the "run" button, and notice what the turtle draws. Now modify the drawHalfSquare method in SquareTurtle.java so that a SquareTurtle correctly draws a square (by drawing two half squares). Then modify the drawStaircase method so that it recursively draws pictures such as the one to the right. Employ the following strategy: (1) draw a square, (2) pick up the pen and move to a new location, (3) put down the pen, and then (4) recursively draw the rest of the squares. Turn in a printout of your final SquareTurtle.java file, as well as a screen snapshot showing that your code works correctly. You can create a snapshot of part of the screen with Apple-Shift-4.
The pictures below depict sierpinski(sideLength, levels) for levels = 1 through 5.
|
|
|
|
|
|
Back to Computer
Science 101 Home
Department of Computer
Science