smalltalk - Creating a method that prints while emptying the contents of a Stack -
given class: 'astack'
object subclass: #astack instancevariablenames: 'elements' ...
it contains orderedcollection object holds it's 'element objects'
initialize super initialize. elements := orderedcollection new
it's fellow member classes force , pop, respectively:
push: anobject self elements addfirst: anobject pop ^self elements removefirst
i trying write print method uses timesrepeat print contents of stack empty simultaneously. calls kid class print method each 'element' (print ^self name) , outputs on screen using 'transcript'.
print self size timesrepeat: [transcript show: self pop print]
on other side have class called 'somerandomobject' who's print method is:
print transcript show:self getname; cr
workspace code:
| o1 o2 stk | o1 := somerandomobject new. o1 name: 'object1'. o2 := somerandomobject new. o2 name: 'object2'. stk := astack new. stk push: o1. stk push: o2. stk print "prints , emptys stack"
upon running error:
error: instances of astack not indexable
how can prepare code print method shows o1 , o2 name while popping them off stack?
i cannot tell before give me total (at to the lowest degree bigger) stack trace, expect, wont have #size
method defined in class should this:
size ^ elements size
smalltalk pharo
No comments:
Post a Comment