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

CoWeb Assignment 2 gtg541u

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 & #8211; Cascade expected ->4 factorial.

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

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

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

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

b collect: [:i | i i] #(1 4 9 16 25)

a perform: #at: withArguments: #(4) 2

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 in Java
while (aBooleanTest)
{
// do stuff
}

In Smalltalk
[aBooleanTest] whileTrue:
[
"do stuff"
].
Object: aBooleanTest
Message: whileTrue
Argument: "do stuff"

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

In Smalltalk
1 to 10 do:
[:i |
"do stuff"
].
Object: 1
Message: to do
Argument: 10, "do stuff"

if/then/else in Java
if (aBooleanTest)
{ // do stuff
}
else
{ // do stuff
}

In Smalltalk
(aBooleanTest)
ifTrue:
[
"do stuff"
].
ifFalse:
[
"do stuff"
].
Object: aBooleanTest
Message: ifTrue ifFalse
Arguments: "do stuff", "do stuff"

Link to this Page