October 23, 1995
Due in quiz sections Nov 2, 1995
(postponed from Oct 31 due to PC lab being down)
To hand in your work for this assignment, file out the class or methods that you define, and print out the file. Also, to show your code being run, open a file editor (rather than a workspace), and keep a record of the results that are printed. Save the file and hand it in along with the listing of the classes.
push: and pop messages.
Open an inspector on each stack so you can see its state.
Also add a
You don't have to hand in anything for this question -- if you get stuck on
the
The old assignment was approximately as follows. Enthusiasts may want to
actually do this assignment rather than copying the code, but it's fine to
just copy the code. However, you should read and understand the old code.
OLD ASSIGNMENT STARTS HERE
The Logo programming language was designed for children, and was an
important influence on the early Smalltalk design. Logo includes a
Turtle. Sometimes, the turtle is a mechanical computer-controlled
device that rolls around on the floor, drawing a line as it moves;
sometimes it is a simulated turtle on the display. Early versions of
Smalltalk included a class Turtle, and even more recent versions had a
class Pen that was much the same. In the most recent versions of
Smalltalk-80, however, the Turtle has become extinct.
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
With turtle geometry, it's easy to define a method such as
Test your turtle on a number of examples, including polygons and squirals.
Define a subclass of
END OF OLD ASSIGNMENT
For this 341 class (Autumn 1995), augment the class Turtle with a new
method
printOn: method to your class stack, so that it
prints something more useful than a Stack. (This is the
standard method for producing a printed representation of an object. The
printOn: should append the following to the stream
that is passed as an argument:
stack[3.14,'fred',2@3]
printOn: part, see the
Solution for Assignment 4
for the Winter 1995 offering of CSE 341.
EndlessStack, that never runs out of items to pop -- if there
aren't any more real items, it returns a default item. (Store the default
item in an instance variable, which should be set by an initialization
message.) EndlessStack should use the inherited versions of
push: and printOn: -- you should not need to
redefine these.
Stack subclass: #EndlessStack
instanceVariableNames: 'default '
classVariableNames: ''
poolDictionaries: ''
category: 'Stacks'!
pop
"if the stack is empty return the default item,
otherwise return what the super class would"
^self isEmpty ifTrue: [default] ifFalse: [super pop]! !
setDefault: item
"initialize the default item to be item"
default := item.! !
LStack,
that uses a linked list to store the elements of the stack rather than an
array. It should have the same protocol as Stack (except for
setSize:).
Object subclass: #LStack
instanceVariableNames: 'top '
classVariableNames: ''
poolDictionaries: ''
category: 'Stacks'!
isEmpty
"the stack is empty if the top is nil"
^top = nil!
pop
"pop an item off the top of the stack. generate an
error if the stack is empty."
self isEmpty
ifTrue: [self error: 'trying to pop an empty stack']
ifFalse: [
| item |
item := top first.
top := top rest.
^item
].!
printHelp: strm list: aList
"if the list is nil, we are done. Otherwise
print the rest, then print the first. This is
how we achieve the same order as the
other stack"
aList = nil
ifFalse: [
self printHelp: strm list: aList rest.
strm space.
aList first printOn: strm.
]!
printOn: strm
"prints with the top to the right. "
strm nextPutAll: 'stack('.
self printHelp: strm list: top.
strm nextPutAll: ')'!
push: item
" create a new ConsCell with the first being the item
we are pushing and the rest being the old top of the
stack. Make this cell the new top."
top := (ConsCell new) cons: item onto: top! !
Object subclass: #ConsCell
instanceVariableNames: 'first rest '
classVariableNames: ''
poolDictionaries: ''
category: 'Stacks'!
cons: item1 onto: item2
"initialize a cons cell to be the result of consing
item1 onto item2"
first := item1.
rest := item2.!
first
"return the first of the list"
^first!
rest
"return the rest of the list"
^rest! !
~borning/smalltalk/TurtleGraphics.st. Copy this file to your
directory on the PC's, load it into Smalltalk, and try out the drawings.
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
Move the turtle the specified distance in its
current direction. If the pen is down, draw a line in the window.
turn: degrees
Change the direction by the number of degrees,
but don't move yet.
home
Go home: set the location to the center of the window,
direction pointing straight up, pen down.
window: w
Tell the turtle to live in window
w, and initialize its state (using self home).
penUp
Tell the turtle to raise its pen.
penDown
Tell the turtle to lower its pen.
drawSquare:
nSides sides, each side
being of length length.
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.
manySquirals that displays 10 squirals at random places
on the screen. For best effect, pick some nice angles, sizes, and colors.
(To generate a random number, make a new generator by evaluating
Random new. Each time you send next to the
generator, you get a new random number between 0 and 1.)
manySquirals
| generator colors numColors|
generator := Random new.
colors := ColorValue constantNames.
numColors := colors size.
"generate 10 random squirals"
1 to: 10 do: [ : i |
"put the squiral somewhere in the window"
self penUp.
self goto:
(Point x: (generator next * window width)
y: (generator next * window height)).
self penDown.
"pick a random color"
self color: (ColorValue perform: (colors at:
(generator next * numColors + 1) asInteger )).
"pick random length and angle"
self squiral: (generator next * 3.0 + 1.0)
angle: (generator next * 360.0 - 180.0) .
]! !