Download Computer Programming with GNU Smalltalk

Transcript
98
Computer Programming with GNU Smalltalk
"answer_4_8.st"
| newNumber howManyNumbers sum arithmeticAverage |
newNumber := 0.
howManyNumbers := 0.
sum := 0.
arithmeticAverage := 0.
[newNumber = 'finish'] whileFalse: [
Transcript show: 'Arithmetic averages of the numbers so far is: ',
arithmeticAverage printString; cr.
Transcript show: 'Please enter a new number or "finish" to exit '.
newNumber := stdin nextLine.
(newNumber isNumeric) ifTrue: [
sum := sum + newNumber asInteger.
howManyNumbers := howManyNumbers + 1.
arithmeticAverage := sum / howManyNumbers.
].
Transcript cr.
].
Arithmetic averages of the numbers so far is: 0
Please enter a new number or "finish" to exit: 2
Arithmetic averages of the numbers so far is: 2
Please enter a new number or "finish" to exit: 4
Arithmetic averages of the numbers so far is: 3
Please enter a new number or "finish" to exit: finish
9.
1 to: 1000 do: [:x | x printNl].
1
...
1000
1
Chapter 5
1. Polymorphism is the name of the concept to determine which method to execute according to the
type of the class of the object we sent the message.
Observing the attributes and behavior of the ancestor class when deriving a new class from it is