CSE 341 - Spring 2002 - Assignment 5 - Smalltalk Warmup

Due May 6, at the beginning of class. Please submit your homework both electronically and on paper. For the electronic submission, you'll want two separate files, one for Question 1 and one for Question 2, as described below.

  1. Define a class Stack, as described in the lecture notes. To make it easy to hand in the answer to this question, define a new class category named Stacks in a browser, and define your class Stack in that category.

    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.

    Once you have the stack working, add two more methods:

  2. Now put some test cases in a more permanent place than a workspace, by defining a method test for Stack that runs some suitable tests, printing the results to the transcript.

    Now define a subclass of your Stack, called PoppingStack, that has just one method, named popAndDo:. This method should take a block as an argument, and should pop each element off of the stack and call the block with that element, until the stack is empty. For example, suppose we have a stack s containing 3.14 and true. Then evaluating

    s popAndDo: [:e | Transcript show: 'popping '; show: e; cr]
    
    should print
    popping true
    popping 3.14
    
    The stack should be empty after sending the popAndDo: message.

    Define another test method for PoppingStack that demonstrates that it works correctly as well.

    To create the file for the electronic submission for your answer to this question, select the class category Stacks in a browser and then pick 'fileOut' from the right-button menu. Then for the paper version, pick 'printOut'. (The 'printOut' option creates an html file which has nicer formatting - unfortunately it isn't set up to file in correctly however.)

  3. Browse to the spiral:angle: method in the Pen class, and try executing the code in the comment:
    Display restoreAfter: [
    Display fillWhite. Pen new spiral: 200 angle: 89; home; spiral: 200 angle: -89].
    
    This version of spiral is arguably a bit over the top with the multiple colors. Define a new method category in Pen named 341-spirals In this category, define a new method simpleSpiral:angle: in Pen that just draws with the current color of the Pen (which defaults to black).

    Using simpleSpiral:angle:, define a new method manySpirals, also in the 341-spirals category, that displays 10 spirals at random places on the screen. For best effect, pick some nice angles, sizes, and colors.

    Hints: 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. To find the width and height of the Squeak window, use Display width and Display height.

    To create the file to submit for your answer to this question, select the method category 341-spirals in a browser and pick 'fileOut' from the right-button menu, as for the first question. Then pick 'printOut' for the printed version.

  4. Crash Squeak in some interesting way by modifying or deleting an essential class that comes with the basic system. For your answer, write down in English what you did, and describe the effect. Don't save the resulting image. If you don't save the image, you won't damage your your copy of Squeak by doing this! In any case you won't damage your computer. (This is an easy question. The purpose is to demonstrate that all parts of the system are changeable by the user, and that this power has its drawbacks. However, in previous years 341 students have sometimes been very nervous answering it. Be bold!) Just submit your answer on paper for this question.