+ All Categories
Home > Documents > 1 Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze...

1 Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze...

Date post: 21-Jan-2016
Category:
Upload: jody-carson
View: 216 times
Download: 0 times
Share this document with a friend
27
1 Chapter 9 Object-Oriented Software Development Software Development Process Analyze Relationships Among Objects Class Development Class Design Guidelines Wrapper Classes Generic Sort Class Generic Matrix Class Generic Linked List Class
Transcript
Page 1: 1 Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

1

Chapter 9Object-Oriented Software Development

Software Development Process Analyze Relationships Among Objects Class Development Class Design Guidelines Wrapper Classes Generic Sort Class Generic Matrix Class Generic Linked List Class

Page 2: 1 Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

2

Software Development

Process

Requirement Specification

System Analysis

System Design

Implementation

Testing

Deployment

Maintenance

Page 3: 1 Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

3

Relationships among Classes

Association

Aggregation

Inheritance

Page 4: 1 Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

4

Association

Association represents a general

binary relationship that describes an

activity between two classes.

Student FacultyCourse*5..60Take Teach

0..3 1Teacher

Page 5: 1 Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

5

Association, cont.An association is usually represented

as a data field in the class.

public class Course

{

private Faculty faculty;

  /**Constructors*/

  /**Methods*/

}

Page 6: 1 Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

6

Aggregation

Aggregation is a special form of association,

which represents an ownership relationship

between two classes. Aggregation models the

relationship like has-a, part-of, owns, and

employed-by.

Magazine ConsultantPublisher1*

Owned by Employed by**

Expert

Page 7: 1 Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

7

Inheritance

Inheritance models the is-a relationship

between two classes.

Person

Faculty

Student

Page 8: 1 Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

8

Inheritancepublic class Student extend Person

{

/**Constructors*/

  /**Methods*/

}

public class Faculty extend Person

{

/**Constructors*/

/**Methods*/

}

Page 9: 1 Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

9

Class Development

1. Identify classes for the system.

2. Describe attributes and methods in each

class.

3. Establish relationships among classes.

4. Create classes.

Page 10: 1 Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

10

Example 9.1 Borrowing Mortgages

NameName BorrowerBorrowerPersonPersonMortgageMortgage

Name

1 1 Has Live 1 1

Mortgage Borrow

Borrower -mortgage: Mortgage +Borrower() +Borrower(name: Name, address: Address) +getMortgage(): Mortgage +setMortgage(mortgage: Mortgage): void +toString(): String

Address -street: String -city: String -state: String -zip: String +Address() +Address(street: String, city: String, state: String, zip: String) getStreet(): String +getCity(): String +getState(): String +getZip(): String +setStreet(street: String): void +setCity(city: String): void +setState(state: String): void +setZip(zip: String): void +getFullAddress(): String

Defined in Example 6.7

Defined in Example 8.6

Person -name: Name -address: Address +Person() +Person(name: Name, address: Address) +getName(): Name +setName(name: Name): void +getAddress(): Address +setAddress(address: Address): void +toString(): String

AddressAddress

Page 11: 1 Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

11

Example 9.1 Borrowing Mortgages, cont.

The following is a test program that uses the classes Name, Person, Address, Borrower, and Mortgage.

BorrowMortgageBorrowMortgage RunRun

Page 12: 1 Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

12

Example 9.2 The Rational Class

RationalRational RunRunTestRationalClassTestRationalClass

1

1 Add, Subtract, Multiply, Divide

Rational -numerator: long -denominator: long +Rational() +Rational(numerator: long, Denomination: long) +getNumerator(): long +getDenominator(): long +add(secondRational: Rational): Rational +multiply(Rational secondRational): Rational +subtract(Rational secondRational): Rational +divide(Rational secondRational): Rational +toString(): String -gcd(n: long, d: long): long

java.lang.Number +byteValue(): byte +shortValue(): short +intValue(): int +longVlaue(): long +floatValue(): float +doubleValue():double

java.lang.Comparable +int compareTo(Object)

