"Lecture 19, CSE341, Spring 2004" "Introduction to Smalltalk" 4. x _ true. y := 5. z := 1 [ y > 1] whileTrue: [ z := z * y. y := y - 1 ]. z str := 'hi mom'. str at: 4. str at: 4 put: $t. str at: 4. "str at: 4 put: 34" "str at: 123 put: $x" "str at: 'huh' put: $x" 5 negated. 4 + 9. 3 raisedTo: 4 "nil negated" " 'hi' negated" " [x:=3] negated" th := [x + y]. x := 4. y := 3. th value factHelp := [:x :acc | [x > 1] whileTrue: [ acc _ acc * x. x _ x - 1]. acc]. fact := [:x | factHelp value: x value: 1]. fact2 := [:x | factHelp valueWithArguments: {x. 1}]. factcheck := [:x | x > 0 ifTrue: [fact x] ifFalse: [self error: 'neg arg']]. (fact value: 5) + (fact2 value: 4) "(fact value: 5) + (factcheck value: -3)" Object subclass: #MyPoint instanceVariableNames: 'x y' classVariableNames: '' poolDictionaries: '' category: 'lec19' pt := MyPoint new. "nobody actually defines methods this way" MyPoint compile: 'x ^x'. MyPoint compile: 'x:a x:=a' pt x: 4. pt x pt := MyPoint new. pt x "after adding some other methods in the browser..." pt x: 4. pt y: 3. ((pt y * pt y) + (pt x * pt x)) sqrt "no precedence on infix" pt distFromOrigin pt x:6 y:8. pt distFromOrigin pt x:6; y:8. pt distFromOrigin "after adding ColorPoint..." cpt := MyColorPoint new. cpt x: 4. cpt color ppt := MyPolarPoint new. ppt rho:0 theta:0. "better way next time" ppt x:4; y:3. ppt distFromOrigin + ppt distFromOrigin2