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

Brian Williams

Coweb Assignment: (Second assignment at bottom)

Tracing Code (1 point)
The following code solves the rainfall problem, which you may have seen in previous CS classes. For each line, describe what the Smalltalk code does. Be as specific as possible. In particular, what is data at the various points in the code?

| data onlyPositiveNumbers |
Specifies two instance variables, data and onlyPositiveNumbers.

data := OrderedCollection withAll: #(1 2 3 -4 -5 'error' 6 -7 999 2).
Creates an ordered collection with those components and sets 'data' to that.

onlyPositiveNumbers := [:i | (i isKindOf: Number) and: [i positive]].
Sets onlyPositiveNumbers to be an executable codeblock which returns true if i is a positive number in some set

data := data select: onlyPositiveNumbers.
This line applies the onlyPositiveNumbers codeblock to data. Data now has only values which returned true.

data := data copyUpTo: 999. "not including"
This sets data to the set returned by 'data copyUpTo: 999' – a new collection with only {1 2 3 6} in it.

Transcript show: data average
The transcript prints the average value of data, in this case 3.

Message Passing (1 point)

What is message passing and how does it fit into the way you program in Smalltalk? How does message passing work together with the class-based inheritance? What are some of the advantages and disadvantages of method passing?

Message passing is the giving of an object some message, which may cause the object to alter its state or return a value. In Smalltalk, it's the way all computation is performed. Objects have a set of messages they receive–in the event a message doesn't match, they check their parent object, and so on. A message may receive any message it or any of its parent objects specify (otherwise it gives an error), but an object might implement a different set of actions in response to a message (overriding the parent behavior). Message passing is a very powerful way of creating complex behavior, but it typically runs slower then non-OO approaches and can obscure the source of an error when a message is poorly written or its functionality misunderstood.

Brian's Coweb Assignment 2

Link to this Page