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

CA2

Tracing Code (1 point)

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)

while loop

Java:
while (aBooleanTest)
{
   // do stuff
}

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

Example:
	x := 0.
	[ (x < 10) ] 
	whileTrue: [Transcript show: (x) printString.
	            Transcript show: ' '.
	            x := x + 1.].            

"whileTrue" is a message sent to boolean values. "x <10" will return either true or false. 
The block of code after "whileTrue:" are arguments.


for loop

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

Squeak:
1 to: 10 by: 1 do: [" stuff "].

Example:
1 to: 10 by: 1 do: [:x | Transcript show: 'Hello '.].

Message "1 to: 10 by: 1 do:" is sent to the object 1. The block of code after "..do:" are arguments.


if/then/else

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

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

Example:
a := 0.
b := 0.
(a = b)
ifTrue:  [Transcript show: 'A and B are equal'; cr.]
ifFalse: [Transcript show: 'A and B are NOT equal'; cr.].

"ifTrue" and "ifFalse" are messages sent to boolean values. "a = b" will return either true or false. 
The block of code after "ifTrue:" and the block of code after "ifFalse" are arguments
for true and false case, respectively.




Link to this Page