Software Engineering for Students A Programming Approach
Software Engineering for Students A Programming Approach Software Engineering for Students A Programming Approach
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
- Page 186 and 187: Answers to self-test questions 163
- Page 188 and 189: CHAPTER 13 Refactoring This chapter
- Page 190 and 191: 13.3 ● Move Method 13.6 Inline Cl
- Page 192 and 193: class Sprite Instance variables x y
- Page 194 and 195: Summary Summary 171 it is making po
- Page 196: PART C PROGRAMMING LANGUAGES
- Page 199 and 200: 176 Chapter 14 ■ The basics and a
- Page 201 and 202: 178 Chapter 14 ■ The basics > > >
- Page 203 and 204: 180 Chapter 14 ■ The basics > Ear
- Page 205 and 206: 182 Chapter 14 ■ The basics > Cas
- Page 207 and 208: 184 Chapter 14 ■ The basics > > >
- Page 209 and 210: 186 Chapter 14 ■ The basics > } }
- Page 211 and 212: 188 Chapter 14 ■ The basics Unfor
- Page 213 and 214: 190 Chapter 14 ■ The basics Ada d
- Page 215 and 216: 192 Chapter 14 ■ The basics The w
- Page 217 and 218: 194 Chapter 14 ■ The basics In a
- Page 219 and 220: 196 Chapter 14 ■ The basics > str
- Page 221 and 222: 198 Chapter 14 ■ The basics Answe
- Page 223 and 224: CHAPTER 15 Object-oriented programm
- Page 225 and 226: 202 Chapter 15 ■ Object-oriented
- Page 227 and 228: 204 Chapter 15 ■ Object-oriented
- Page 229 and 230: 206 Chapter 15 ■ Object-oriented
- Page 231 and 232: 208 Chapter 15 ■ Object-oriented
- Page 233 and 234: 210 Chapter 15 ■ Object-oriented
- Page 235: 212 Chapter 15 ■ Object-oriented
- Page 239 and 240: 216 Chapter 15 ■ Object-oriented
- Page 241 and 242: 218 Chapter 15 ■ Object-oriented
- Page 243 and 244: 220 Chapter 15 ■ Object-oriented
- Page 245 and 246: 222 Chapter 16 ■ Programming in t
- Page 247 and 248: 224 Chapter 16 ■ Programming in t
- Page 249 and 250: 226 Chapter 16 ■ Programming in t
- Page 251 and 252: 228 Chapter 16 ■ Programming in t
- Page 253 and 254: 230 Chapter 16 ■ Programming in t
- Page 255 and 256: 232 Chapter 16 ■ Programming in t
- Page 257 and 258: 234 Chapter 16 ■ Programming in t
- Page 259 and 260: 236 Chapter 16 ■ Programming in t
- Page 261 and 262: 238 Chapter 17 ■ Software robustn
- Page 263 and 264: 240 Chapter 17 ■ Software robustn
- Page 265 and 266: 242 Chapter 17 ■ Software robustn
- Page 267 and 268: 244 Chapter 17 ■ Software robustn
- Page 269 and 270: 246 Chapter 17 ■ Software robustn
- Page 271 and 272: 248 Chapter 17 ■ Software robustn
- Page 273 and 274: 250 Chapter 17 ■ Software robustn
- Page 275 and 276: 252 Chapter 17 ■ Software robustn
- Page 277 and 278: 254 Chapter 17 ■ Software robustn
- Page 279 and 280: 256 Chapter 17 ■ Software robustn
- Page 281 and 282: 258 Chapter 17 ■ Software robustn
- Page 283 and 284: 260 Chapter 18 ■ Scripting GNU/Li
- Page 285 and 286: 262 Chapter 18 ■ Scripting In sum
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 />
>