Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Chapter 1 Object-Oriented Design Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 1 Objectives To become familiar with the process of program development (§12.2). To understand the relationship types: association, aggregation, composition, strong inheritance, and weak inheritance (§12.3). To declare classes to represent the relationships among the classes (§12.3). Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 2 Software Development Process Requirement Specification System Analysis System Design Implementation Testing Deployment Maintenance Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 3 Requirement Specification Requirement Specification System Analysis System Design •A formal process •to understand the problem •document in detail what the software system needs to do. •involves close interaction between users and designers. Implementation Testing Most of the examples in this book are simple, and their requirements are clearly stated. In the real world, however, problems are not well defined. You need to study a problem carefully to identify its requirements. Deployment Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 Maintenance 4 System Analysis Requirement Specification Seeks to analyze the business process in terms of data flow, and to identify the system’s input and output. System Analysis System Design Implementation Part of the analysis entails modeling the system’s behavior. The model is intended to capture the essential elements of the system and to define services to the system. Testing Deployment Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 Maintenance 5 System Design The process of designing the system’s components Requirement Specification System Analysis System Design Implementation Testing This phase involves the use of many levels of abstraction to decompose the problem into manageable components, identify classes and interfaces, and establish relationships among the classes and interfaces. Deployment Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 Maintenance 6 Implementation The process of translating the system design into programs. Separate programs are written for each component and put to work together. Requirement Specification System Analysis System Design Implementation This phase requires the use of a programming language like Java. The implementation involves coding, testing, and debugging. Testing Deployment Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 Maintenance 7 Testing Requirement Specification Ensures that the code meets the requirements specification and weeds out bugs. System Analysis System Design Implementation An independent team of software engineers not involved in the design and implementation of the project usually conducts such testing. Testing Deployment Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 Maintenance 8 Deployment Requirement Specification Deployment makes the project available for use. System Analysis System Design Implementation For a Java applet, this means installing it on a Web server; for a Java application, installing it on the client's computer. Testing Deployment Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 Maintenance 9 Maintenance Requirement Specification Maintenance is concerned with changing and improving the product. System Analysis System Design Implementation Testing A software product must continue to perform and improve in a changing environment. This requires periodic upgrades of the product to fix newly discovered bugs and incorporate changes. Deployment Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 Maintenance 10 Relationships among Classes Association Aggregation Composition Inheritance Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 11 Association Association represents a general binary relationship that describes an activity between two classes. Student 5..60 public class Student { /** Data fields */ private Course[] courseList; Take * Course 0..3 public class Course { /** Data fields */ private Student[] classList; private Faculty faculty; /** Constructors */ /** Methods */ /** Constructors */ /** Methods */ } Teach 1 Teacher Faculty public class Faculty { /** Data fields */ private Course[] courseList; /** Constructors */ /** Methods */ } } An association is usually represented as a data field in the class. Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 12 Translation is not Unique NOTE: If you don’t need to know the courses a student takes or a faculty teaches, the data field coureList in Student or Faculty can be omitted. Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 13 Association Between Same Class Association may exist between objects of the same class. For example, a person may have a supervisor. 1 Person Supervisor 1 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 14 Aggregation and Composition Aggregation is a special form of association, which represents an ownership relationship between two classes. Aggregation models the has-a relationship. If an object is exclusively owned by an aggregated object, the relationship between the object and its aggregated object is referred to as composition. Composition Name Aggregation Person Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 Address 15 Representing Aggregation in Classes An aggregation relationship is usually represented as a data field in the aggregated class. public class Name { /** Data fields */ /** Constructors */ /** Methods */ } public class Person { /** Data fields */ private Name name; private Address address; public class Address { /** Data fields */ /** Constructors */ /** Methods */ } /** Constructors */ /** Methods */ } Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 16 Dependency Relationship between two classes where one class (client) uses the other (supplier) Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 17 Degree of Coupling Coupling increases Dependency, association, aggregation, composition Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 18 Inner Classes Translation If Name or Address is used in the Person class only, they can be declared as an inner class in Person. For example, public class Person { private Name name; private Address address; ... class Name { ... } class Address { ... } } Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 19 Inheritance Inheritance models the is-an-extension-of relationship between two classes. public class Faculty extends Person { Person /** Data fields */ /** Constructors */ /** Methods */ Faculty } (A) (B) Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 20 Weak Inheritance Relationship A weak is-an-extension-of relationship can be represented using interfaces. For example, the weak is-an-extension-of relationship “students are comparable based on their grades” can be represented by implementing the Comparable interface, as follows: public class Student extends Person implements Comparable { Person /** Data fields, Constructors, and */ /** Methods */ Student Comparable /** Implement the compareTo method */ public int compareTo(Object object) { // ... } } (A) (B) Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 21 Class Design 1. Identify classes for the system (e.g, Person, Name, Address) 2. Establish relationships among classes (e.g, A borrower is a person who has a loan) 3. Describe attributes and methods in each class (e.g, The Name class has the properties firstName, lastName, their associated get and set methods, and the getFullName method) 4. Implement the classes (coding the classes, often using get ans set method) Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 22 Example 12.1 Borrowing Loans Name Person -firstName: String -mi: char -lastName: String -name: Name -address: Address +Person() +Person(name: Name, address: Address) +getName(): Name +seName(name: Name): void +getAddress(): Address +setAddress(address: Address): void +toString(): String +Name() +Name(firstName: String, mi: char, lastName: String) +getFirstName(): String +getMi(): char +getLastName(): String +setFirstName(firstName: String): void +setMi(mi: char): void +setLastName(lastName: String): void +getFullName(): String Borrower -loan: Loan Loan Defined in Example 6.7 Name 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 +Borrower() +Borrower(name: Name, address: Address) +getLoan(): Loan +setLoan(loan: Loan): void +toString(): String Loan Person Borrower Address Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 23 Example 12.1 Borrowing Loans, cont. The following is a test program that uses the classes Name, Person, Address, Borrower, and Loan. BorrowLoan Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 Run 24 Example 12.2 The Rational Class Is a subclass of Number class A rational number has a numerator (n) and denominator (d) in the form n/d Cannot have a denominator of 0, but numerator of 0 is fine. Rational numbers are used in exact computations involving fractions Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 25 Example 11.2 The Rational Class 1 java.lang.Number +byteValue(): byte +shortValue(): short +intValue(): int +longVlaue(): long +floatValue(): float +doubleValue():double java.lang.Comparable compareTo(Object): int Rational -numerator: long -denominator: long +Rational() +Rational(numerator: long, denominator: long) +getNumerator(): long +getDenominator(): long +add(secondRational: Rational): Rational +multiply(secondRational: Rational): Rational +subtract(secondRational: Rational): Rational +divide(secondRational: Rational): Rational +toString(): String -gcd(n: long, d: long): long 1 Rational TestRationalClass Add, Subtract, Multiply, Divide Run Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 26