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

Summary Exercises 217 Writing a class means that strongly related elements of data and actions are grouped together. A class presents an interface to its users and hides information about its internal workings. It means that the user of a class need not worry about its implementation. This promotes abstraction in thinking about the structure of software. It also means that a class can be changed without any effect on the rest of the program (provided that it continues to present the same interface). Thus classes promote modularity. Extending (inheriting from) a class is another way of making use of existing components (classes). A subclass inherits the facilities of its immediate superclass and all the superclasses. Most languages support single inheritance. A class can extend the facilities of an existing class by providing one or more of: ■ additional methods ■ additional variables ■ methods that override (act instead of) methods in the superclass. Polymorphism means that similarities between objects can be exploited in the code that uses objects. This means that software is more concise and more easily adapted. Altogether encapsulation, inheritance and polymorphism mean that software is modular, concise and adaptable. It also means that greater use can be made of libraries of useful components. The programming language must explicitly support these features for OOP to be viable. Generics enable tailor-made collections to be constructed. This makes programs more concise and assists with compile-time type checking, and consequently software reliability. There are a number of approaches to garbage collection for software that uses dynamic allocation of memory. Some schemes are automatic but may create timing problems. Some schemes rely on the programmer to make explicit requests, but this can lead to subtle memory problems. • Exercises 15.1 Explain how classes, inheritance and polymorphism support software development. 15.2 Explain how classes, inheritance and polymorphism promote reusable software.

218 Chapter 15 ■ Object-oriented programming 15.3 Suppose that you were asked to design a new programming language for software engineering: ■ select and justify a mechanism for encapsulation ■ select and justify a mechanism for modularity. 15.4 Explain what the term modularity means. Assess how well the following features of programming languages contribute to modularity: ■ methods ■ classes. 15.5 Assess the generics feature. 15.6 Argue for and against pointers in a programming language. 15.7 Argue for and against automatic garbage collection. Answers to self-test questions 15.1 public void moveDown(int amount) { } y = y + amount; 15.2 public int yCoord { } get { return y; } 15.3 Methods: creditAccount, debitAccount, calculateInterest Properties: currentBalance, name 15.4 class Stack { } private Arraylist s = new Arraylist(); public void push(String item) { s.add(0, item); } public String pop() { String item = (String) s.get(0); s.remove(0); return item; }

218 Chapter 15 ■ Object-oriented programming<br />

15.3 Suppose that you were asked to design a new programming language <strong>for</strong> software<br />

engineering:<br />

■ select and justify a mechanism <strong>for</strong> encapsulation<br />

■ select and justify a mechanism <strong>for</strong> modularity.<br />

15.4 Explain what the term modularity means. Assess how well the following features of<br />

programming languages contribute to modularity:<br />

■ methods<br />

■ classes.<br />

15.5 Assess the generics feature.<br />

15.6 Argue <strong>for</strong> and against pointers in a programming language.<br />

15.7 Argue <strong>for</strong> and against automatic garbage collection.<br />

Answers to self-test questions<br />

15.1 public void moveDown(int amount) {<br />

}<br />

y = y + amount;<br />

15.2 public int yCoord {<br />

}<br />

get {<br />

return y;<br />

}<br />

15.3 Methods: creditAccount, debitAccount, calculateInterest<br />

Properties: currentBalance, name<br />

15.4 class Stack {<br />

}<br />

private Arraylist s = new Arraylist();<br />

public void push(String item) {<br />

s.add(0, item);<br />

}<br />

public String pop() {<br />

String item = (String) s.get(0);<br />

s.remove(0);<br />

return item;<br />

}

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

Saved successfully!

Ooh no, something went wrong!