29.11.2014 Views

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Just as with the class Date, these times c<strong>an</strong> be compared, added <strong><strong>an</strong>d</strong> subtracted. For example:<br />

(currentTime = previousTime) ifTrue:<br />

[Tr<strong>an</strong>script show: 'Time has stood still!'].<br />

differentTime := currentTime subtractTime: previousTime.<br />

However, the usefulness of the subtractTime: message is somewhat limited. For example,<br />

having evaluated this expression I obta ined the result 2:57:18 PM, when what I really w<strong>an</strong>ted to know<br />

was the difference in hours. In the case of time, I c<strong>an</strong> obtain this by converting the time into seconds<br />

<strong><strong>an</strong>d</strong> dividing by 3600 (the number of seconds in <strong>an</strong> hour). For example:<br />

| currentTime previousTime differentTime |<br />

currentTime := Time now.<br />

previousTime := Time readFromString: '1:00:20 am'.<br />

differentTime := currentTime asSeconds - previousTime asSeconds.<br />

differentTime // 3600.<br />

There are two class side messages which may be of general use. The first is<br />

millisecondClockValue. This message returns a value indicating the number of milliseconds<br />

since the system clock was reset. It c<strong>an</strong> be useful as a me<strong>an</strong>s for generating <strong>an</strong> unique name for a<br />

temporary file etc. The uniqueness of such a filename c<strong>an</strong> not be guar<strong>an</strong>teed as there is a vague ch<strong>an</strong>ce<br />

that a file with the same name may have been left around from <strong>an</strong> execution of the system prior to the<br />

resetting of the system clock (although the ch<strong>an</strong>ces of a temporary file being created with a name<br />

derived from the same millisecond clock reading are remote). For example:<br />

Time millisecondClockValue.<br />

The second class side message is the millisecondsToRun: message. This takes a block as a<br />

parameter <strong><strong>an</strong>d</strong> generates a report on the time required to evaluate that blo ck. This message is most often<br />

used when determining the perform<strong>an</strong>ce of various constructs. For example, in the last chapter it was<br />

used to compare the do: loop with the inject: construct on two collection classes Bag <strong><strong>an</strong>d</strong> Array.<br />

For example:<br />

Time millisecondsToRun:<br />

[1 to: 10000 do: [:i | Tr<strong>an</strong>script show: ' ' , i ] ].<br />

On a Pentium 120 PC with 16 megabytes of memory this resulted in a value of<br />

returned.<br />

79056 seconds being<br />

14.5 The class Character<br />

Characters such as letters (e.g. A, b or Z) are objects in the ir own right in <strong>Smalltalk</strong>. When they are<br />

written alone they are preceded by a dollar sign. For example:<br />

$A, $b, $Z<br />

These objects c<strong>an</strong> be combined together to form strings (which are actually collections of characters).<br />

As the Character class inherits from Magnitude, you c<strong>an</strong> compare characters to determine if<br />

one is greater, less th<strong>an</strong> or equal to <strong>an</strong>other. For example:<br />

Tr<strong>an</strong>script show: ($A < $a) printString.<br />

In addition, Character adds <strong>an</strong>other comparison method called sameAs:. This metho d ignores<br />

case. Thus it is possible to evaluate the following <strong><strong>an</strong>d</strong> have true printed in the Tr<strong>an</strong>script:<br />

Tr<strong>an</strong>script show: ($A sameAs: $a) printString.<br />

The Character class also underst<strong><strong>an</strong>d</strong>s the differences between alphabetic characters, numeric<br />

characters, vowels, uppercase <strong><strong>an</strong>d</strong> lowercase letters etc. These are provided by methods in the testing<br />

protocol, which include:<br />

120

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

Saved successfully!

Ooh no, something went wrong!