CSE 341 -- Assignment 4 -- Smalltalk Warmup

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.

  1. Define a class Stack, as described in the lecture notes. Test your stack by opening a workspace, making a couple of instances of Stack, and sending each of them some push: and pop messages. Open an inspector on each stack so you can see its state.

    Also add a 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 message is sent, for example, to a stacks when you select a variable or expression in the workspace and then the "print it" operation.) Thus if we first push 3.14, then 'fred', and finally the point 2@3, then printOn: should append the following to the stream that is passed as an argument:

    stack[3.14,'fred',2@3] 
    

    You don't have to hand in anything for this question -- if you get stuck on the printOn: part, see the Solution for Assignment 4 for the Winter 1995 offering of CSE 341.

  2. Implement and test a subclass of Stack, called 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.

  3. Implement and test another version of Stack, say 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:).

  4. One of the homework problems for 341 in winter 1994 was to define a class Turtle and a subclass ColorTurtle. A sample solution for this assignment is on lynx/wolf/grizzly and mscc on ~borning/smalltalk/TurtleGraphics.st. Copy this file to your directory on the PC's, load it into Smalltalk, and try out the drawings.

    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 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:

    With turtle geometry, it's easy to define a method such as drawSquare:

    drawSquare: length self home. 4 timesRepeat: [self go: length. self turn: 90] A squiral design can be defined as: squiral: delta angle: angle | distance | distance := delta. self home. 200 timesRepeat: [self go: distance. self turn: angle. distance := distance+delta]. Define a more general message polygon: nSides length: length which draws a regular polygon with 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:

    Smalltalk at: #W put: ScheduledWindow new. W open. Smalltalk at: #Yertle put: ColorTurtle new. Yertle window: W. Yertle color: ColorValue blue. Yertle home. Yertle squiral: 2 angle: 122. Yertle color: ColorValue red. Yertle home. Yertle squiral: 2 angle: -122. Try some other distances and angles as well, for example, squiral: 3 angle: 170; squiral: 3 angle: -170 squiral: 2 angle: 177; squiral: 2 angle: -177 squiral: 3 angle: 89; squiral: 2 angle: 89 Another interesting effect can be achieved by having two turtles drawing the squirals at the same time, for example: Smalltalk at: #W put: ScheduledWindow new. W open. Smalltalk at: #Yertle put: ColorTurtle new. Yertle window: W. Yertle color: ColorValue blue. Smalltalk at: #Myrtle put: ColorTurtle new. Myrtle window: W. Myrtle color: ColorValue red. | distance | distance := 1. Yertle home. Myrtle home. 200 timesRepeat: [Yertle go: distance. Yertle turn: 89 Myrtle go: distance. Myrtle turn: -89. distance := distance+2]. Note that this last example illustrates that each instance of Turtle has its own state.

    END OF OLD ASSIGNMENT

    For this 341 class (Autumn 1995), augment the class Turtle with a new method 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.)