21.08.2013 Views

Software Engineering for Students A Programming Approach

Software Engineering for Students A Programming Approach

Software Engineering for Students A Programming Approach

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.

194 Chapter 14 ■ The basics<br />

In a language without this facility, we are <strong>for</strong>ced to map the days of the week onto<br />

integers, so that 1 means Monday etc. But then we get no help from the compiler when<br />

we write (correct) statements, such as:<br />

int today;<br />

today = 2;<br />

or even the “illegal”<br />

today = 0;<br />

since today is an integer variable and there<strong>for</strong>e may be assigned any integer value.<br />

SELF-TEST QUESTION<br />

14.4 Make the case <strong>for</strong> user-defined types.<br />

Enumerated types, such as the C++ facility described above, have their limitations. An<br />

enumerated type can be declared, variables created, assignments and comparisons carried<br />

out, but these are the only operations and we cannot create any more. For example, in<br />

the above example one cannot write a method nextDay. Moreover different enums cannot<br />

contain identical names. For example, we are prevented from writing:<br />

enum Weekend {Saturday, Sunday};<br />

because the names clash with those already in enum Day.<br />

Arguably, if the language provides classes (Chapter 15) it does not need enums. In<br />

fact the Java enum facility is almost a class.<br />

14.14 ● Arrays<br />

Composite data types allow the programmer to model structured data objects. The<br />

most common aggregate data abstraction provided by programming languages is the<br />

array: a collection of homogeneous elements (all elements of the same type) which may<br />

be referenced through their positions (usually an integer) within the collection. Arrays<br />

are characterized by the type of their elements and by the index or subscript range or<br />

ranges which specify the size, number of dimensions and how individual elements of<br />

the array may be referenced.<br />

For example, the Java array definition shown below defines an array named table.<br />

It is a one-dimensional array of integers with the subscript varying from 0 through 9.<br />

In Java, subscripts always start at 0, betraying the C origins of the language as a language<br />

close to machine instructions.<br />

int table[] = new int[10];

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

Saved successfully!

Ooh no, something went wrong!