Oct 21, 1994
Due: October 28, 1994
Binary trees should understand the following messages:
isEmpty
includes: item
add: item
remove: item
preorderDo: aBlock
inorderDo: aBlock
postorderDo: aBlock
printOn: aStream
"the standard print message"
The messages preorderDo:
,
inorderDo:
, and
postorderDo:
define iterations of various kinds. Each takes a
Block as an argument.
For example, evaluating
The messages isEmpty
and
includes: item
should return true or false; the return values
for the others isn't significant.
There are several ways to implement this. One suggestion is to define a
class BinaryTree
as a subclass of Object.
BinaryTree
has a single instance variable
nodeOrNil
. This refers either to an instance of
BinaryNode
or to nil. Binary nodes have three instance
variables: left
, right
, and value
.
Take it from there ...
Collection
, defining only those messages that
are needed to define binary trees. Which messages are no longer needed?
What other functionality do you gain? (Hints: to find out which message
you need to redefine, search for senders of the message
subclassResponsibility
in Collection
. You will
need to decide what do:
means for a binary tree, and will need
to define a slightly different version of remove:
)
A turtle has a current location and a direction in which it is heading; it understands commands to go a certain distance or to change its heading. The beauty of this is that the turtle has its own coordinate system; children can draw geometric shapes using intuitive commands, without needing to know trigonometry.
Recently, the "Save the Turtle League" (headed by Sebastian Q. Kay, Alan
Kay's lesser-known younger brother), has been campaigning to restore the
Turtle to its rightful place in the system, and has hired you as the chief
programmer. Your mission is to define and test a class Turtle. Every
turtle should have a window
in which it lives, a
location
, a direction
(the direction in which it
is pointing), and a flag penDown
indicating whether a line is
to be drawn or not as the turtle moves. These, then, will be instance
variables. The turtle should understand a number of messages, including:
go: dist
turn: degrees
home
window: w
w
, and initialize its state (using self home
).
With turtle geometry, it's easy to define a method such as
drawSquare:
nSides
sides, each side
being of length length
.
Test your turtle on a number of examples, including polygons and squirals.
Define a subclass of Turtle
, ColorTurtle
, that
can draw lines of different colors and different widths.
ColorTurtle
should understand color:
and
width:
messages to set its color and width.
window:
should be redefined to initialize the color to black,
and the width to 1. (Naturally, as a Smalltalk programmer who values good
style, in the window:
method in ColorTurtle
, you
will use super
to invoke the window:
method
inherited from Turtle
, rather than duplicating the code for
the parts of the initialization already handled by the method in
Turtle
.) Test your ColorTurtle
on a number of
examples. For example, the following will draw a nice pair of squirals on
top of each other:
Turtle
has its own state.