CS341, Autumn 1996
(Check the last two pages of the exam for help with Smalltalk syntax.)
max: aNumber for class Number, that
returns the maxiumum of the receiver and its argument.
isBigger: aCollection for class Collection that
returns true if the receiver has more elements than
the argument and false otherwise.
isNumeric for class Collection that
returns true if and only if all elements in the collection
are numbers. (Smalltalk has a method isNumber that
answers whether the receiver is an instance of any numeric class.)
numOccurrences: anObject for class Collection that
returns the number of times the argument appears in the receiver.
sumSquares for class Collection that
returns the sum of the squares of all the numbers in the receiver.
Signal an error if the collection contains a non-number.
AccountList
is a Collection, all members of which are instances
either of class CheckingAccount or SavingsAccount.
These two classes are in turn subclasses of Account.
Suppose that instances of CheckingAccount implement the
method assessServiceCharge and
instances of SavingsAccount implement the
method payInterest. Both of these are unary methods.
Design a simple scheme by which every member of AccountList gets
its service charge assessed or its interest paid, as appropriate.
(Hint: you can define define additional instance methods for
Account, CheckingAccount, SavingsAccount, and
Collection.)
SomeClass that are identical except one has a single instance
variable and the other has a single class variable. Show some
code that generates different results depending on which definition
of SomeClass you use.
Object subclass: SomeClass || Object subclass: SomeClass
instanceVariableNames: 'aVar' || instanceVariableNames: ''
classVariableNames: '' || classVariableNames: 'AVar'
||
new: aValue || new: aValue
"A class method for SomeClass || "A class method for SomeClass
to create an instance." || to create an instance."
^self new set: aValue. || ^self new set: aValue.
||
access || access
"An instance method for SomeClass || "An instance method for SomeClass
to access its variable." || to access its variable."
^aVar. || ^AVar.
||
set: newValue || set: newValue
"An instance method for SomeClass || "An instance method for SomeClass
to set its variable value." || to set its variable value."
aVar := newValue. || AVar := newValue.
^self. || ^self.
A complex number has a real part and an imaginary part,
which we might write
where a is the
real part and b is the imaginary part. The rules for
adding and multiplying two complex numbers
are as follows:

Define the class ComplexNumber and define these methods:
real:imaginary: (a class method): Answer an
instance of the receiver with the specified real and imaginary parts.
new (a class method): Answer an
instance of the receiver with real part 0 and imaginary part
0.
add: (an instance method): Answer a complex number that
is the sum of the receiver and the argument (also a complex number).
multiply: (an instance method): Answer a complex number that
is the product of the receiver and the argument (also a complex number).
add: can accept as an argument
either an integer or a complex number. Adding an integer
to a complex number is
straightforward:
.
Hint: for a truly elegant solution, consider the fact
that an integer is just a complex number whose complex part is
0.
(Put your answers on the next page.)
Answer to complex numbers goes here.
Here is some Smalltalk syntax that might help you out:
true and false are boolean constants.
aNumber <= anotherNumber
anObject = anObject (object-dependent equality)
anObject == anotherNumber (true if the two objects are identical)
anObject isNil (true only if the object is the constant nil)
aCollection isEmpty (true only if the collection has no elements).
aBoolean ifTrue: [aBlock].
aBoolean ifTrue: [aBlock] ifFalse: [aBlock].
Set and Bag.
Array new: 10 creates
an array with 10 positions, numbered 1 through 10.
anOrderedCollection first, anOrderedCollection last
anArray at: anIndex
anUnorderedCollection add: anObject
anOrderedCollection addFirst: anObjectanOrderedCollection addLast: anObject
anArray at: index put: newValue
anUnorderedCollection remove: anObject
anOrderedCollection remove: anObjectanOrderedCollection removeFirstanOrderedCollection removeLast
aCollection isEmpty
aCollection includes: anObject
aCollection size answers the number of items in the collection
block is of the form [:var | body] where
var is a formal parameter that can be used in the body.
aCollection do: aBlockitem is
bound to each item in turn.
aCollection select: aBlockaCollection
for which the block answers true.
aCollection reject: aBlockaCollection
for which the block answers false.
aCollection collect: aBlockaCollection detect: aBlockaCollection detect: aBlock ifNone: exceptionBlock
NameOfSuperClass subclass: #NameOfClass
instanceVariableNames: 'instVarName1 instVarName2'
classVariableNames: 'ClassVarName1 ClassVarName2'
category: 'Magnitude-Numbers'
----------------------------------
message selector and argument names
"comment stating purpose of message"
| temporary variable names |
statements