CSE 341 -- Assignment 1 -- Smalltalk Warmup

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.

  1. (Don't hand in the solution to this question.) 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:


  2. (Don't hand in the solution to this question.) 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:).

  3. Define a method 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

    A couple of useful messages to know about are // (integer division) and rem: (remainder). These are both understood by integers.

    You don't need to worry about efficiency in your isPrime method; doing something simple and obvious is fine. If you do want to know about more sophisticated tests for primality, see Primality Test.

  4. One of the basic classes for doing graphics in Smalltalk Express is class Pen. Pens can draw lines, circles, etc. They also can act like the turtle in the Logo language, so that a pen has a current direction and state (up or down). Using the turtle model of graphics, one can send messages to a pen asking it to go a certain distance (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.

    There is a quick-and-dirty way of doing graphics examples in Smalltalk Express: send the message 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.)

    For example, the following code opens a graphics window and draws a blue triangle:

    Window turtleWindow: '341 homework'. Turtle foreColor: ClrBlue. 3 timesRepeat: [Turtle go: 100. Turtle turn: 120]

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

    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:

    squiral image

    If you want to play with this a bit, some other useful messages to pens are as follows:

  5. Write a method for pens that draws a sawtooth pattern -- but make the saw's tooth higher if the number of that tooth is a prime. Thus:

    sawtooth image
    (There are some handdrawn numbers on the printed handout to show a few teeth corresponding to primes and composites.)