![]() |
Lab 7
|
Below are the declarations for two classes: a
ThrowerWorld
class that is a subclass of
BuggleWorld
and a ThrowerBuggle
class that
is a subclass of Buggle
.
// a BuggleWorld in which Buggles throw Bagels public class ThrowerWorld extends BuggleWorld { public void run () { ThrowerBuggle tara = new ThrowerBuggle(); tara.throwBagel(3); } } class ThrowerBuggle extends Buggle { public void throwBagel (int distance) { if (distance == 0) { dropBagel(); } else { forward(); throwBagel(distance - 1); backward(); } } }
Task 1a) Draw the result (in a ThrowerWorld
grid) of asking tara
to throw bagels at distances of 0,
1, 2, and 3. Use a different grid for each distance. The size
of the grid doesn't matter (just make it large enough so the buggle
doesn't run into walls). When given recursive code, it is usually a
good idea to draw on paper a couple of instances of running the code
beginning with the easiest case and working up.
Task 1b) Draw a Java Execution Model diagram that shows all
of the execution frames created by invoking the run()
method on an instance of the ThrowerWorld
class. 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.
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. (Of course, you may enjoy reviewing the turtle contract after you finish.)
For the tasks below, you will test your work by specifying a number of levels and some size (and perhaps an offset) in a 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 a size. If your program hangs, you may need to "force quit" it by depressing the option, apple, and escape keys all at the same time. If that doesn't help, restart the computer by pressing the little button with the triangle on the machine itself (under your desk).
System.out.println()
statements. These statements can help
you understand how your program works, and can be a big help in
debugging when your program doesn't work. Each time the method is
invoked, a statement is printed to the console window. Here
is how you can open the console window to see the printed statements
from your program:
Download the lab7_programs
folder from the
cs111d
account on cs.wellesley.edu
.
Most of the programming problems for this lab take place in
extensions of TurtleWorld
. The problems are arranged in
order of difficulty with the easiest problem first. The last problem
is a BuggleWorld recursion problem. Note that the exercises are
challenging, so don't be disappointed if you don't get to the last
ones.
You can load working examples from lab7_programs/test
.