my tiny method is broken......
Rik Fischer Smoody
riks at smOO.com
Wed Jun 20 18:49:58 UTC 2001
Well, the method is OK, but misplaced.
It should be a class-side method.
Remember that creating new instances is the bailiwick of classes.
Any particular Stack does not know how to create new ones.
At 7:22 PM +0100 6/20/01, John Hinsley wrote:
>I'd be grateful if someone could take a peek at this if they have a
>moment to spare.
>
>I'm trying to put all the "special" methods from M206 (which natively
>use Learning Works)
>into Squeak so that, ultimately, students could choose to use whatever
>Smalltalk (and whatever platform) they chose.
>
>As usual, I fell flat on my face at the first hurdle!
>
>Here's the brief (note: this isn't a marked exercise!):
>
>"In the class browser create a new class called Stack (as a subclass of
>Object) having an instance variable called elements."
>
>Here are the suggested tests:
>
>myStack := Stack new.
>
>myStack isEmpty.
>
>myStack push:1.
>
>myStack isEmpty
>
>myStack push:2
>
>"Check 2 is popped from the stack."
>
> myStack pop
>
> "Check 1 is popped from the stack."
>
> myStack pop
>
> "Check an exception results from popping an empty stack."
>
> myStack pop
>
>Now, according to the brief, inspecting elements within myStack should
>display an empty OrderedCollection. It doesn't in Squeak (or at least,
>not in my implementation). I wonder if the " ^super new initialize "
>method was creating problems?
>
>Alternatively, it could be that I'm not using the Browser to create new
>classes and methods correctly: the only documentation I seem to have is
>in Spanish!
>
>Cheers
>
>John
>
>--
>******************************************************************************
>Marx: "Why do Anarchists only drink herbal tea?"
>Proudhon: "Because all proper tea is theft."
>******************************************************************************
>Object subclass: #Stack
> instanceVariableNames: 'elements '
> classVariableNames: ''
> poolDictionaries: ''
> category: 'M206'!
>
>!Stack methodsFor: 'accessing' stamp: 'jh 6/20/2001 18:41'!
>pop
> "Remove and answer with the first element of elements."
>
> ^elements removeFirst
>! !
>
>!Stack methodsFor: 'accessing' stamp: 'jh 6/20/2001 18:41'!
>push: anObject
> "Add anObject to the front of elements and return the updated receiver."
>
> elements addFirst: anObject
>! !
>
>
>!Stack methodsFor: 'testing' stamp: 'jh 6/20/2001 18:39'!
>isEmpty
> "Is elements empty?"
>
> ^elements isEmpty
>! !
>
>
>!Stack methodsFor: 'creation' stamp: 'jh 6/20/2001 18:37'!
>initialize
> "Initialise the elements to an empty OrderedCollection instance."
>
> elements := OrderedCollection new
>
>! !
>
>!Stack methodsFor: 'creation' stamp: 'jh 6/20/2001 18:34'!
>new
> "Answer with an initialised instance of the receiver."
>
> ^super new initialize
>! !
Rik Fischer SmOOdy riks at smoo.com
smOOdynamics - Systems Made Outa Objects
503-249-8300 ph/fx
2400 NE 25th, Suite 800
Portland, OR 97212
---
Don't worry that people will steal your ideas. . .
Good ideas have to be crammed down peoples' throats. . .
and even then, they might not sink in.
More information about the Squeak-dev
mailing list
|