SMALLTALK / SQUEAK WORKSPACE EXAMPLES Select one example at a time and use the indicated Workspace menu command. Method definitions are in TutorialCounter.st; file that class in before using it. 1. Your first conversation — A first expression Workspace · select the expression and choose Print it 6 * 7 Expected: 42 1. Your first conversation — Write to the Transcript Workspace · Do it; open the Transcript from the tools menu to see the text Transcript show: 'Hello from Smalltalk!'; cr. Expected: Hello from Smalltalk! (in the Transcript) 2. Objects receive messages — Three forms of message Workspace · Print it on each line separately 'squeak' asUppercase. 7 + 5. 'Smalltalk' copyFrom: 1 to: 5. Expected: 'SQUEAK' · 12 · 'Small' 2. Objects receive messages — Discover an object’s class Workspace · Print it 'Squeak' class Expected: ByteString 3. A little syntax goes a long way — Names and values Workspace · select the whole script and Print it | name greeting | name := 'Ada'. greeting := 'Hello, ', name. greeting Expected: 'Hello, Ada' 3. A little syntax goes a long way — A few literal objects Workspace · select the whole expression and Inspect it {42. 3.5. 'It''s Squeak'. #blue. $S. true. nil} Expected: An Array containing seven objects 4. Learn the message order — Grouping changes the answer Workspace · Print it on each line separately 2 + 3 * 4. 2 + (3 * 4). Expected: 20 · 14 4. Learn the message order — Build a collection with a cascade Workspace · select the whole expression and Inspect it OrderedCollection new add: 'red'; add: 'green'; add: 'blue'; yourself Expected: An OrderedCollection of three strings 5. Blocks are deferred work — Evaluate a block with an argument Workspace · select the whole script and Print it | double | double := [:number | number * 2]. double value: 21 Expected: 42 5. Blocks are deferred work — Conditional and repetition Workspace · select the whole script and Print it | total | total := 0. 5 timesRepeat: [total := total + 2]. total = 10 ifTrue: ['Made ten'] ifFalse: ['Try again'] Expected: 'Made ten' 6. Let collections do the work — Transform, filter, and total Workspace · select the whole script and Inspect it | values doubled evens sum | values := #(1 2 3 4 5). doubled := values collect: [:n | n * 2]. evens := values select: [:n | n even]. sum := values inject: 0 into: [:total :n | total + n]. {doubled. evens. sum} Expected: #(#(2 4 6 8 10) #(2 4) 15) 6. Let collections do the work — Look up a value by key Workspace · select the whole script and Print it | colors | colors := Dictionary new. colors at: #accent put: 'coral'. colors at: #missing ifAbsent: ['default color'] Expected: 'default color' 7. Create your own class — Define TutorialCounter Workspace · Do it once to create the class Object subclass: #TutorialCounter instanceVariableNames: 'count' classVariableNames: '' poolDictionaries: '' category: 'Smalltalk-Tutorial' Expected: TutorialCounter appears in the System Browser 7. Create your own class — Use an instance Workspace · after adding the three methods, Print it on the whole script | counter | counter := TutorialCounter new. counter increment; increment. counter value Expected: 2 8. Program to a shared protocol — One message, several kinds of object Workspace · select the whole expression and Print it {'Squeak'. #(10 20 30). OrderedCollection new} collect: [:each | each size] Expected: #(6 3 0) 9. Explore the running system — Inspect a live collection Workspace · Do it (OrderedCollection with: 'first' with: 'second') inspect Expected: An inspector opens on the collection 9. Explore the running system — Ask about behavior Workspace · Print it on each line separately 'hello' respondsTo: #asUppercase. 'hELLo' perform: #asUppercase. Expected: true · 'HELLO' 9. Explore the running system — Browse a class Workspace · Do it OrderedCollection browse Expected: The System Browser opens on OrderedCollection 10. Make a Morph — Build a card and its label Workspace · select the whole script and Do it | card label | card := BorderedMorph new color: Color lightBlue; borderWidth: 2; extent: 220@100; position: 120@120; yourself. label := StringMorph contents: 'Hello, Morphic'. card addMorph: label. label center: card center. card openInWorld Expected: A blue card with a centered label appears in the World 11. Draw with a Pen — A square on a bitmap Workspace · select the whole script and Do it | form pen | form := Form extent: 280@240 depth: 32. form fillColor: Color white. pen := Pen newOnForm: form. pen defaultNib: 3; color: Color blue. pen place: 60@40; north; turn: 90; down. 4 timesRepeat: [pen go: 160; turn: 90]. pen up. (ImageMorph new image: form) openInWorld Expected: A blue 160-pixel square appears in a bitmap Morph 12. Keep your Smalltalk world — Revisit your new class Workspace · after creating or filing in TutorialCounter, Do it TutorialCounter new inspect Expected: An inspector opens on a fresh counter whose count is 0 12. Keep your Smalltalk world — Open the project launcher Workspace · in the Pen Studio project image only · Do it PenDemoLauncher run. Expected: The four-demo launcher opens