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

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

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

7. <strong>Smalltalk</strong> Constructs<br />

7.1 <strong>Introduction</strong><br />

This chapter presents more of the <strong>Smalltalk</strong> l<strong>an</strong>guage. Section two considers the representation <strong><strong>an</strong>d</strong> use<br />

of numbers in <strong>Smalltalk</strong> while Section three considers strings <strong><strong>an</strong>d</strong> characters. Section four discu sses<br />

variables in <strong>Smalltalk</strong> <strong><strong>an</strong>d</strong> Section five literals <strong><strong>an</strong>d</strong> variables. Sections six considers messages, message<br />

types <strong><strong>an</strong>d</strong> their precedence.<br />

7.2 Numbers <strong><strong>an</strong>d</strong> operators<br />

7.2.1 Numeric values in <strong>Smalltalk</strong><br />

Numbers in <strong>Smalltalk</strong> are all objects, that me<strong>an</strong>s that they are ins t<strong>an</strong>ces of a class. For example, integer<br />

numbers such as 2 are <strong>an</strong> inst<strong>an</strong>ce of the class SmallInteger. In fact there are a number of classes<br />

which together provide for the types of numbers normally used, these include SmallInteger,<br />

LargePositiveInteger, LargeNegativeInteger, Float <strong><strong>an</strong>d</strong> Double. These will all be<br />

considered in greater detail later in the book. For the moment we will just consider what numbers look<br />

like in <strong>Smalltalk</strong>.<br />

Just as in most programming l<strong>an</strong>guages, a numeric value in Smalltal k is a series of numbers which<br />

may or may not have a preceding ‘-’ <strong><strong>an</strong>d</strong> may contain a decimal point. For example:<br />

25 -10 1996 12.45 0.13451345 -3.14<br />

It is also possible to specify numbers in bases other th<strong>an</strong> 10. This is done by preceding the number<br />

with the base <strong><strong>an</strong>d</strong> the letter r (which st<strong><strong>an</strong>d</strong>s for radix). For example:<br />

2r101<br />

16r452<br />

Numbers c<strong>an</strong> also be expressed using scientific notation using the ‘e’ (for exponent) or ‘d’ (for<br />

double-precision) suffix plus the exponent in decimal. For example:<br />

10e3 which equals 1000<br />

In addition to integers <strong><strong>an</strong>d</strong> real numbers, <strong>Smalltalk</strong> also explicitly supports fractions (e.g. 7/8) <strong><strong>an</strong>d</strong><br />

radi<strong>an</strong>s. In all other ways numbers in <strong>Smalltalk</strong> are just like numbers in <strong>an</strong>y other l<strong>an</strong>guage.<br />

7.2.2 Arithmetic operators<br />

Table 7.1: Numeric operators<br />

+ addition * multiplication<br />

- subtraction / division<br />

/ / modulus quo quoti<strong>an</strong>t<br />

rem remainder \ \ remainder from modulo division<br />

< less th<strong>an</strong> greater th<strong>an</strong> >= greater th<strong>an</strong> or equal to<br />

max: maximum min: minimum<br />

ceiling roundup floor round down<br />

69

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

Saved successfully!

Ooh no, something went wrong!