Page 13: 1 Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

13

Class Design Guidelines

Hide private data and private methods.

A property that is shared by all the instances of the class should be declared as a class property.

Provide a public default constructor and override the equals method and the toString method defined in the Object class whenever possible.

Page 14: 1 Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

14

Class Design Guidelines, cont.

Choose informative names and followconsistent styles.

A class should describe a single entity or a set of similar operations.

Group common data fields and operations shared by other classes.

Page 15: 1 Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

15

Wrapper Classes

Boolean

Character

Short

ByteObject

-

Double-

Float-

Long-

Integer-

Short-

Byte-

Character-

Boolean-

Number-

Integer Long

Float

Double

Page 16: 1 Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

16

The Integer Classand The Double Class

Constructors

Class Constants MAX_VALUE, MIN_VALUE

Conversion Methods

Page 17: 1 Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

17

Example 9.3 Sorting an Array of Objects

Objective: The example presents a generic method for sorting an array of objects. The objects are instances of the Comparable interface and they are compared using the compareTo method.

Generic SortGeneric Sort RunRun

Page 18: 1 Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

18

Example 9.4Designing Generic Classes

Objective: This example gives a generic class for matrix arithmetic. This class implements matrix addition and multiplication common for all types of matrices.

GenericMatrixGenericMatrix

Page 19: 1 Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

19

Example 9.4, cont.

GenericMatrix

-matrix: Object[][] #GenericMatrix(matrix: Object[][]) +getMatrix(): Object[][] +setMatrix(matrix: Object[][]): void +addMatrix(secondMatrix: Object[][]): Object[][] +multiplyMatrix(secondMatrix: Object[][]): Object[][] +printResult(m1: GenericMatrix, m2: GenericMatrix, m3: GenericMatrix, op: char): void #createGenericMatrix():GenericMatrix #add(o1: Object, o2: Object): Object #multiply(o1: Object, o2: Object): Object #zero():Object

IntegerMatrix

RationalMatrix

Page 20: 1 Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

20

Example 9.4, cont.

Objective: This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic.

TestIntegerMatrixTestIntegerMatrix RunRun

TestRationalMatrixTestRationalMatrix RunRunRational MatrixRational Matrix

IntegerMatrixIntegerMatrix

Page 21: 1 Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

21

Linked ListArrays are useful for storing and managing a set of elements of the same type. However, since the length of an array is fixed once the array is created, you need to know the length of the array before you create it. A linked list can grow or shrink dynamically as needed.

A linked list consists of nodes, as shown in Figure 9.14. Each node contains an element and each node is linked to its next neighbor. Thus, a node can be defined as a class as follows:

Page 22: 1 Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

22

Linked List Structurepublic class Node{ Object element; Node next;  public Node(Object o) { element = o; }}

first element next

element next

element next

last …

node1 node2 node n

Page 23: 1 Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

23

Create a Linked List with three Nodes

Node n1 = new Node(new String(“Welcome to Java!”));

Node n2 = new Node(new JButton(“OK”));

Node n3 = new Node(new Rational(1, 2));

n1.next = n2;

n2.next = n3;

Node first = n1;

Node last = n3;

Page 24: 1 Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

24

Generic Linked List

… element next

element next

current

first element next

element next:null

last …

element next

New node inserted here

temp

Add a new node

Page 25: 1 Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

25

Generic Linked List

Remove a new node

… element next

element next

previous

first element next

element next:null

last …

Node to be deleted

current

After the node isdeleted

Page 26: 1 Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

26

Example 9.5 Using Linked List

This example creates a linked list using GenericLinkedList. It then uses the add method to add strings to the list and uses the remove method to remove strings from the list.

GenericLinkedListGenericLinkedList

RunRunTestLinkedListTestLinkedList

Page 27: 1 Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

27

Generic Linked List

GenericLinkedList list = new GenericLinkedList();

list.addLast(new String(“Welcome to Java”));list.addLast(new JButton(“OK”));list.addLast(new Rational(1, 2));


Recommended