06.06.2013 Views

Computer Programming with GNU Smalltalk - Free

Computer Programming with GNU Smalltalk - Free

Computer Programming with GNU Smalltalk - Free

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

98 <strong>Computer</strong> <strong>Programming</strong> <strong>with</strong> <strong>GNU</strong> <strong>Smalltalk</strong><br />

9.<br />

Chapter 5<br />

"answer_4_8.st"<br />

| newNumber howManyNumbers sum arithmeticAverage |<br />

newNumber := 0.<br />

howManyNumbers := 0.<br />

sum := 0.<br />

arithmeticAverage := 0.<br />

[newNumber = 'finish'] whileFalse: [<br />

Transcript show: 'Arithmetic averages of the numbers so far is: ',<br />

arithmeticAverage printString; cr.<br />

].<br />

Transcript show: 'Please enter a new number or "finish" to exit '.<br />

newNumber := stdin nextLine.<br />

(newNumber isNumeric) ifTrue: [<br />

sum := sum + newNumber asInteger.<br />

howManyNumbers := howManyNumbers + 1.<br />

arithmeticAverage := sum / howManyNumbers.<br />

].<br />

Transcript cr.<br />

Arithmetic averages of the numbers so far is: 0<br />

Please enter a new number or "finish" to exit: 2<br />

Arithmetic averages of the numbers so far is: 2<br />

Please enter a new number or "finish" to exit: 4<br />

Arithmetic averages of the numbers so far is: 3<br />

Please enter a new number or "finish" to exit: finish<br />

1 to: 1000 do: [:x | x printNl].<br />

1<br />

...<br />

1000<br />

1<br />

1. Polymorphism is the name of the concept to determine which method to execute according to the<br />

type of the class of the object we sent the message.<br />

Observing the attributes and behavior of the ancestor class when deriving a new class from it is

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!