April 1, 1998
Due in quiz sections April 9, 1998
To hand in your work for this assignment for Questions 3, 4, and 5, file out the class or methods that you define, and print out the file. Also, for Questions 4 and 5, print out the graphics window showing the graphical results of your code. You don't need to hand in anything for Questions 1 and 2 -- these are just small exercises to get you started with the basic language concepts. If you get stuck, see the Solution for Assignment 4 for the Autumn 1995 offering of CSE 341.
push:
and pop
messages.
Open an inspector on each stack so you can see its state.
Also add a
A couple of useful messages to know about are // (integer division) and
You don't need to worry about efficiency in your
There is a quick-and-dirty way of doing graphics examples in Smalltalk
Express: send the message
For example, the following code opens a graphics window and draws a blue
triangle:
A squiral is the Logo name for a pattern produced by telling the
turtle to go (for example) a distance 1, then turning 89 degrees, going 2,
turning 89 degrees, going 3, turning 89 degrees, going 4, etc.
Define a method
Test your squiral method with some different values for nSteps and d.
For example, here is the result of using nSteps=200 and d=89:
If you want to play with this a bit, some other useful messages to pens are
as follows:
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]
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:
).
isPrime
for the class Integer. This
should return true if the receiver is a prime, and false if not. Also
return false if the receiver is less than 2. Thus
5 prime
returns true
8 prime
returns false
0 prime
returns false
rem:
(remainder). These are both understood by integers.
isPrime
method; doing something simple and obvious is fine. If you do want to know
about more sophisticated tests for primality, see Primality
Test.
go: dist
)
or to turn its orientation by a specified number of degrees
(turn: degrees
). Browse to the class Pen and poke around and
see what kinds of messages it understands.
turtleWindow: title
to the class
Window. This pops up a new window with the given title, and binds the
global variable Turtle
to the pen associated with that window.
You can then send messages to Turtle. This isn't the way to do
high-quality interfaces -- first, it's not great style to have a single
global variable Turtle, and second, the window doesn't remember its
contents so that it doesn't refresh or resize properly. (Naturally there
is a way to do this right, but it's more complicated, and we won't worry
about it for now.)
squiral: nSteps degrees: d
for Pen that draws a
squiral in which the pen goes a distance of 1, then 2, then 3, etc, each
time turning d degrees. It should repeat this nSteps
times.
north
set the pen's direction to straight up
setLineWidth:
set the line width
home
go to the center of the window
backColor:
set the background color (some useful color
constants are ClrWhite, ClrBlack, ClrBlue, ClrGreen, and ClrBlue)
foreColor:
set the color the pen uses to draw
erase
clear the window to the current background color