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

Darryl D. Prince CoWeb Assignment 2

Tracing Code (1 point)
The following Smalltalk statements are written in a Squeak Workspace. In order from top to bottom, you execute each statement with Alt-p to print its result. Next to each statement, write the result.

1 + 2 3 – 4 factorial RESULT -15

a := #(1 2 3 4 5) RESULT #(1 2 3 4 5)

a select: [:i | i odd] RESULT #(1 3 5)

b := a RESULT #(1 2 3 4 5)

a := a reversed RESULT #(1 2 3 4 5)

b collect: [:i | i i] RESULT #(1 2 3 4 5)

a perform: #at: withArguments: #(4) RESULT #(1 2 3 4 5)




Message Passing (1 point)
Smalltalk is built on a few uniform design principles. One of these is that computation happens through message passing: An object gets sent a message (perhaps with some arguments) and returns an object. Even traditional control structures (while loops, for loops, if/then/else) are implemented through message passing. For each of the control structures below, translate the Java code into Smalltalk. For each part, indicate what is the object, what is the message, and what are the arguments.

while loop

while (aBooleanTest)
{
// do stuff
}


[aBooleanTest]
whileTrue: ["do stuff"].


for loop

for (i = 1; i = 10; i++)
{
// do stuff
}


1 to: 10 do: [:i | "do stuff" ].


if/then/else

if (aBooleanTest)
{ // do stuff
}
else
{ // do stuff
}

(aBooleanTest)
ifTrue: ["do stuff"]
ifFalse: ["do stuff2"]


BACK TO Darryl D. "The" Prince

Link to this Page