![]() |
Problem Set 5 Due on Wednesday October 24 at the start of lab |
ps05_programs
folder in the cs111 download directory on the cs
server.
At the bottom of this page, we have included a link to a working example of a solution to Task 2. It is a good idea to run this program to get a feel for what we are asking you to do. Your solution should provide the exact same results as our solution (except as noted in the individual problem descriptions below).
With the introduction of conditionals and recursion, programming becomes much more challenging than on your first few assignments. It is a good idea to think carefully about how to solve your problems before you sit down at a computer and start writing code. It has been our experience that attempting to solve these sorts of problems while sitting at the computer is a recipe for disaster, because it can easily turn into a frustrating trial-and-error nightmare. Instead, we suggest that you form study groups and talk about solution strategies with your classmates before attempting to write any code. Once you have settled upon a strategy, we suggest that you write your Java code first using pencil and paper and simulate it in your head to check for its correctness. When you are prepared to type in code, remember to enter and debug your program in stages, that is, first type in just the simplest piece of your solution and get that to work before going on to the next piece. Use stubs for methods that you need to refer to but whose implementation you want to defer. Of course, while talking with classmates about solution strategies is encouraged, you should never look at another classmate's code nor share your code with someone else.
Notes, hints, and suggestions for Task 2 are given on a separate page. We want you to have freedom to think about the problem in your own way. Reading the hints page is not required. It's main purpose is to help you think about the problem if you don't know how to get started.
SierpinskiWorld.java
file from Task 2.
Save your SierpinskiWorld.java
solutions
in the
ps05_programs
folder. Submit the entire
ps05_programs
folder to your drop folder on the cs111
server.
The following recursive method is defined for the Recurser
subclass of Buggle
:
public void spiral (int n) { if (n > 0) { forward(n); left(); spiral(n - 1); right(); backward(n); } }
Consider the following run()
method in the
RecursionWorld
subclass of BuggleWorld
:
public void run () { Recurser rita = new Recurser(); rita.spiral(3); }
For this task you are to draw a Java Execution Model
diagram that models the execution of run()
invoked on an instance (call it RW) of
RecursionWorld
with a 5×5 grid.
Your JEM diagram should consist of three parts:
run()
method on RW. Your diagram should depict the point in
time when the invocation of run()
returns. Although
Java can discard an execution frame when control returns from
it, you should not discard any frames when drawing your diagram.
RecursionWorld
object along with any other objects
created or manipulated as a result of the exectution of the
run()
method. You may abbreviate references to
instances of the Location
, Direction
,
and Color
classes as ovals surrounding appropriate
identifying information. Your drawing should show the state of
Object Land after the completion of the execution of the
run()
method.
run()
method.
Let sierpinski(levels, side)
be the
notation for a Sierpinski gasket with levels
levels and side length side
. Then a
recursive mathematical definition of sierpinski()
is:
sierpinski(0, side)
is an empty picture
with no lines.sierpinski(1, side)
is an equilateral
triangle with sides of length side
.sierpinski(levels, side)
for
levels
> 1 is three copies of
sierpinski(levels - 1,
side/2.0)
arranged in the corners of an
equilateral triangle
with side length side
.sierpinski(levels,
side)
for levels
= 0 through
5. These are approximations to the true self-similar
Sierpinksi gasket, which
is the limit of sierpinski(levels, side)
as levels
approaches infinity.
sierpinski(0,100)
|
sierpinski(1,100)
|
sierpinski(2,100)
|
sierpinski(3,100)
|
sierpinski(4,100)
|
sierpinski(5,100)
|
In this problem you will define a Java class with a method that
uses a turtle to draw Sierpinski's gasket. To begin this problem,
look in the SierpinskiWorld
folder from the
ps05_programs
folder. The file
SierpinskiWorld.java
contains a subclass of
TurtleWorld
called SierpinskiWorld
. The
run()
method it defines uses a
SierpinskiMaker
to draw a gasket.
The SierpinskiMaker
is positioned
toward the lower left hand corner of the screen facing east.
A gasket whose lower left-hand corner is positioned at this starting point
will be automatically centered in the TurtleWorld window.
You must add the definition of the SierpinskiMaker
class
to this file. SierpinskiMaker
is a subclass of
Turtle
that defines a void
method called
sierpinski()
that draws Sierpinski's gasket given two
parameters:
levels
and
side
.
sierpinski()
method so that it draws the
specified Sierpinski gasket and maintains the turtle's position,
heading, and color as invariants. Your method should use recursion.
You may define any auxiliary methods that you deem helpful.
Recall that the Turtle drawing primitives include the following:
Additionally, there are also versions ofpublic void fd (double n) Move the turtle forward n steps. public void bd (double n) Move the turtle backward n steps. public void lt (double angle) Turn the turtle to the left angle degrees. public void rt (double angle) Turn the turtle to the right angle degrees. public void pu () Raise the turtle's pen up. public void pd () Lower the turtle's pen down.
fd()
,
bd()
, lt()
, and rt()
that take int
parameters, so you can invoke these methods with either an integer or double
floating-point value.
You should not need to use any other Turtle primitives other than those listed above. In fact, many solutions use only a subset of the primitives listed above.
Test your definition by specifying levels and side in the parameter window and then clicking on the Run button in the TurtleWorld window. The Reset button will clear the screen. Good parameter values are in the ranges [0 ... 8] for levels and [100 ... 400] for side.
To experiment with a working version of a solution to this problem, run the applet SierpinskiWorld in a web browser. (Note: a "Sierpinski Parameters" window will appear at the top of your screen, but if you don't see it, it may be located behind your web browser window, so you should move your web browser window.) Your solution should produce the same pictures as this sample solution applet. It's worth noting that there are many possible ways to correctly decompose the problem, and the sample solution applet uses only one possible approach. Thus, while you must produce the same designs as does the sample solution applet, you need not produce the designs in the same way as the sample solution applet.