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

CoWeb Assignment 2 (due 6/14)

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
-15
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
Java
while (aBooleanTest)
 {
    // do stuff
 }
Squeak
[aBooleanTest] whileTrue:
 [
    "do stuff, remember to change aBooleanTest"
 ].

aBooleanTest is the object/receiver, whileTrue: is the message, ["do stuff, remember to change aBooleanTest"] is the argument.

for loop
Java
for (i = 1; i <= 10; i++)
 {
    // do stuff
 }
Squeak
1 to 10 do:
	[ :i |
	    "do stuff, use i in here"
	].

1 is the object/receiver, to: do: are the messages,  10 and [ :i | "do stuff, use i in here"] are the arguments.

if/then/else
Java
if (aBooleanTest)
 {   
    // do stuff
 }
else
 {  
    // do stuff
 }
Squeak
(aBooleanTest)
   ifTrue:[
         "do stuff"
      ]
   ifFalse:
      [
         "do stuff"
      ].

aBooleanTest is the object/receiver,  ifTrue: and ifFalse: are the messages,  ["do stuff"] and ["do stuff"] are the arguments.


Link to this Page