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.
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:
Stack with 3.14, 'fred', 2@3If the stack is empty, print
empty stack
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.14The 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.)
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.