View this PageEdit this Page (locked)Attachments to this PageHistory of this PageHomeRecent ChangesSearch the SwikiHelp Guide
Hotspots: Admin Pages | Turn-in Site |
Current Links: Cases Final Project Summer 2007

spring 2004 midterm - 3: randy

3. Know Your Box

(a)Box defines a class method new as ^super new initialize. Explain what happens whe joe:= Box new is executed - what messages are sent, who receives each message and what happens in the method executed in response to the message?

(b)If you do jane:= NamedBox new. jane named: 'Jane'. jane draw. in the Box world, you get the picture at the right. Why does it look so different from jane:=NamedBox named:'jane'?

The key to this problem is to realize that NamedBox inherits from Box and has no draw of its own.

jane:=NamedBox new. This creates a new NamedBox. Since we call super (box) new in NamedBox new and Box's new calls draw, which we have overloaded in NamedBox, a box is drawn with name:= Unnamed, the default.

jane named:='Jane'. sets the name variable to 'Jane'.

jane draw. then calls the NamedBox instance method draw again. This overlays another box with the name 'Jane' on top of the box that was already drawn.

jane:=NamedBox named:'jane' This class method calls super new. This draws a box with name = 'Unnamed'. The named: method then continues to Undraw the just drawn box. It then sets the name = 'Jane' and calls the draw method again.

Simce named: undraws the unnamed box and the first code does not, that is why they look different

(c) What are the key differences between a class method and an instance method?

The main thig we were looking for here was that a Class method can only manipulate class variables. An Instance method can manipulate both class variables and instance variables.

Any other information that is true would gain you partial credit, except for saying that a Class method is a class and a Instance method is a method, which is vacuously true.

Link to this Page