Software Engineering for Students A Programming Approach

Software Engineering for Students A Programming Approach Software Engineering for Students A Programming Approach

web.firat.edu.tr
from web.firat.edu.tr More from this publisher
21.08.2013 Views

15.8 Dynamic data structures and pointers 213 holds only objects of the class Object. We can avoid this if we create an array list that can only contain Sprite objects, as follows: ArrayList shapes = new ArrayList(); The declaration, with the class Sprite enclosed in diamond brackets, says that this new array list is to contain only Sprite objects. Remember that ArrayList is a Java library class. We have qualified it by saying it must contain only Sprite objects. So now we can avoid the casting operation, rewriting the above as follows: for (int s = 0, s < game.size(); s++) { Sprite sprite = game.get(s); sprite.display(paper); } But there is much more to be gained than brevity. The compiler can check that only objects of the class Sprite (or its subclasses) are added to the array list in statements such as: game.add(alien); Thus errors can be caught at compile time, rather than at (more embarrassingly) run time. The run-time error would be an InvalidCastException when an object copied from the array list is casted. In summary, generics allow more concise programming (by avoiding casting) and better compile-time checking. SELF-TEST QUESTIONS 15.6 Write a method that accepts as a parameter an array list of String objects. Each string is an integer number. Return the sum of the numbers. 15.7 Suggest a drawback of generics. Generics are provided in Ada, Java and C++ but are not provided in C. 15.8 ● Dynamic data structures and pointers Many programs need to acquire temporary memory to carry out their task. Examples are a graphics program that needs to acquire sufficient memory to represent an image in memory, and a word processor that needs memory to hold the text of a document. In the cyberspace invaders game, objects representing lasers and bombs are created and destroyed. >

214 Chapter 15 ■ Object-oriented programming In an object-oriented language, memory is required each time a new object is created (instantiated) to provide space for the data associated with the object. This space can be released when the object is no longer required. Similarly, if a nonobject-oriented language is used, a program will often need temporary workspace in which to build a data structure that grows and shrinks according to the demand. These are sometimes termed dynamic data structures, and clearly it requires dynamic memory management. SELF-TEST QUESTION 15.8 Think of an example of a program that needs to acquire memory dynamically. In C or C++, the programmer can explicitly issue a request (using the function malloc) to the memory manager component of the operating system to obtain a region of memory. Subsequently a call to function free returns the space to the memory manager. The pointer data type is provided by such modern languages as Ada and C++ but not by older languages, such as Fortran and Cobol. More recently, the Java language does not provide pointers accessible to the programmer. Pointers provide the programmer with the ability to refer to a data object indirectly. We can manipulate the object “pointed” to or referenced by the pointer. Pointers are particularly useful in conjunction with dynamic data structures – situations where the size of a data collection cannot be predicted in advance or where the structure of the collection is dynamically varying. Typically pointers are used to link one record to another in what is called a linked data structure. In some languages, recursive data structures, such as lists and trees, are more easily described using pointers. Similarly, such operations as deleting an element from a linked list or inserting a new element into a balanced binary tree are more easily accomplished using pointers. Although such data types can be implemented using arrays, the mapping is less clear and certainly less flexible. Also performance is often faster when a dynamic structure is used. SELF-TEST QUESTION 15.9 Compare inserting a new item into a structure implemented as: ■ an array ■ a dynamic linked data structure. The use of pointers brings considerable power and flexibility, but with the consequent responsibility. It is well recognized that the explicit use of pointers is extremely

15.8 Dynamic data structures and pointers 213<br />

holds only objects of the class Object. We can avoid this if we create an array list that<br />

can only contain Sprite objects, as follows:<br />

ArrayList shapes = new ArrayList();<br />

The declaration, with the class Sprite enclosed in diamond brackets, says that this<br />

new array list is to contain only Sprite objects. Remember that ArrayList is a Java<br />

library class. We have qualified it by saying it must contain only Sprite objects. So now<br />

we can avoid the casting operation, rewriting the above as follows:<br />

<strong>for</strong> (int s = 0, s < game.size(); s++) {<br />

Sprite sprite = game.get(s);<br />

sprite.display(paper);<br />

}<br />

But there is much more to be gained than brevity. The compiler can check that only<br />

objects of the class Sprite (or its subclasses) are added to the array list in statements<br />

such as:<br />

game.add(alien);<br />

Thus errors can be caught at compile time, rather than at (more embarrassingly) run<br />

time. The run-time error would be an InvalidCastException when an object copied<br />

from the array list is casted.<br />

In summary, generics allow more concise programming (by avoiding casting) and<br />

better compile-time checking.<br />

SELF-TEST QUESTIONS<br />

15.6 Write a method that accepts as a parameter an array list of String objects.<br />

Each string is an integer number. Return the sum of the numbers.<br />

15.7 Suggest a drawback of generics.<br />

Generics are provided in Ada, Java and C++ but are not provided in C.<br />

15.8 ● Dynamic data structures and pointers<br />

Many programs need to acquire temporary memory to carry out their task. Examples<br />

are a graphics program that needs to acquire sufficient memory to represent an image<br />

in memory, and a word processor that needs memory to hold the text of a document.<br />

In the cyberspace invaders game, objects representing lasers and bombs are created and<br />

destroyed.<br />

>

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

Saved successfully!

Ooh no, something went wrong!