Date: Wed, 08 Jan 1997 21:25:11 GMT Server: NCSA/1.4.2 Content-type: text/html
I haven't actually typed this code in and tried to run it, so I apologize in advance for any typos.
max: aNumber ^ self > aNumber ifTrue: [^self] ifFalse: [^aNumber]
isBigger: aCollection ^ self size > aCollection size
isNumeric self do: [:elem| elem isNumber ifFalse: [^false]] . ^true
numOccurances: anObject | counter | counter := 0 . self do: [:elem | elem == anObject ifTrue: [ counter := counter + 1 ] . ^ counter
sumSquares: aCollection | sum | sum := 0 . self isNumeric ifFalse: [ self error: 'Non numeric collection' ] . self do: [:elem| sum := sum + (elem * elem)]. ^sum
serviceAccount self subclassResponsibility
serviceAccount self assessServiceCharge
serviceAccount self payInterest
serviceAccounts self do: [:acct| acct serviceAccount] .
showDifference "Returns 5 if instance variables, 6 if class variable" |a b| a := SomeClass new: 2 b := SomeClass new: 3 ^ a access + b access
Number subclass: #ComplexNumber instanceVariableName: 'real imaginary' classVariableName: '' category: 'Magnitude-Numbers'
new ^self real: 0 imaginary: 0 real: aNumber imaginary: anotherNumber ^ super new real: aNumber imaginary: anotherNumber
real: aNumber imaginary: anotherNumber real := aNumber . imaginary := anotherNumber . ^self real ^ real imaginary ^ imaginary add: aNumber ^ aNumber addWithComplex self addWithComplex: aComplexNumber "The first version of the add method looked like this" |r i| r := self real + aComplexNumber real . i := self imaginary + aComplexNumber imaginary . ^ self class real: r imaginary: i multiply: aComplexNumber "If I was really doing this, I'd do double dispatching again" |r i| r := (self real * aComplexNumber real) - (self imaginary + aComplexNumber imaginary) . i := (self real * aComplexNumber imaginary) + (self imaginary * aComplexNumber real) . ^ self class real: r imaginary: i
addWithComplex aComplexNumber ^ (ComplexNumber real: self imaginary: 0) add: aComplexNumber