Question
Smalltalk (1 mark) Create a new project in Squeak called Matryoshka Project. Using the system browser, define a class category called Matryoshka. (1 mark) Define
Smalltalk
(1 mark) Create a new project in Squeak called Matryoshka Project. Using the system browser, define a class category called Matryoshka. (1 mark) Define a class in this category, called Doll, that has the following instance variables: size (the size of the Doll) nestedDoll (the Doll inside this Doll, or nil if empty)
(2 marks) Provide accessor methods for size and nestedDoll called size and nestedDoll, respectively. (2 marks) implement a mutator for size called size: that sets the Dolls size to the provided argument value if and only if size is currently nil (unset), otherwise it ignores the message. (2 marks) Define a constructor (a class method) newSize: that takes a single argument specifying the Dolls size and returns a new Doll object with that size. (2 marks) Provide a nest: method, which receives another Doll as an argument. If the Doll to nest is smaller in size than this Doll, set the nestedDoll instance variable to refer to the Doll to nest. Ignore whether or not nestedDoll is nil. (2 marks) Provide a method called unstack, which sends a message to nestedDoll telling it to unstack (if not nil), and removes the association with nestedDoll (by setting nestedDoll to nil).
(2 marks) Override the Object asString method such that it returns Doll of size X[, ] where X is the size of this Doll, and if nestedDoll is not nil it will append a comma, a space, and the result of calling nestedDoll: asString. Hint: the comma (,) binary message is used to concatenate strings.
(4 marks) Define a class method called nest: that takes a Set of Dolls and nests them according to size, returning a reference to the outermost Doll. If more than one Doll of a given size is encountered during this process, the method will select one to nest and discard the other. If any of the Dolls in the Set already contain other Dolls, simply ignore and replace them.
Hint: it may be useful to first sort the Set by Doll size before nesting the Dolls. The asSortedCollection: message can be sent to a Smalltalk collection (including a Set) in order to sort it by criteria that you include in a block as a message argument.
(2 marks): include scratch code (from the Workspace) that tests each of your methods. For Doll nest: the following scratch code must be included (you may include other test code as well)
someDolls := Set new.
aDoll := Doll newSize: 5.
someDolls add: aDoll. someDolls add: (Doll newSize: 3). someDolls add: (Doll newSize: 1). someDolls add: (Doll newSize: 2). someDolls add: (Doll newSize: 4). Doll nest: someDolls. Transcript show: aDoll; cr
This must output the following in the Transcript window: Doll of size 5, Doll of size 4, Doll of size 3, Doll of size 2, Doll of size 1
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started