Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 10 Object-Oriented Modeling.
-
date post
19-Dec-2015 -
Category
Documents
-
view
231 -
download
0
Transcript of Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 10 Object-Oriented Modeling.
LiangIntroduction to Java Programmingrevised by Dai-kaiyu1
Chapter 10 Object-Oriented Modeling
Prerequisites for Part II
Chapter 6 Objects and Classes
Chapter 7 Strings
Chapter 8 Inheritance and Polymorphism
Chapter 5 Arrays
Chapter 9 Abstract Classes and Interfaces
Chapter 10 Object-Oriented Modeling
Chapter 11 Getting Started with GUI Programming
Chapter 12 Event-Driven Programming
Chapter 15 Exceptions and Assertions
Chapter 16 Simple Input and Output
You can cover Exceptions and IO after Chapter 8
You can cover GUI after Chapter 8
LiangIntroduction to Java Programmingrevised by Dai-kaiyu2
Objectives To become familiar with the process of program development
(sect102) To the relationship types association aggregation composition
strong inheritance and weak inheritance (sect103) To declare classes to represent the relationships among the classes
(sect103) To design systems by identifying the classes and discovering the
relationships among these classes (sect104) To implement the Rational class and process rational numbers
using this class (sect105) To design classes that follow the class-design guidelines (sect106) To model dynamic behavior using sequence diagrams and
statechart diagrams (sect107 Optional) To know the concept of framework-based programming using Java
API (sect108)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu3
Software Development Process
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
LiangIntroduction to Java Programmingrevised by Dai-kaiyu4
Requirement Specification
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
A formal process that seeks to understand the problem and document in detail what the software system needs to do This phase involves close interaction between users and designers
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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu5
System Analysis
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
Seeks to analyze the business process in terms of data flow and to identify the systemrsquos input and output
Part of the analysis entails modeling the systemrsquos behavior The model is intended to capture the essential elements of the system and to define services to the system
LiangIntroduction to Java Programmingrevised by Dai-kaiyu6
System Design
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
The process of designing the systemrsquos components
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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu7
Implementation
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
The process of translating the system design into programs Separate programs are written for each component and put to work together
This phase requires the use of a programming language like Java The implementation involves coding testing and debugging
LiangIntroduction to Java Programmingrevised by Dai-kaiyu8
Testing
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
Ensures that the code meets the requirements specification and weeds out bugs
An independent team of software engineers not involved in the design and implementation of the project usually conducts such testing
LiangIntroduction to Java Programmingrevised by Dai-kaiyu9
Deployment
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
Deployment makes the project available for use
For a Java applet this means installing it on a Web server for a Java application installing it on the clients computer
LiangIntroduction to Java Programmingrevised by Dai-kaiyu10
Maintenance
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
Maintenance is concerned with changing and improving the product
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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu11
UML- Use-Case
Describe system behavior and show how to capture it in a model
LiangIntroduction to Java Programmingrevised by Dai-kaiyu12
UML- Use-Case
LiangIntroduction to Java Programmingrevised by Dai-kaiyu13
UML-Activity DiagramIt is essentially a flow chart showing flow of control from activity to activity
LiangIntroduction to Java Programmingrevised by Dai-kaiyu14
UML-Class Diagrams
Describe the static view of the system and show how to capture it in a model
LiangIntroduction to Java Programmingrevised by Dai-kaiyu15
Sequence diagramsSequence diagrams describe interactions among objects by depicting the time ordering of method invocations The diagram shows1048707 The objects participating in the interaction1048707 The sequence of messages exchanged
anObject TheClass Class role
Method Invocation
Activation
anotherObject TheOtherClass
Method InvocationanotherMethod()
aMethod()
LiangIntroduction to Java Programmingrevised by Dai-kaiyu16
Sequence diagrams cont
name Name BorrowLoan address Address loan Loan borrower Borrower
setFirstName
setMi
setLastName
setStreet
setCity
setState
setZip
setAnnualInterestRate
setNumOfYears
setLoanAmount
setName
setAddress
setLoan
LiangIntroduction to Java Programmingrevised by Dai-kaiyu17
UML-Collaboration Diagram
LiangIntroduction to Java Programmingrevised by Dai-kaiyu18
UML-Collaboration Diagram
LiangIntroduction to Java Programmingrevised by Dai-kaiyu19
Statechart diagrams
Statechart diagrams describe flow of control of the object
IndicateInitial State
Transition
State1
State2
LiangIntroduction to Java Programmingrevised by Dai-kaiyu20
Statechart diagrams cont
Class Loaded
JVM loads the class for the object
Use the new operator to create the object
Object Created
Invoke the finalize method on the object
Object Destroyed
LiangIntroduction to Java Programmingrevised by Dai-kaiyu21
UML- Component Diagram
A diagram that shows the organization of and the dependencies among a set of components
LiangIntroduction to Java Programmingrevised by Dai-kaiyu22
UML-Deployment DiagramThe deployment diagram shows
Configuration of processing nodes at run-time
Communication links between these nodes
Component instances and objects that reside on them
LiangIntroduction to Java Programmingrevised by Dai-kaiyu23
Relationships among Classes
Association
Aggregation
Composition
Inheritance
LiangIntroduction to Java Programmingrevised by Dai-kaiyu24
Association
Association represents a general binary relationship that describes an
activity between two classes
Student FacultyCourse560Take Teach
03 1Teacher
public class Student
Data fields private Course[]
courseList
Constructors
Methods
public class Course
Data fields private Student[]
classList
private Faculty faculty
Constructors
Methods
public class Faculty
Data fields
private Course[]
courseList
Constructors
Methods
An association is usually represented as a data field in the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu25
Association Between Same Class
Association may exist between objects of the same class
For example a person may have a supervisor
Person Supervisor
1
1
LiangIntroduction to Java Programmingrevised by Dai-kaiyu26
Aggregation and CompositionAggregation 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
Name Address Person
Composition Aggregation
LiangIntroduction to Java Programmingrevised by Dai-kaiyu27
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
Constructors
Methods
public class Address
Data fields
Constructors
Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu28
Inner Classes TranslationIf 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu29
Inheritance
Inheritance models the is-an-extension-of
relationship between two classes
Person Faculty
public class Student extends Person
Data fields
Constructors
Methods
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu30
Weak Inheritance RelationshipA weak is-an-extension-of relationship can be represented using
interfaces For example the weak is-an-extension-of relationship
ldquostudents are comparable based on their gradesrdquo can be represented
by implementing the Comparable interface as follows
Person
Student Comparable
public class Student extends Person
implements Comparable
Data fields Constructors and
Methods
Implement the compareTo method
public int compareTo(Object object)
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu31
Class Design
1 Identify classes for the system
2 Describe attributes and methods in each class
3 Establish relationships among classes
4 Create classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu32
Example 101 Borrowing Loans
NameName BorrowerBorrowerPersonPersonLoanLoan AddressAddress
Loan
Borrower -loan Loan +Borrower() +Borrower(name Name address Address) +getLoan() Loan +setLoan(loan Loan) 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 67
Person -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 -firstName String -mi char -lastName 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu33
Example 101 Borrowing Loans cont
The following is a test program that uses the classes Name Person Address Borrower and Loan
BorrowLoanBorrowLoan RunRun
LiangIntroduction to Java Programmingrevised by Dai-kaiyu34
Example 102 The Rational Class
RationalRational RunRunTestRationalClassTestRationalClass
1
1 Add Subtract Multiply Divide
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
javalangNumber +byteValue() byte +shortValue() short +intValue() int +longVlaue() long +floatValue() float +doubleValue()double
javalangComparable compareTo(Object) int
LiangIntroduction to Java Programmingrevised by Dai-kaiyu35
Class Design Guidelines
Designing a Single Class
Using Modifiers public protected private and static
Using Inheritance or Aggregation
Using Interfaces or Abstract Classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu36
Designing a Class
A class should describe a single entity or a set of similar operations
A single entity with too many responsibilities can be broken into several classes to separate responsibilities
The String class StringBuffer class and StringTokenizer class all deal with strings for example but have different responsibilities
SystemTextStringBuilder in jdk50
LiangIntroduction to Java Programmingrevised by Dai-kaiyu37
Designing a Class cont
Classes are usually designed for use by many different customers To make a class useful in a wide range of applications the class should provide a variety of ways for customization through properties and methods
Provide a public no-arg constructor and override the equals method and the toString method defined in the Object class whenever possible
LiangIntroduction to Java Programmingrevised by Dai-kaiyu38
Designing a Class cont
Follow standard Java programming style and naming conventions
Choose informative names for classes data fields and methods
Always place the data declaration before the constructor and place constructors before methods
Always provide a constructor and initialize variables to avoid programming errors
LiangIntroduction to Java Programmingrevised by Dai-kaiyu39
Using Visibility Modifiers
Each class can present two contracts ndash one for the users of the class and one for the extenders of the class
Make the fields private and accessor methods public if they are intended for the users of the class Make the fields or method protected if they are intended for extenders of the class
A class should also hide methods not intended for client use but only for internal use within the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu40
Using the static Modifier
A property that is shared by all the instances of the class should be declared as a static property
A method that is not dependent on a specific instance should
be declared as a static method
LiangIntroduction to Java Programmingrevised by Dai-kaiyu41
Using Inheritance or Composition cont
public class Cylinder private Circle circle Constructors Methods
If polymorphism is desirable you need to use the inheritance design If you donrsquot care about polymorphism the aggregation design gives more flexibility because the classes are less
dependent using aggregation than using inheritance
public class Cylinder extends Circle Constructors Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu42
Using Interfaces or Abstract Classes
Both interfaces and abstract classes can be used to generalize common features
a strong is-an-extension-of relationship that clearly describes a parent-child relationship should be modeled using classes
since an orange is a fruit their relationship should be modeled using class inheritance
A weak is-an-extension-of relationship can be modeled using interfaces
all strings are comparable so the String class implements the Comparable interface
LiangIntroduction to Java Programmingrevised by Dai-kaiyu43
Using Interfaces or Abstract Classes cont
Interfaces are more flexible than abstract classes because a subclass can extend only one superclass but
implement any number of interfaces However interfaces cannot contain concrete methods
You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface So you can use the interface or its companion class whichever is more convenient
LiangIntroduction to Java Programmingrevised by Dai-kaiyu44
Supplement P Designing Generic Matrix 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu2
Objectives To become familiar with the process of program development
(sect102) To the relationship types association aggregation composition
strong inheritance and weak inheritance (sect103) To declare classes to represent the relationships among the classes
(sect103) To design systems by identifying the classes and discovering the
relationships among these classes (sect104) To implement the Rational class and process rational numbers
using this class (sect105) To design classes that follow the class-design guidelines (sect106) To model dynamic behavior using sequence diagrams and
statechart diagrams (sect107 Optional) To know the concept of framework-based programming using Java
API (sect108)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu3
Software Development Process
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
LiangIntroduction to Java Programmingrevised by Dai-kaiyu4
Requirement Specification
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
A formal process that seeks to understand the problem and document in detail what the software system needs to do This phase involves close interaction between users and designers
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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu5
System Analysis
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
Seeks to analyze the business process in terms of data flow and to identify the systemrsquos input and output
Part of the analysis entails modeling the systemrsquos behavior The model is intended to capture the essential elements of the system and to define services to the system
LiangIntroduction to Java Programmingrevised by Dai-kaiyu6
System Design
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
The process of designing the systemrsquos components
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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu7
Implementation
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
The process of translating the system design into programs Separate programs are written for each component and put to work together
This phase requires the use of a programming language like Java The implementation involves coding testing and debugging
LiangIntroduction to Java Programmingrevised by Dai-kaiyu8
Testing
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
Ensures that the code meets the requirements specification and weeds out bugs
An independent team of software engineers not involved in the design and implementation of the project usually conducts such testing
LiangIntroduction to Java Programmingrevised by Dai-kaiyu9
Deployment
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
Deployment makes the project available for use
For a Java applet this means installing it on a Web server for a Java application installing it on the clients computer
LiangIntroduction to Java Programmingrevised by Dai-kaiyu10
Maintenance
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
Maintenance is concerned with changing and improving the product
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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu11
UML- Use-Case
Describe system behavior and show how to capture it in a model
LiangIntroduction to Java Programmingrevised by Dai-kaiyu12
UML- Use-Case
LiangIntroduction to Java Programmingrevised by Dai-kaiyu13
UML-Activity DiagramIt is essentially a flow chart showing flow of control from activity to activity
LiangIntroduction to Java Programmingrevised by Dai-kaiyu14
UML-Class Diagrams
Describe the static view of the system and show how to capture it in a model
LiangIntroduction to Java Programmingrevised by Dai-kaiyu15
Sequence diagramsSequence diagrams describe interactions among objects by depicting the time ordering of method invocations The diagram shows1048707 The objects participating in the interaction1048707 The sequence of messages exchanged
anObject TheClass Class role
Method Invocation
Activation
anotherObject TheOtherClass
Method InvocationanotherMethod()
aMethod()
LiangIntroduction to Java Programmingrevised by Dai-kaiyu16
Sequence diagrams cont
name Name BorrowLoan address Address loan Loan borrower Borrower
setFirstName
setMi
setLastName
setStreet
setCity
setState
setZip
setAnnualInterestRate
setNumOfYears
setLoanAmount
setName
setAddress
setLoan
LiangIntroduction to Java Programmingrevised by Dai-kaiyu17
UML-Collaboration Diagram
LiangIntroduction to Java Programmingrevised by Dai-kaiyu18
UML-Collaboration Diagram
LiangIntroduction to Java Programmingrevised by Dai-kaiyu19
Statechart diagrams
Statechart diagrams describe flow of control of the object
IndicateInitial State
Transition
State1
State2
LiangIntroduction to Java Programmingrevised by Dai-kaiyu20
Statechart diagrams cont
Class Loaded
JVM loads the class for the object
Use the new operator to create the object
Object Created
Invoke the finalize method on the object
Object Destroyed
LiangIntroduction to Java Programmingrevised by Dai-kaiyu21
UML- Component Diagram
A diagram that shows the organization of and the dependencies among a set of components
LiangIntroduction to Java Programmingrevised by Dai-kaiyu22
UML-Deployment DiagramThe deployment diagram shows
Configuration of processing nodes at run-time
Communication links between these nodes
Component instances and objects that reside on them
LiangIntroduction to Java Programmingrevised by Dai-kaiyu23
Relationships among Classes
Association
Aggregation
Composition
Inheritance
LiangIntroduction to Java Programmingrevised by Dai-kaiyu24
Association
Association represents a general binary relationship that describes an
activity between two classes
Student FacultyCourse560Take Teach
03 1Teacher
public class Student
Data fields private Course[]
courseList
Constructors
Methods
public class Course
Data fields private Student[]
classList
private Faculty faculty
Constructors
Methods
public class Faculty
Data fields
private Course[]
courseList
Constructors
Methods
An association is usually represented as a data field in the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu25
Association Between Same Class
Association may exist between objects of the same class
For example a person may have a supervisor
Person Supervisor
1
1
LiangIntroduction to Java Programmingrevised by Dai-kaiyu26
Aggregation and CompositionAggregation 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
Name Address Person
Composition Aggregation
LiangIntroduction to Java Programmingrevised by Dai-kaiyu27
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
Constructors
Methods
public class Address
Data fields
Constructors
Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu28
Inner Classes TranslationIf 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu29
Inheritance
Inheritance models the is-an-extension-of
relationship between two classes
Person Faculty
public class Student extends Person
Data fields
Constructors
Methods
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu30
Weak Inheritance RelationshipA weak is-an-extension-of relationship can be represented using
interfaces For example the weak is-an-extension-of relationship
ldquostudents are comparable based on their gradesrdquo can be represented
by implementing the Comparable interface as follows
Person
Student Comparable
public class Student extends Person
implements Comparable
Data fields Constructors and
Methods
Implement the compareTo method
public int compareTo(Object object)
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu31
Class Design
1 Identify classes for the system
2 Describe attributes and methods in each class
3 Establish relationships among classes
4 Create classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu32
Example 101 Borrowing Loans
NameName BorrowerBorrowerPersonPersonLoanLoan AddressAddress
Loan
Borrower -loan Loan +Borrower() +Borrower(name Name address Address) +getLoan() Loan +setLoan(loan Loan) 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 67
Person -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 -firstName String -mi char -lastName 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu33
Example 101 Borrowing Loans cont
The following is a test program that uses the classes Name Person Address Borrower and Loan
BorrowLoanBorrowLoan RunRun
LiangIntroduction to Java Programmingrevised by Dai-kaiyu34
Example 102 The Rational Class
RationalRational RunRunTestRationalClassTestRationalClass
1
1 Add Subtract Multiply Divide
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
javalangNumber +byteValue() byte +shortValue() short +intValue() int +longVlaue() long +floatValue() float +doubleValue()double
javalangComparable compareTo(Object) int
LiangIntroduction to Java Programmingrevised by Dai-kaiyu35
Class Design Guidelines
Designing a Single Class
Using Modifiers public protected private and static
Using Inheritance or Aggregation
Using Interfaces or Abstract Classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu36
Designing a Class
A class should describe a single entity or a set of similar operations
A single entity with too many responsibilities can be broken into several classes to separate responsibilities
The String class StringBuffer class and StringTokenizer class all deal with strings for example but have different responsibilities
SystemTextStringBuilder in jdk50
LiangIntroduction to Java Programmingrevised by Dai-kaiyu37
Designing a Class cont
Classes are usually designed for use by many different customers To make a class useful in a wide range of applications the class should provide a variety of ways for customization through properties and methods
Provide a public no-arg constructor and override the equals method and the toString method defined in the Object class whenever possible
LiangIntroduction to Java Programmingrevised by Dai-kaiyu38
Designing a Class cont
Follow standard Java programming style and naming conventions
Choose informative names for classes data fields and methods
Always place the data declaration before the constructor and place constructors before methods
Always provide a constructor and initialize variables to avoid programming errors
LiangIntroduction to Java Programmingrevised by Dai-kaiyu39
Using Visibility Modifiers
Each class can present two contracts ndash one for the users of the class and one for the extenders of the class
Make the fields private and accessor methods public if they are intended for the users of the class Make the fields or method protected if they are intended for extenders of the class
A class should also hide methods not intended for client use but only for internal use within the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu40
Using the static Modifier
A property that is shared by all the instances of the class should be declared as a static property
A method that is not dependent on a specific instance should
be declared as a static method
LiangIntroduction to Java Programmingrevised by Dai-kaiyu41
Using Inheritance or Composition cont
public class Cylinder private Circle circle Constructors Methods
If polymorphism is desirable you need to use the inheritance design If you donrsquot care about polymorphism the aggregation design gives more flexibility because the classes are less
dependent using aggregation than using inheritance
public class Cylinder extends Circle Constructors Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu42
Using Interfaces or Abstract Classes
Both interfaces and abstract classes can be used to generalize common features
a strong is-an-extension-of relationship that clearly describes a parent-child relationship should be modeled using classes
since an orange is a fruit their relationship should be modeled using class inheritance
A weak is-an-extension-of relationship can be modeled using interfaces
all strings are comparable so the String class implements the Comparable interface
LiangIntroduction to Java Programmingrevised by Dai-kaiyu43
Using Interfaces or Abstract Classes cont
Interfaces are more flexible than abstract classes because a subclass can extend only one superclass but
implement any number of interfaces However interfaces cannot contain concrete methods
You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface So you can use the interface or its companion class whichever is more convenient
LiangIntroduction to Java Programmingrevised by Dai-kaiyu44
Supplement P Designing Generic Matrix 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu3
Software Development Process
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
LiangIntroduction to Java Programmingrevised by Dai-kaiyu4
Requirement Specification
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
A formal process that seeks to understand the problem and document in detail what the software system needs to do This phase involves close interaction between users and designers
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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu5
System Analysis
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
Seeks to analyze the business process in terms of data flow and to identify the systemrsquos input and output
Part of the analysis entails modeling the systemrsquos behavior The model is intended to capture the essential elements of the system and to define services to the system
LiangIntroduction to Java Programmingrevised by Dai-kaiyu6
System Design
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
The process of designing the systemrsquos components
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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu7
Implementation
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
The process of translating the system design into programs Separate programs are written for each component and put to work together
This phase requires the use of a programming language like Java The implementation involves coding testing and debugging
LiangIntroduction to Java Programmingrevised by Dai-kaiyu8
Testing
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
Ensures that the code meets the requirements specification and weeds out bugs
An independent team of software engineers not involved in the design and implementation of the project usually conducts such testing
LiangIntroduction to Java Programmingrevised by Dai-kaiyu9
Deployment
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
Deployment makes the project available for use
For a Java applet this means installing it on a Web server for a Java application installing it on the clients computer
LiangIntroduction to Java Programmingrevised by Dai-kaiyu10
Maintenance
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
Maintenance is concerned with changing and improving the product
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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu11
UML- Use-Case
Describe system behavior and show how to capture it in a model
LiangIntroduction to Java Programmingrevised by Dai-kaiyu12
UML- Use-Case
LiangIntroduction to Java Programmingrevised by Dai-kaiyu13
UML-Activity DiagramIt is essentially a flow chart showing flow of control from activity to activity
LiangIntroduction to Java Programmingrevised by Dai-kaiyu14
UML-Class Diagrams
Describe the static view of the system and show how to capture it in a model
LiangIntroduction to Java Programmingrevised by Dai-kaiyu15
Sequence diagramsSequence diagrams describe interactions among objects by depicting the time ordering of method invocations The diagram shows1048707 The objects participating in the interaction1048707 The sequence of messages exchanged
anObject TheClass Class role
Method Invocation
Activation
anotherObject TheOtherClass
Method InvocationanotherMethod()
aMethod()
LiangIntroduction to Java Programmingrevised by Dai-kaiyu16
Sequence diagrams cont
name Name BorrowLoan address Address loan Loan borrower Borrower
setFirstName
setMi
setLastName
setStreet
setCity
setState
setZip
setAnnualInterestRate
setNumOfYears
setLoanAmount
setName
setAddress
setLoan
LiangIntroduction to Java Programmingrevised by Dai-kaiyu17
UML-Collaboration Diagram
LiangIntroduction to Java Programmingrevised by Dai-kaiyu18
UML-Collaboration Diagram
LiangIntroduction to Java Programmingrevised by Dai-kaiyu19
Statechart diagrams
Statechart diagrams describe flow of control of the object
IndicateInitial State
Transition
State1
State2
LiangIntroduction to Java Programmingrevised by Dai-kaiyu20
Statechart diagrams cont
Class Loaded
JVM loads the class for the object
Use the new operator to create the object
Object Created
Invoke the finalize method on the object
Object Destroyed
LiangIntroduction to Java Programmingrevised by Dai-kaiyu21
UML- Component Diagram
A diagram that shows the organization of and the dependencies among a set of components
LiangIntroduction to Java Programmingrevised by Dai-kaiyu22
UML-Deployment DiagramThe deployment diagram shows
Configuration of processing nodes at run-time
Communication links between these nodes
Component instances and objects that reside on them
LiangIntroduction to Java Programmingrevised by Dai-kaiyu23
Relationships among Classes
Association
Aggregation
Composition
Inheritance
LiangIntroduction to Java Programmingrevised by Dai-kaiyu24
Association
Association represents a general binary relationship that describes an
activity between two classes
Student FacultyCourse560Take Teach
03 1Teacher
public class Student
Data fields private Course[]
courseList
Constructors
Methods
public class Course
Data fields private Student[]
classList
private Faculty faculty
Constructors
Methods
public class Faculty
Data fields
private Course[]
courseList
Constructors
Methods
An association is usually represented as a data field in the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu25
Association Between Same Class
Association may exist between objects of the same class
For example a person may have a supervisor
Person Supervisor
1
1
LiangIntroduction to Java Programmingrevised by Dai-kaiyu26
Aggregation and CompositionAggregation 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
Name Address Person
Composition Aggregation
LiangIntroduction to Java Programmingrevised by Dai-kaiyu27
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
Constructors
Methods
public class Address
Data fields
Constructors
Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu28
Inner Classes TranslationIf 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu29
Inheritance
Inheritance models the is-an-extension-of
relationship between two classes
Person Faculty
public class Student extends Person
Data fields
Constructors
Methods
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu30
Weak Inheritance RelationshipA weak is-an-extension-of relationship can be represented using
interfaces For example the weak is-an-extension-of relationship
ldquostudents are comparable based on their gradesrdquo can be represented
by implementing the Comparable interface as follows
Person
Student Comparable
public class Student extends Person
implements Comparable
Data fields Constructors and
Methods
Implement the compareTo method
public int compareTo(Object object)
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu31
Class Design
1 Identify classes for the system
2 Describe attributes and methods in each class
3 Establish relationships among classes
4 Create classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu32
Example 101 Borrowing Loans
NameName BorrowerBorrowerPersonPersonLoanLoan AddressAddress
Loan
Borrower -loan Loan +Borrower() +Borrower(name Name address Address) +getLoan() Loan +setLoan(loan Loan) 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 67
Person -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 -firstName String -mi char -lastName 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu33
Example 101 Borrowing Loans cont
The following is a test program that uses the classes Name Person Address Borrower and Loan
BorrowLoanBorrowLoan RunRun
LiangIntroduction to Java Programmingrevised by Dai-kaiyu34
Example 102 The Rational Class
RationalRational RunRunTestRationalClassTestRationalClass
1
1 Add Subtract Multiply Divide
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
javalangNumber +byteValue() byte +shortValue() short +intValue() int +longVlaue() long +floatValue() float +doubleValue()double
javalangComparable compareTo(Object) int
LiangIntroduction to Java Programmingrevised by Dai-kaiyu35
Class Design Guidelines
Designing a Single Class
Using Modifiers public protected private and static
Using Inheritance or Aggregation
Using Interfaces or Abstract Classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu36
Designing a Class
A class should describe a single entity or a set of similar operations
A single entity with too many responsibilities can be broken into several classes to separate responsibilities
The String class StringBuffer class and StringTokenizer class all deal with strings for example but have different responsibilities
SystemTextStringBuilder in jdk50
LiangIntroduction to Java Programmingrevised by Dai-kaiyu37
Designing a Class cont
Classes are usually designed for use by many different customers To make a class useful in a wide range of applications the class should provide a variety of ways for customization through properties and methods
Provide a public no-arg constructor and override the equals method and the toString method defined in the Object class whenever possible
LiangIntroduction to Java Programmingrevised by Dai-kaiyu38
Designing a Class cont
Follow standard Java programming style and naming conventions
Choose informative names for classes data fields and methods
Always place the data declaration before the constructor and place constructors before methods
Always provide a constructor and initialize variables to avoid programming errors
LiangIntroduction to Java Programmingrevised by Dai-kaiyu39
Using Visibility Modifiers
Each class can present two contracts ndash one for the users of the class and one for the extenders of the class
Make the fields private and accessor methods public if they are intended for the users of the class Make the fields or method protected if they are intended for extenders of the class
A class should also hide methods not intended for client use but only for internal use within the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu40
Using the static Modifier
A property that is shared by all the instances of the class should be declared as a static property
A method that is not dependent on a specific instance should
be declared as a static method
LiangIntroduction to Java Programmingrevised by Dai-kaiyu41
Using Inheritance or Composition cont
public class Cylinder private Circle circle Constructors Methods
If polymorphism is desirable you need to use the inheritance design If you donrsquot care about polymorphism the aggregation design gives more flexibility because the classes are less
dependent using aggregation than using inheritance
public class Cylinder extends Circle Constructors Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu42
Using Interfaces or Abstract Classes
Both interfaces and abstract classes can be used to generalize common features
a strong is-an-extension-of relationship that clearly describes a parent-child relationship should be modeled using classes
since an orange is a fruit their relationship should be modeled using class inheritance
A weak is-an-extension-of relationship can be modeled using interfaces
all strings are comparable so the String class implements the Comparable interface
LiangIntroduction to Java Programmingrevised by Dai-kaiyu43
Using Interfaces or Abstract Classes cont
Interfaces are more flexible than abstract classes because a subclass can extend only one superclass but
implement any number of interfaces However interfaces cannot contain concrete methods
You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface So you can use the interface or its companion class whichever is more convenient
LiangIntroduction to Java Programmingrevised by Dai-kaiyu44
Supplement P Designing Generic Matrix 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu4
Requirement Specification
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
A formal process that seeks to understand the problem and document in detail what the software system needs to do This phase involves close interaction between users and designers
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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu5
System Analysis
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
Seeks to analyze the business process in terms of data flow and to identify the systemrsquos input and output
Part of the analysis entails modeling the systemrsquos behavior The model is intended to capture the essential elements of the system and to define services to the system
LiangIntroduction to Java Programmingrevised by Dai-kaiyu6
System Design
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
The process of designing the systemrsquos components
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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu7
Implementation
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
The process of translating the system design into programs Separate programs are written for each component and put to work together
This phase requires the use of a programming language like Java The implementation involves coding testing and debugging
LiangIntroduction to Java Programmingrevised by Dai-kaiyu8
Testing
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
Ensures that the code meets the requirements specification and weeds out bugs
An independent team of software engineers not involved in the design and implementation of the project usually conducts such testing
LiangIntroduction to Java Programmingrevised by Dai-kaiyu9
Deployment
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
Deployment makes the project available for use
For a Java applet this means installing it on a Web server for a Java application installing it on the clients computer
LiangIntroduction to Java Programmingrevised by Dai-kaiyu10
Maintenance
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
Maintenance is concerned with changing and improving the product
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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu11
UML- Use-Case
Describe system behavior and show how to capture it in a model
LiangIntroduction to Java Programmingrevised by Dai-kaiyu12
UML- Use-Case
LiangIntroduction to Java Programmingrevised by Dai-kaiyu13
UML-Activity DiagramIt is essentially a flow chart showing flow of control from activity to activity
LiangIntroduction to Java Programmingrevised by Dai-kaiyu14
UML-Class Diagrams
Describe the static view of the system and show how to capture it in a model
LiangIntroduction to Java Programmingrevised by Dai-kaiyu15
Sequence diagramsSequence diagrams describe interactions among objects by depicting the time ordering of method invocations The diagram shows1048707 The objects participating in the interaction1048707 The sequence of messages exchanged
anObject TheClass Class role
Method Invocation
Activation
anotherObject TheOtherClass
Method InvocationanotherMethod()
aMethod()
LiangIntroduction to Java Programmingrevised by Dai-kaiyu16
Sequence diagrams cont
name Name BorrowLoan address Address loan Loan borrower Borrower
setFirstName
setMi
setLastName
setStreet
setCity
setState
setZip
setAnnualInterestRate
setNumOfYears
setLoanAmount
setName
setAddress
setLoan
LiangIntroduction to Java Programmingrevised by Dai-kaiyu17
UML-Collaboration Diagram
LiangIntroduction to Java Programmingrevised by Dai-kaiyu18
UML-Collaboration Diagram
LiangIntroduction to Java Programmingrevised by Dai-kaiyu19
Statechart diagrams
Statechart diagrams describe flow of control of the object
IndicateInitial State
Transition
State1
State2
LiangIntroduction to Java Programmingrevised by Dai-kaiyu20
Statechart diagrams cont
Class Loaded
JVM loads the class for the object
Use the new operator to create the object
Object Created
Invoke the finalize method on the object
Object Destroyed
LiangIntroduction to Java Programmingrevised by Dai-kaiyu21
UML- Component Diagram
A diagram that shows the organization of and the dependencies among a set of components
LiangIntroduction to Java Programmingrevised by Dai-kaiyu22
UML-Deployment DiagramThe deployment diagram shows
Configuration of processing nodes at run-time
Communication links between these nodes
Component instances and objects that reside on them
LiangIntroduction to Java Programmingrevised by Dai-kaiyu23
Relationships among Classes
Association
Aggregation
Composition
Inheritance
LiangIntroduction to Java Programmingrevised by Dai-kaiyu24
Association
Association represents a general binary relationship that describes an
activity between two classes
Student FacultyCourse560Take Teach
03 1Teacher
public class Student
Data fields private Course[]
courseList
Constructors
Methods
public class Course
Data fields private Student[]
classList
private Faculty faculty
Constructors
Methods
public class Faculty
Data fields
private Course[]
courseList
Constructors
Methods
An association is usually represented as a data field in the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu25
Association Between Same Class
Association may exist between objects of the same class
For example a person may have a supervisor
Person Supervisor
1
1
LiangIntroduction to Java Programmingrevised by Dai-kaiyu26
Aggregation and CompositionAggregation 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
Name Address Person
Composition Aggregation
LiangIntroduction to Java Programmingrevised by Dai-kaiyu27
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
Constructors
Methods
public class Address
Data fields
Constructors
Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu28
Inner Classes TranslationIf 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu29
Inheritance
Inheritance models the is-an-extension-of
relationship between two classes
Person Faculty
public class Student extends Person
Data fields
Constructors
Methods
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu30
Weak Inheritance RelationshipA weak is-an-extension-of relationship can be represented using
interfaces For example the weak is-an-extension-of relationship
ldquostudents are comparable based on their gradesrdquo can be represented
by implementing the Comparable interface as follows
Person
Student Comparable
public class Student extends Person
implements Comparable
Data fields Constructors and
Methods
Implement the compareTo method
public int compareTo(Object object)
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu31
Class Design
1 Identify classes for the system
2 Describe attributes and methods in each class
3 Establish relationships among classes
4 Create classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu32
Example 101 Borrowing Loans
NameName BorrowerBorrowerPersonPersonLoanLoan AddressAddress
Loan
Borrower -loan Loan +Borrower() +Borrower(name Name address Address) +getLoan() Loan +setLoan(loan Loan) 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 67
Person -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 -firstName String -mi char -lastName 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu33
Example 101 Borrowing Loans cont
The following is a test program that uses the classes Name Person Address Borrower and Loan
BorrowLoanBorrowLoan RunRun
LiangIntroduction to Java Programmingrevised by Dai-kaiyu34
Example 102 The Rational Class
RationalRational RunRunTestRationalClassTestRationalClass
1
1 Add Subtract Multiply Divide
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
javalangNumber +byteValue() byte +shortValue() short +intValue() int +longVlaue() long +floatValue() float +doubleValue()double
javalangComparable compareTo(Object) int
LiangIntroduction to Java Programmingrevised by Dai-kaiyu35
Class Design Guidelines
Designing a Single Class
Using Modifiers public protected private and static
Using Inheritance or Aggregation
Using Interfaces or Abstract Classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu36
Designing a Class
A class should describe a single entity or a set of similar operations
A single entity with too many responsibilities can be broken into several classes to separate responsibilities
The String class StringBuffer class and StringTokenizer class all deal with strings for example but have different responsibilities
SystemTextStringBuilder in jdk50
LiangIntroduction to Java Programmingrevised by Dai-kaiyu37
Designing a Class cont
Classes are usually designed for use by many different customers To make a class useful in a wide range of applications the class should provide a variety of ways for customization through properties and methods
Provide a public no-arg constructor and override the equals method and the toString method defined in the Object class whenever possible
LiangIntroduction to Java Programmingrevised by Dai-kaiyu38
Designing a Class cont
Follow standard Java programming style and naming conventions
Choose informative names for classes data fields and methods
Always place the data declaration before the constructor and place constructors before methods
Always provide a constructor and initialize variables to avoid programming errors
LiangIntroduction to Java Programmingrevised by Dai-kaiyu39
Using Visibility Modifiers
Each class can present two contracts ndash one for the users of the class and one for the extenders of the class
Make the fields private and accessor methods public if they are intended for the users of the class Make the fields or method protected if they are intended for extenders of the class
A class should also hide methods not intended for client use but only for internal use within the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu40
Using the static Modifier
A property that is shared by all the instances of the class should be declared as a static property
A method that is not dependent on a specific instance should
be declared as a static method
LiangIntroduction to Java Programmingrevised by Dai-kaiyu41
Using Inheritance or Composition cont
public class Cylinder private Circle circle Constructors Methods
If polymorphism is desirable you need to use the inheritance design If you donrsquot care about polymorphism the aggregation design gives more flexibility because the classes are less
dependent using aggregation than using inheritance
public class Cylinder extends Circle Constructors Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu42
Using Interfaces or Abstract Classes
Both interfaces and abstract classes can be used to generalize common features
a strong is-an-extension-of relationship that clearly describes a parent-child relationship should be modeled using classes
since an orange is a fruit their relationship should be modeled using class inheritance
A weak is-an-extension-of relationship can be modeled using interfaces
all strings are comparable so the String class implements the Comparable interface
LiangIntroduction to Java Programmingrevised by Dai-kaiyu43
Using Interfaces or Abstract Classes cont
Interfaces are more flexible than abstract classes because a subclass can extend only one superclass but
implement any number of interfaces However interfaces cannot contain concrete methods
You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface So you can use the interface or its companion class whichever is more convenient
LiangIntroduction to Java Programmingrevised by Dai-kaiyu44
Supplement P Designing Generic Matrix 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu5
System Analysis
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
Seeks to analyze the business process in terms of data flow and to identify the systemrsquos input and output
Part of the analysis entails modeling the systemrsquos behavior The model is intended to capture the essential elements of the system and to define services to the system
LiangIntroduction to Java Programmingrevised by Dai-kaiyu6
System Design
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
The process of designing the systemrsquos components
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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu7
Implementation
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
The process of translating the system design into programs Separate programs are written for each component and put to work together
This phase requires the use of a programming language like Java The implementation involves coding testing and debugging
LiangIntroduction to Java Programmingrevised by Dai-kaiyu8
Testing
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
Ensures that the code meets the requirements specification and weeds out bugs
An independent team of software engineers not involved in the design and implementation of the project usually conducts such testing
LiangIntroduction to Java Programmingrevised by Dai-kaiyu9
Deployment
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
Deployment makes the project available for use
For a Java applet this means installing it on a Web server for a Java application installing it on the clients computer
LiangIntroduction to Java Programmingrevised by Dai-kaiyu10
Maintenance
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
Maintenance is concerned with changing and improving the product
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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu11
UML- Use-Case
Describe system behavior and show how to capture it in a model
LiangIntroduction to Java Programmingrevised by Dai-kaiyu12
UML- Use-Case
LiangIntroduction to Java Programmingrevised by Dai-kaiyu13
UML-Activity DiagramIt is essentially a flow chart showing flow of control from activity to activity
LiangIntroduction to Java Programmingrevised by Dai-kaiyu14
UML-Class Diagrams
Describe the static view of the system and show how to capture it in a model
LiangIntroduction to Java Programmingrevised by Dai-kaiyu15
Sequence diagramsSequence diagrams describe interactions among objects by depicting the time ordering of method invocations The diagram shows1048707 The objects participating in the interaction1048707 The sequence of messages exchanged
anObject TheClass Class role
Method Invocation
Activation
anotherObject TheOtherClass
Method InvocationanotherMethod()
aMethod()
LiangIntroduction to Java Programmingrevised by Dai-kaiyu16
Sequence diagrams cont
name Name BorrowLoan address Address loan Loan borrower Borrower
setFirstName
setMi
setLastName
setStreet
setCity
setState
setZip
setAnnualInterestRate
setNumOfYears
setLoanAmount
setName
setAddress
setLoan
LiangIntroduction to Java Programmingrevised by Dai-kaiyu17
UML-Collaboration Diagram
LiangIntroduction to Java Programmingrevised by Dai-kaiyu18
UML-Collaboration Diagram
LiangIntroduction to Java Programmingrevised by Dai-kaiyu19
Statechart diagrams
Statechart diagrams describe flow of control of the object
IndicateInitial State
Transition
State1
State2
LiangIntroduction to Java Programmingrevised by Dai-kaiyu20
Statechart diagrams cont
Class Loaded
JVM loads the class for the object
Use the new operator to create the object
Object Created
Invoke the finalize method on the object
Object Destroyed
LiangIntroduction to Java Programmingrevised by Dai-kaiyu21
UML- Component Diagram
A diagram that shows the organization of and the dependencies among a set of components
LiangIntroduction to Java Programmingrevised by Dai-kaiyu22
UML-Deployment DiagramThe deployment diagram shows
Configuration of processing nodes at run-time
Communication links between these nodes
Component instances and objects that reside on them
LiangIntroduction to Java Programmingrevised by Dai-kaiyu23
Relationships among Classes
Association
Aggregation
Composition
Inheritance
LiangIntroduction to Java Programmingrevised by Dai-kaiyu24
Association
Association represents a general binary relationship that describes an
activity between two classes
Student FacultyCourse560Take Teach
03 1Teacher
public class Student
Data fields private Course[]
courseList
Constructors
Methods
public class Course
Data fields private Student[]
classList
private Faculty faculty
Constructors
Methods
public class Faculty
Data fields
private Course[]
courseList
Constructors
Methods
An association is usually represented as a data field in the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu25
Association Between Same Class
Association may exist between objects of the same class
For example a person may have a supervisor
Person Supervisor
1
1
LiangIntroduction to Java Programmingrevised by Dai-kaiyu26
Aggregation and CompositionAggregation 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
Name Address Person
Composition Aggregation
LiangIntroduction to Java Programmingrevised by Dai-kaiyu27
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
Constructors
Methods
public class Address
Data fields
Constructors
Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu28
Inner Classes TranslationIf 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu29
Inheritance
Inheritance models the is-an-extension-of
relationship between two classes
Person Faculty
public class Student extends Person
Data fields
Constructors
Methods
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu30
Weak Inheritance RelationshipA weak is-an-extension-of relationship can be represented using
interfaces For example the weak is-an-extension-of relationship
ldquostudents are comparable based on their gradesrdquo can be represented
by implementing the Comparable interface as follows
Person
Student Comparable
public class Student extends Person
implements Comparable
Data fields Constructors and
Methods
Implement the compareTo method
public int compareTo(Object object)
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu31
Class Design
1 Identify classes for the system
2 Describe attributes and methods in each class
3 Establish relationships among classes
4 Create classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu32
Example 101 Borrowing Loans
NameName BorrowerBorrowerPersonPersonLoanLoan AddressAddress
Loan
Borrower -loan Loan +Borrower() +Borrower(name Name address Address) +getLoan() Loan +setLoan(loan Loan) 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 67
Person -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 -firstName String -mi char -lastName 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu33
Example 101 Borrowing Loans cont
The following is a test program that uses the classes Name Person Address Borrower and Loan
BorrowLoanBorrowLoan RunRun
LiangIntroduction to Java Programmingrevised by Dai-kaiyu34
Example 102 The Rational Class
RationalRational RunRunTestRationalClassTestRationalClass
1
1 Add Subtract Multiply Divide
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
javalangNumber +byteValue() byte +shortValue() short +intValue() int +longVlaue() long +floatValue() float +doubleValue()double
javalangComparable compareTo(Object) int
LiangIntroduction to Java Programmingrevised by Dai-kaiyu35
Class Design Guidelines
Designing a Single Class
Using Modifiers public protected private and static
Using Inheritance or Aggregation
Using Interfaces or Abstract Classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu36
Designing a Class
A class should describe a single entity or a set of similar operations
A single entity with too many responsibilities can be broken into several classes to separate responsibilities
The String class StringBuffer class and StringTokenizer class all deal with strings for example but have different responsibilities
SystemTextStringBuilder in jdk50
LiangIntroduction to Java Programmingrevised by Dai-kaiyu37
Designing a Class cont
Classes are usually designed for use by many different customers To make a class useful in a wide range of applications the class should provide a variety of ways for customization through properties and methods
Provide a public no-arg constructor and override the equals method and the toString method defined in the Object class whenever possible
LiangIntroduction to Java Programmingrevised by Dai-kaiyu38
Designing a Class cont
Follow standard Java programming style and naming conventions
Choose informative names for classes data fields and methods
Always place the data declaration before the constructor and place constructors before methods
Always provide a constructor and initialize variables to avoid programming errors
LiangIntroduction to Java Programmingrevised by Dai-kaiyu39
Using Visibility Modifiers
Each class can present two contracts ndash one for the users of the class and one for the extenders of the class
Make the fields private and accessor methods public if they are intended for the users of the class Make the fields or method protected if they are intended for extenders of the class
A class should also hide methods not intended for client use but only for internal use within the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu40
Using the static Modifier
A property that is shared by all the instances of the class should be declared as a static property
A method that is not dependent on a specific instance should
be declared as a static method
LiangIntroduction to Java Programmingrevised by Dai-kaiyu41
Using Inheritance or Composition cont
public class Cylinder private Circle circle Constructors Methods
If polymorphism is desirable you need to use the inheritance design If you donrsquot care about polymorphism the aggregation design gives more flexibility because the classes are less
dependent using aggregation than using inheritance
public class Cylinder extends Circle Constructors Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu42
Using Interfaces or Abstract Classes
Both interfaces and abstract classes can be used to generalize common features
a strong is-an-extension-of relationship that clearly describes a parent-child relationship should be modeled using classes
since an orange is a fruit their relationship should be modeled using class inheritance
A weak is-an-extension-of relationship can be modeled using interfaces
all strings are comparable so the String class implements the Comparable interface
LiangIntroduction to Java Programmingrevised by Dai-kaiyu43
Using Interfaces or Abstract Classes cont
Interfaces are more flexible than abstract classes because a subclass can extend only one superclass but
implement any number of interfaces However interfaces cannot contain concrete methods
You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface So you can use the interface or its companion class whichever is more convenient
LiangIntroduction to Java Programmingrevised by Dai-kaiyu44
Supplement P Designing Generic Matrix 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu6
System Design
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
The process of designing the systemrsquos components
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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu7
Implementation
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
The process of translating the system design into programs Separate programs are written for each component and put to work together
This phase requires the use of a programming language like Java The implementation involves coding testing and debugging
LiangIntroduction to Java Programmingrevised by Dai-kaiyu8
Testing
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
Ensures that the code meets the requirements specification and weeds out bugs
An independent team of software engineers not involved in the design and implementation of the project usually conducts such testing
LiangIntroduction to Java Programmingrevised by Dai-kaiyu9
Deployment
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
Deployment makes the project available for use
For a Java applet this means installing it on a Web server for a Java application installing it on the clients computer
LiangIntroduction to Java Programmingrevised by Dai-kaiyu10
Maintenance
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
Maintenance is concerned with changing and improving the product
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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu11
UML- Use-Case
Describe system behavior and show how to capture it in a model
LiangIntroduction to Java Programmingrevised by Dai-kaiyu12
UML- Use-Case
LiangIntroduction to Java Programmingrevised by Dai-kaiyu13
UML-Activity DiagramIt is essentially a flow chart showing flow of control from activity to activity
LiangIntroduction to Java Programmingrevised by Dai-kaiyu14
UML-Class Diagrams
Describe the static view of the system and show how to capture it in a model
LiangIntroduction to Java Programmingrevised by Dai-kaiyu15
Sequence diagramsSequence diagrams describe interactions among objects by depicting the time ordering of method invocations The diagram shows1048707 The objects participating in the interaction1048707 The sequence of messages exchanged
anObject TheClass Class role
Method Invocation
Activation
anotherObject TheOtherClass
Method InvocationanotherMethod()
aMethod()
LiangIntroduction to Java Programmingrevised by Dai-kaiyu16
Sequence diagrams cont
name Name BorrowLoan address Address loan Loan borrower Borrower
setFirstName
setMi
setLastName
setStreet
setCity
setState
setZip
setAnnualInterestRate
setNumOfYears
setLoanAmount
setName
setAddress
setLoan
LiangIntroduction to Java Programmingrevised by Dai-kaiyu17
UML-Collaboration Diagram
LiangIntroduction to Java Programmingrevised by Dai-kaiyu18
UML-Collaboration Diagram
LiangIntroduction to Java Programmingrevised by Dai-kaiyu19
Statechart diagrams
Statechart diagrams describe flow of control of the object
IndicateInitial State
Transition
State1
State2
LiangIntroduction to Java Programmingrevised by Dai-kaiyu20
Statechart diagrams cont
Class Loaded
JVM loads the class for the object
Use the new operator to create the object
Object Created
Invoke the finalize method on the object
Object Destroyed
LiangIntroduction to Java Programmingrevised by Dai-kaiyu21
UML- Component Diagram
A diagram that shows the organization of and the dependencies among a set of components
LiangIntroduction to Java Programmingrevised by Dai-kaiyu22
UML-Deployment DiagramThe deployment diagram shows
Configuration of processing nodes at run-time
Communication links between these nodes
Component instances and objects that reside on them
LiangIntroduction to Java Programmingrevised by Dai-kaiyu23
Relationships among Classes
Association
Aggregation
Composition
Inheritance
LiangIntroduction to Java Programmingrevised by Dai-kaiyu24
Association
Association represents a general binary relationship that describes an
activity between two classes
Student FacultyCourse560Take Teach
03 1Teacher
public class Student
Data fields private Course[]
courseList
Constructors
Methods
public class Course
Data fields private Student[]
classList
private Faculty faculty
Constructors
Methods
public class Faculty
Data fields
private Course[]
courseList
Constructors
Methods
An association is usually represented as a data field in the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu25
Association Between Same Class
Association may exist between objects of the same class
For example a person may have a supervisor
Person Supervisor
1
1
LiangIntroduction to Java Programmingrevised by Dai-kaiyu26
Aggregation and CompositionAggregation 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
Name Address Person
Composition Aggregation
LiangIntroduction to Java Programmingrevised by Dai-kaiyu27
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
Constructors
Methods
public class Address
Data fields
Constructors
Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu28
Inner Classes TranslationIf 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu29
Inheritance
Inheritance models the is-an-extension-of
relationship between two classes
Person Faculty
public class Student extends Person
Data fields
Constructors
Methods
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu30
Weak Inheritance RelationshipA weak is-an-extension-of relationship can be represented using
interfaces For example the weak is-an-extension-of relationship
ldquostudents are comparable based on their gradesrdquo can be represented
by implementing the Comparable interface as follows
Person
Student Comparable
public class Student extends Person
implements Comparable
Data fields Constructors and
Methods
Implement the compareTo method
public int compareTo(Object object)
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu31
Class Design
1 Identify classes for the system
2 Describe attributes and methods in each class
3 Establish relationships among classes
4 Create classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu32
Example 101 Borrowing Loans
NameName BorrowerBorrowerPersonPersonLoanLoan AddressAddress
Loan
Borrower -loan Loan +Borrower() +Borrower(name Name address Address) +getLoan() Loan +setLoan(loan Loan) 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 67
Person -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 -firstName String -mi char -lastName 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu33
Example 101 Borrowing Loans cont
The following is a test program that uses the classes Name Person Address Borrower and Loan
BorrowLoanBorrowLoan RunRun
LiangIntroduction to Java Programmingrevised by Dai-kaiyu34
Example 102 The Rational Class
RationalRational RunRunTestRationalClassTestRationalClass
1
1 Add Subtract Multiply Divide
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
javalangNumber +byteValue() byte +shortValue() short +intValue() int +longVlaue() long +floatValue() float +doubleValue()double
javalangComparable compareTo(Object) int
LiangIntroduction to Java Programmingrevised by Dai-kaiyu35
Class Design Guidelines
Designing a Single Class
Using Modifiers public protected private and static
Using Inheritance or Aggregation
Using Interfaces or Abstract Classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu36
Designing a Class
A class should describe a single entity or a set of similar operations
A single entity with too many responsibilities can be broken into several classes to separate responsibilities
The String class StringBuffer class and StringTokenizer class all deal with strings for example but have different responsibilities
SystemTextStringBuilder in jdk50
LiangIntroduction to Java Programmingrevised by Dai-kaiyu37
Designing a Class cont
Classes are usually designed for use by many different customers To make a class useful in a wide range of applications the class should provide a variety of ways for customization through properties and methods
Provide a public no-arg constructor and override the equals method and the toString method defined in the Object class whenever possible
LiangIntroduction to Java Programmingrevised by Dai-kaiyu38
Designing a Class cont
Follow standard Java programming style and naming conventions
Choose informative names for classes data fields and methods
Always place the data declaration before the constructor and place constructors before methods
Always provide a constructor and initialize variables to avoid programming errors
LiangIntroduction to Java Programmingrevised by Dai-kaiyu39
Using Visibility Modifiers
Each class can present two contracts ndash one for the users of the class and one for the extenders of the class
Make the fields private and accessor methods public if they are intended for the users of the class Make the fields or method protected if they are intended for extenders of the class
A class should also hide methods not intended for client use but only for internal use within the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu40
Using the static Modifier
A property that is shared by all the instances of the class should be declared as a static property
A method that is not dependent on a specific instance should
be declared as a static method
LiangIntroduction to Java Programmingrevised by Dai-kaiyu41
Using Inheritance or Composition cont
public class Cylinder private Circle circle Constructors Methods
If polymorphism is desirable you need to use the inheritance design If you donrsquot care about polymorphism the aggregation design gives more flexibility because the classes are less
dependent using aggregation than using inheritance
public class Cylinder extends Circle Constructors Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu42
Using Interfaces or Abstract Classes
Both interfaces and abstract classes can be used to generalize common features
a strong is-an-extension-of relationship that clearly describes a parent-child relationship should be modeled using classes
since an orange is a fruit their relationship should be modeled using class inheritance
A weak is-an-extension-of relationship can be modeled using interfaces
all strings are comparable so the String class implements the Comparable interface
LiangIntroduction to Java Programmingrevised by Dai-kaiyu43
Using Interfaces or Abstract Classes cont
Interfaces are more flexible than abstract classes because a subclass can extend only one superclass but
implement any number of interfaces However interfaces cannot contain concrete methods
You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface So you can use the interface or its companion class whichever is more convenient
LiangIntroduction to Java Programmingrevised by Dai-kaiyu44
Supplement P Designing Generic Matrix 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu7
Implementation
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
The process of translating the system design into programs Separate programs are written for each component and put to work together
This phase requires the use of a programming language like Java The implementation involves coding testing and debugging
LiangIntroduction to Java Programmingrevised by Dai-kaiyu8
Testing
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
Ensures that the code meets the requirements specification and weeds out bugs
An independent team of software engineers not involved in the design and implementation of the project usually conducts such testing
LiangIntroduction to Java Programmingrevised by Dai-kaiyu9
Deployment
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
Deployment makes the project available for use
For a Java applet this means installing it on a Web server for a Java application installing it on the clients computer
LiangIntroduction to Java Programmingrevised by Dai-kaiyu10
Maintenance
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
Maintenance is concerned with changing and improving the product
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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu11
UML- Use-Case
Describe system behavior and show how to capture it in a model
LiangIntroduction to Java Programmingrevised by Dai-kaiyu12
UML- Use-Case
LiangIntroduction to Java Programmingrevised by Dai-kaiyu13
UML-Activity DiagramIt is essentially a flow chart showing flow of control from activity to activity
LiangIntroduction to Java Programmingrevised by Dai-kaiyu14
UML-Class Diagrams
Describe the static view of the system and show how to capture it in a model
LiangIntroduction to Java Programmingrevised by Dai-kaiyu15
Sequence diagramsSequence diagrams describe interactions among objects by depicting the time ordering of method invocations The diagram shows1048707 The objects participating in the interaction1048707 The sequence of messages exchanged
anObject TheClass Class role
Method Invocation
Activation
anotherObject TheOtherClass
Method InvocationanotherMethod()
aMethod()
LiangIntroduction to Java Programmingrevised by Dai-kaiyu16
Sequence diagrams cont
name Name BorrowLoan address Address loan Loan borrower Borrower
setFirstName
setMi
setLastName
setStreet
setCity
setState
setZip
setAnnualInterestRate
setNumOfYears
setLoanAmount
setName
setAddress
setLoan
LiangIntroduction to Java Programmingrevised by Dai-kaiyu17
UML-Collaboration Diagram
LiangIntroduction to Java Programmingrevised by Dai-kaiyu18
UML-Collaboration Diagram
LiangIntroduction to Java Programmingrevised by Dai-kaiyu19
Statechart diagrams
Statechart diagrams describe flow of control of the object
IndicateInitial State
Transition
State1
State2
LiangIntroduction to Java Programmingrevised by Dai-kaiyu20
Statechart diagrams cont
Class Loaded
JVM loads the class for the object
Use the new operator to create the object
Object Created
Invoke the finalize method on the object
Object Destroyed
LiangIntroduction to Java Programmingrevised by Dai-kaiyu21
UML- Component Diagram
A diagram that shows the organization of and the dependencies among a set of components
LiangIntroduction to Java Programmingrevised by Dai-kaiyu22
UML-Deployment DiagramThe deployment diagram shows
Configuration of processing nodes at run-time
Communication links between these nodes
Component instances and objects that reside on them
LiangIntroduction to Java Programmingrevised by Dai-kaiyu23
Relationships among Classes
Association
Aggregation
Composition
Inheritance
LiangIntroduction to Java Programmingrevised by Dai-kaiyu24
Association
Association represents a general binary relationship that describes an
activity between two classes
Student FacultyCourse560Take Teach
03 1Teacher
public class Student
Data fields private Course[]
courseList
Constructors
Methods
public class Course
Data fields private Student[]
classList
private Faculty faculty
Constructors
Methods
public class Faculty
Data fields
private Course[]
courseList
Constructors
Methods
An association is usually represented as a data field in the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu25
Association Between Same Class
Association may exist between objects of the same class
For example a person may have a supervisor
Person Supervisor
1
1
LiangIntroduction to Java Programmingrevised by Dai-kaiyu26
Aggregation and CompositionAggregation 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
Name Address Person
Composition Aggregation
LiangIntroduction to Java Programmingrevised by Dai-kaiyu27
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
Constructors
Methods
public class Address
Data fields
Constructors
Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu28
Inner Classes TranslationIf 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu29
Inheritance
Inheritance models the is-an-extension-of
relationship between two classes
Person Faculty
public class Student extends Person
Data fields
Constructors
Methods
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu30
Weak Inheritance RelationshipA weak is-an-extension-of relationship can be represented using
interfaces For example the weak is-an-extension-of relationship
ldquostudents are comparable based on their gradesrdquo can be represented
by implementing the Comparable interface as follows
Person
Student Comparable
public class Student extends Person
implements Comparable
Data fields Constructors and
Methods
Implement the compareTo method
public int compareTo(Object object)
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu31
Class Design
1 Identify classes for the system
2 Describe attributes and methods in each class
3 Establish relationships among classes
4 Create classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu32
Example 101 Borrowing Loans
NameName BorrowerBorrowerPersonPersonLoanLoan AddressAddress
Loan
Borrower -loan Loan +Borrower() +Borrower(name Name address Address) +getLoan() Loan +setLoan(loan Loan) 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 67
Person -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 -firstName String -mi char -lastName 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu33
Example 101 Borrowing Loans cont
The following is a test program that uses the classes Name Person Address Borrower and Loan
BorrowLoanBorrowLoan RunRun
LiangIntroduction to Java Programmingrevised by Dai-kaiyu34
Example 102 The Rational Class
RationalRational RunRunTestRationalClassTestRationalClass
1
1 Add Subtract Multiply Divide
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
javalangNumber +byteValue() byte +shortValue() short +intValue() int +longVlaue() long +floatValue() float +doubleValue()double
javalangComparable compareTo(Object) int
LiangIntroduction to Java Programmingrevised by Dai-kaiyu35
Class Design Guidelines
Designing a Single Class
Using Modifiers public protected private and static
Using Inheritance or Aggregation
Using Interfaces or Abstract Classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu36
Designing a Class
A class should describe a single entity or a set of similar operations
A single entity with too many responsibilities can be broken into several classes to separate responsibilities
The String class StringBuffer class and StringTokenizer class all deal with strings for example but have different responsibilities
SystemTextStringBuilder in jdk50
LiangIntroduction to Java Programmingrevised by Dai-kaiyu37
Designing a Class cont
Classes are usually designed for use by many different customers To make a class useful in a wide range of applications the class should provide a variety of ways for customization through properties and methods
Provide a public no-arg constructor and override the equals method and the toString method defined in the Object class whenever possible
LiangIntroduction to Java Programmingrevised by Dai-kaiyu38
Designing a Class cont
Follow standard Java programming style and naming conventions
Choose informative names for classes data fields and methods
Always place the data declaration before the constructor and place constructors before methods
Always provide a constructor and initialize variables to avoid programming errors
LiangIntroduction to Java Programmingrevised by Dai-kaiyu39
Using Visibility Modifiers
Each class can present two contracts ndash one for the users of the class and one for the extenders of the class
Make the fields private and accessor methods public if they are intended for the users of the class Make the fields or method protected if they are intended for extenders of the class
A class should also hide methods not intended for client use but only for internal use within the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu40
Using the static Modifier
A property that is shared by all the instances of the class should be declared as a static property
A method that is not dependent on a specific instance should
be declared as a static method
LiangIntroduction to Java Programmingrevised by Dai-kaiyu41
Using Inheritance or Composition cont
public class Cylinder private Circle circle Constructors Methods
If polymorphism is desirable you need to use the inheritance design If you donrsquot care about polymorphism the aggregation design gives more flexibility because the classes are less
dependent using aggregation than using inheritance
public class Cylinder extends Circle Constructors Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu42
Using Interfaces or Abstract Classes
Both interfaces and abstract classes can be used to generalize common features
a strong is-an-extension-of relationship that clearly describes a parent-child relationship should be modeled using classes
since an orange is a fruit their relationship should be modeled using class inheritance
A weak is-an-extension-of relationship can be modeled using interfaces
all strings are comparable so the String class implements the Comparable interface
LiangIntroduction to Java Programmingrevised by Dai-kaiyu43
Using Interfaces or Abstract Classes cont
Interfaces are more flexible than abstract classes because a subclass can extend only one superclass but
implement any number of interfaces However interfaces cannot contain concrete methods
You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface So you can use the interface or its companion class whichever is more convenient
LiangIntroduction to Java Programmingrevised by Dai-kaiyu44
Supplement P Designing Generic Matrix 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu8
Testing
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
Ensures that the code meets the requirements specification and weeds out bugs
An independent team of software engineers not involved in the design and implementation of the project usually conducts such testing
LiangIntroduction to Java Programmingrevised by Dai-kaiyu9
Deployment
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
Deployment makes the project available for use
For a Java applet this means installing it on a Web server for a Java application installing it on the clients computer
LiangIntroduction to Java Programmingrevised by Dai-kaiyu10
Maintenance
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
Maintenance is concerned with changing and improving the product
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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu11
UML- Use-Case
Describe system behavior and show how to capture it in a model
LiangIntroduction to Java Programmingrevised by Dai-kaiyu12
UML- Use-Case
LiangIntroduction to Java Programmingrevised by Dai-kaiyu13
UML-Activity DiagramIt is essentially a flow chart showing flow of control from activity to activity
LiangIntroduction to Java Programmingrevised by Dai-kaiyu14
UML-Class Diagrams
Describe the static view of the system and show how to capture it in a model
LiangIntroduction to Java Programmingrevised by Dai-kaiyu15
Sequence diagramsSequence diagrams describe interactions among objects by depicting the time ordering of method invocations The diagram shows1048707 The objects participating in the interaction1048707 The sequence of messages exchanged
anObject TheClass Class role
Method Invocation
Activation
anotherObject TheOtherClass
Method InvocationanotherMethod()
aMethod()
LiangIntroduction to Java Programmingrevised by Dai-kaiyu16
Sequence diagrams cont
name Name BorrowLoan address Address loan Loan borrower Borrower
setFirstName
setMi
setLastName
setStreet
setCity
setState
setZip
setAnnualInterestRate
setNumOfYears
setLoanAmount
setName
setAddress
setLoan
LiangIntroduction to Java Programmingrevised by Dai-kaiyu17
UML-Collaboration Diagram
LiangIntroduction to Java Programmingrevised by Dai-kaiyu18
UML-Collaboration Diagram
LiangIntroduction to Java Programmingrevised by Dai-kaiyu19
Statechart diagrams
Statechart diagrams describe flow of control of the object
IndicateInitial State
Transition
State1
State2
LiangIntroduction to Java Programmingrevised by Dai-kaiyu20
Statechart diagrams cont
Class Loaded
JVM loads the class for the object
Use the new operator to create the object
Object Created
Invoke the finalize method on the object
Object Destroyed
LiangIntroduction to Java Programmingrevised by Dai-kaiyu21
UML- Component Diagram
A diagram that shows the organization of and the dependencies among a set of components
LiangIntroduction to Java Programmingrevised by Dai-kaiyu22
UML-Deployment DiagramThe deployment diagram shows
Configuration of processing nodes at run-time
Communication links between these nodes
Component instances and objects that reside on them
LiangIntroduction to Java Programmingrevised by Dai-kaiyu23
Relationships among Classes
Association
Aggregation
Composition
Inheritance
LiangIntroduction to Java Programmingrevised by Dai-kaiyu24
Association
Association represents a general binary relationship that describes an
activity between two classes
Student FacultyCourse560Take Teach
03 1Teacher
public class Student
Data fields private Course[]
courseList
Constructors
Methods
public class Course
Data fields private Student[]
classList
private Faculty faculty
Constructors
Methods
public class Faculty
Data fields
private Course[]
courseList
Constructors
Methods
An association is usually represented as a data field in the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu25
Association Between Same Class
Association may exist between objects of the same class
For example a person may have a supervisor
Person Supervisor
1
1
LiangIntroduction to Java Programmingrevised by Dai-kaiyu26
Aggregation and CompositionAggregation 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
Name Address Person
Composition Aggregation
LiangIntroduction to Java Programmingrevised by Dai-kaiyu27
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
Constructors
Methods
public class Address
Data fields
Constructors
Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu28
Inner Classes TranslationIf 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu29
Inheritance
Inheritance models the is-an-extension-of
relationship between two classes
Person Faculty
public class Student extends Person
Data fields
Constructors
Methods
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu30
Weak Inheritance RelationshipA weak is-an-extension-of relationship can be represented using
interfaces For example the weak is-an-extension-of relationship
ldquostudents are comparable based on their gradesrdquo can be represented
by implementing the Comparable interface as follows
Person
Student Comparable
public class Student extends Person
implements Comparable
Data fields Constructors and
Methods
Implement the compareTo method
public int compareTo(Object object)
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu31
Class Design
1 Identify classes for the system
2 Describe attributes and methods in each class
3 Establish relationships among classes
4 Create classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu32
Example 101 Borrowing Loans
NameName BorrowerBorrowerPersonPersonLoanLoan AddressAddress
Loan
Borrower -loan Loan +Borrower() +Borrower(name Name address Address) +getLoan() Loan +setLoan(loan Loan) 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 67
Person -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 -firstName String -mi char -lastName 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu33
Example 101 Borrowing Loans cont
The following is a test program that uses the classes Name Person Address Borrower and Loan
BorrowLoanBorrowLoan RunRun
LiangIntroduction to Java Programmingrevised by Dai-kaiyu34
Example 102 The Rational Class
RationalRational RunRunTestRationalClassTestRationalClass
1
1 Add Subtract Multiply Divide
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
javalangNumber +byteValue() byte +shortValue() short +intValue() int +longVlaue() long +floatValue() float +doubleValue()double
javalangComparable compareTo(Object) int
LiangIntroduction to Java Programmingrevised by Dai-kaiyu35
Class Design Guidelines
Designing a Single Class
Using Modifiers public protected private and static
Using Inheritance or Aggregation
Using Interfaces or Abstract Classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu36
Designing a Class
A class should describe a single entity or a set of similar operations
A single entity with too many responsibilities can be broken into several classes to separate responsibilities
The String class StringBuffer class and StringTokenizer class all deal with strings for example but have different responsibilities
SystemTextStringBuilder in jdk50
LiangIntroduction to Java Programmingrevised by Dai-kaiyu37
Designing a Class cont
Classes are usually designed for use by many different customers To make a class useful in a wide range of applications the class should provide a variety of ways for customization through properties and methods
Provide a public no-arg constructor and override the equals method and the toString method defined in the Object class whenever possible
LiangIntroduction to Java Programmingrevised by Dai-kaiyu38
Designing a Class cont
Follow standard Java programming style and naming conventions
Choose informative names for classes data fields and methods
Always place the data declaration before the constructor and place constructors before methods
Always provide a constructor and initialize variables to avoid programming errors
LiangIntroduction to Java Programmingrevised by Dai-kaiyu39
Using Visibility Modifiers
Each class can present two contracts ndash one for the users of the class and one for the extenders of the class
Make the fields private and accessor methods public if they are intended for the users of the class Make the fields or method protected if they are intended for extenders of the class
A class should also hide methods not intended for client use but only for internal use within the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu40
Using the static Modifier
A property that is shared by all the instances of the class should be declared as a static property
A method that is not dependent on a specific instance should
be declared as a static method
LiangIntroduction to Java Programmingrevised by Dai-kaiyu41
Using Inheritance or Composition cont
public class Cylinder private Circle circle Constructors Methods
If polymorphism is desirable you need to use the inheritance design If you donrsquot care about polymorphism the aggregation design gives more flexibility because the classes are less
dependent using aggregation than using inheritance
public class Cylinder extends Circle Constructors Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu42
Using Interfaces or Abstract Classes
Both interfaces and abstract classes can be used to generalize common features
a strong is-an-extension-of relationship that clearly describes a parent-child relationship should be modeled using classes
since an orange is a fruit their relationship should be modeled using class inheritance
A weak is-an-extension-of relationship can be modeled using interfaces
all strings are comparable so the String class implements the Comparable interface
LiangIntroduction to Java Programmingrevised by Dai-kaiyu43
Using Interfaces or Abstract Classes cont
Interfaces are more flexible than abstract classes because a subclass can extend only one superclass but
implement any number of interfaces However interfaces cannot contain concrete methods
You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface So you can use the interface or its companion class whichever is more convenient
LiangIntroduction to Java Programmingrevised by Dai-kaiyu44
Supplement P Designing Generic Matrix 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu9
Deployment
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
Deployment makes the project available for use
For a Java applet this means installing it on a Web server for a Java application installing it on the clients computer
LiangIntroduction to Java Programmingrevised by Dai-kaiyu10
Maintenance
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
Maintenance is concerned with changing and improving the product
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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu11
UML- Use-Case
Describe system behavior and show how to capture it in a model
LiangIntroduction to Java Programmingrevised by Dai-kaiyu12
UML- Use-Case
LiangIntroduction to Java Programmingrevised by Dai-kaiyu13
UML-Activity DiagramIt is essentially a flow chart showing flow of control from activity to activity
LiangIntroduction to Java Programmingrevised by Dai-kaiyu14
UML-Class Diagrams
Describe the static view of the system and show how to capture it in a model
LiangIntroduction to Java Programmingrevised by Dai-kaiyu15
Sequence diagramsSequence diagrams describe interactions among objects by depicting the time ordering of method invocations The diagram shows1048707 The objects participating in the interaction1048707 The sequence of messages exchanged
anObject TheClass Class role
Method Invocation
Activation
anotherObject TheOtherClass
Method InvocationanotherMethod()
aMethod()
LiangIntroduction to Java Programmingrevised by Dai-kaiyu16
Sequence diagrams cont
name Name BorrowLoan address Address loan Loan borrower Borrower
setFirstName
setMi
setLastName
setStreet
setCity
setState
setZip
setAnnualInterestRate
setNumOfYears
setLoanAmount
setName
setAddress
setLoan
LiangIntroduction to Java Programmingrevised by Dai-kaiyu17
UML-Collaboration Diagram
LiangIntroduction to Java Programmingrevised by Dai-kaiyu18
UML-Collaboration Diagram
LiangIntroduction to Java Programmingrevised by Dai-kaiyu19
Statechart diagrams
Statechart diagrams describe flow of control of the object
IndicateInitial State
Transition
State1
State2
LiangIntroduction to Java Programmingrevised by Dai-kaiyu20
Statechart diagrams cont
Class Loaded
JVM loads the class for the object
Use the new operator to create the object
Object Created
Invoke the finalize method on the object
Object Destroyed
LiangIntroduction to Java Programmingrevised by Dai-kaiyu21
UML- Component Diagram
A diagram that shows the organization of and the dependencies among a set of components
LiangIntroduction to Java Programmingrevised by Dai-kaiyu22
UML-Deployment DiagramThe deployment diagram shows
Configuration of processing nodes at run-time
Communication links between these nodes
Component instances and objects that reside on them
LiangIntroduction to Java Programmingrevised by Dai-kaiyu23
Relationships among Classes
Association
Aggregation
Composition
Inheritance
LiangIntroduction to Java Programmingrevised by Dai-kaiyu24
Association
Association represents a general binary relationship that describes an
activity between two classes
Student FacultyCourse560Take Teach
03 1Teacher
public class Student
Data fields private Course[]
courseList
Constructors
Methods
public class Course
Data fields private Student[]
classList
private Faculty faculty
Constructors
Methods
public class Faculty
Data fields
private Course[]
courseList
Constructors
Methods
An association is usually represented as a data field in the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu25
Association Between Same Class
Association may exist between objects of the same class
For example a person may have a supervisor
Person Supervisor
1
1
LiangIntroduction to Java Programmingrevised by Dai-kaiyu26
Aggregation and CompositionAggregation 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
Name Address Person
Composition Aggregation
LiangIntroduction to Java Programmingrevised by Dai-kaiyu27
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
Constructors
Methods
public class Address
Data fields
Constructors
Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu28
Inner Classes TranslationIf 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu29
Inheritance
Inheritance models the is-an-extension-of
relationship between two classes
Person Faculty
public class Student extends Person
Data fields
Constructors
Methods
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu30
Weak Inheritance RelationshipA weak is-an-extension-of relationship can be represented using
interfaces For example the weak is-an-extension-of relationship
ldquostudents are comparable based on their gradesrdquo can be represented
by implementing the Comparable interface as follows
Person
Student Comparable
public class Student extends Person
implements Comparable
Data fields Constructors and
Methods
Implement the compareTo method
public int compareTo(Object object)
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu31
Class Design
1 Identify classes for the system
2 Describe attributes and methods in each class
3 Establish relationships among classes
4 Create classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu32
Example 101 Borrowing Loans
NameName BorrowerBorrowerPersonPersonLoanLoan AddressAddress
Loan
Borrower -loan Loan +Borrower() +Borrower(name Name address Address) +getLoan() Loan +setLoan(loan Loan) 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 67
Person -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 -firstName String -mi char -lastName 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu33
Example 101 Borrowing Loans cont
The following is a test program that uses the classes Name Person Address Borrower and Loan
BorrowLoanBorrowLoan RunRun
LiangIntroduction to Java Programmingrevised by Dai-kaiyu34
Example 102 The Rational Class
RationalRational RunRunTestRationalClassTestRationalClass
1
1 Add Subtract Multiply Divide
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
javalangNumber +byteValue() byte +shortValue() short +intValue() int +longVlaue() long +floatValue() float +doubleValue()double
javalangComparable compareTo(Object) int
LiangIntroduction to Java Programmingrevised by Dai-kaiyu35
Class Design Guidelines
Designing a Single Class
Using Modifiers public protected private and static
Using Inheritance or Aggregation
Using Interfaces or Abstract Classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu36
Designing a Class
A class should describe a single entity or a set of similar operations
A single entity with too many responsibilities can be broken into several classes to separate responsibilities
The String class StringBuffer class and StringTokenizer class all deal with strings for example but have different responsibilities
SystemTextStringBuilder in jdk50
LiangIntroduction to Java Programmingrevised by Dai-kaiyu37
Designing a Class cont
Classes are usually designed for use by many different customers To make a class useful in a wide range of applications the class should provide a variety of ways for customization through properties and methods
Provide a public no-arg constructor and override the equals method and the toString method defined in the Object class whenever possible
LiangIntroduction to Java Programmingrevised by Dai-kaiyu38
Designing a Class cont
Follow standard Java programming style and naming conventions
Choose informative names for classes data fields and methods
Always place the data declaration before the constructor and place constructors before methods
Always provide a constructor and initialize variables to avoid programming errors
LiangIntroduction to Java Programmingrevised by Dai-kaiyu39
Using Visibility Modifiers
Each class can present two contracts ndash one for the users of the class and one for the extenders of the class
Make the fields private and accessor methods public if they are intended for the users of the class Make the fields or method protected if they are intended for extenders of the class
A class should also hide methods not intended for client use but only for internal use within the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu40
Using the static Modifier
A property that is shared by all the instances of the class should be declared as a static property
A method that is not dependent on a specific instance should
be declared as a static method
LiangIntroduction to Java Programmingrevised by Dai-kaiyu41
Using Inheritance or Composition cont
public class Cylinder private Circle circle Constructors Methods
If polymorphism is desirable you need to use the inheritance design If you donrsquot care about polymorphism the aggregation design gives more flexibility because the classes are less
dependent using aggregation than using inheritance
public class Cylinder extends Circle Constructors Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu42
Using Interfaces or Abstract Classes
Both interfaces and abstract classes can be used to generalize common features
a strong is-an-extension-of relationship that clearly describes a parent-child relationship should be modeled using classes
since an orange is a fruit their relationship should be modeled using class inheritance
A weak is-an-extension-of relationship can be modeled using interfaces
all strings are comparable so the String class implements the Comparable interface
LiangIntroduction to Java Programmingrevised by Dai-kaiyu43
Using Interfaces or Abstract Classes cont
Interfaces are more flexible than abstract classes because a subclass can extend only one superclass but
implement any number of interfaces However interfaces cannot contain concrete methods
You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface So you can use the interface or its companion class whichever is more convenient
LiangIntroduction to Java Programmingrevised by Dai-kaiyu44
Supplement P Designing Generic Matrix 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu10
Maintenance
Requirement Specification
System Analysis
System Design
Testing
Implementation
Maintenance
Deployment
Maintenance is concerned with changing and improving the product
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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu11
UML- Use-Case
Describe system behavior and show how to capture it in a model
LiangIntroduction to Java Programmingrevised by Dai-kaiyu12
UML- Use-Case
LiangIntroduction to Java Programmingrevised by Dai-kaiyu13
UML-Activity DiagramIt is essentially a flow chart showing flow of control from activity to activity
LiangIntroduction to Java Programmingrevised by Dai-kaiyu14
UML-Class Diagrams
Describe the static view of the system and show how to capture it in a model
LiangIntroduction to Java Programmingrevised by Dai-kaiyu15
Sequence diagramsSequence diagrams describe interactions among objects by depicting the time ordering of method invocations The diagram shows1048707 The objects participating in the interaction1048707 The sequence of messages exchanged
anObject TheClass Class role
Method Invocation
Activation
anotherObject TheOtherClass
Method InvocationanotherMethod()
aMethod()
LiangIntroduction to Java Programmingrevised by Dai-kaiyu16
Sequence diagrams cont
name Name BorrowLoan address Address loan Loan borrower Borrower
setFirstName
setMi
setLastName
setStreet
setCity
setState
setZip
setAnnualInterestRate
setNumOfYears
setLoanAmount
setName
setAddress
setLoan
LiangIntroduction to Java Programmingrevised by Dai-kaiyu17
UML-Collaboration Diagram
LiangIntroduction to Java Programmingrevised by Dai-kaiyu18
UML-Collaboration Diagram
LiangIntroduction to Java Programmingrevised by Dai-kaiyu19
Statechart diagrams
Statechart diagrams describe flow of control of the object
IndicateInitial State
Transition
State1
State2
LiangIntroduction to Java Programmingrevised by Dai-kaiyu20
Statechart diagrams cont
Class Loaded
JVM loads the class for the object
Use the new operator to create the object
Object Created
Invoke the finalize method on the object
Object Destroyed
LiangIntroduction to Java Programmingrevised by Dai-kaiyu21
UML- Component Diagram
A diagram that shows the organization of and the dependencies among a set of components
LiangIntroduction to Java Programmingrevised by Dai-kaiyu22
UML-Deployment DiagramThe deployment diagram shows
Configuration of processing nodes at run-time
Communication links between these nodes
Component instances and objects that reside on them
LiangIntroduction to Java Programmingrevised by Dai-kaiyu23
Relationships among Classes
Association
Aggregation
Composition
Inheritance
LiangIntroduction to Java Programmingrevised by Dai-kaiyu24
Association
Association represents a general binary relationship that describes an
activity between two classes
Student FacultyCourse560Take Teach
03 1Teacher
public class Student
Data fields private Course[]
courseList
Constructors
Methods
public class Course
Data fields private Student[]
classList
private Faculty faculty
Constructors
Methods
public class Faculty
Data fields
private Course[]
courseList
Constructors
Methods
An association is usually represented as a data field in the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu25
Association Between Same Class
Association may exist between objects of the same class
For example a person may have a supervisor
Person Supervisor
1
1
LiangIntroduction to Java Programmingrevised by Dai-kaiyu26
Aggregation and CompositionAggregation 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
Name Address Person
Composition Aggregation
LiangIntroduction to Java Programmingrevised by Dai-kaiyu27
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
Constructors
Methods
public class Address
Data fields
Constructors
Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu28
Inner Classes TranslationIf 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu29
Inheritance
Inheritance models the is-an-extension-of
relationship between two classes
Person Faculty
public class Student extends Person
Data fields
Constructors
Methods
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu30
Weak Inheritance RelationshipA weak is-an-extension-of relationship can be represented using
interfaces For example the weak is-an-extension-of relationship
ldquostudents are comparable based on their gradesrdquo can be represented
by implementing the Comparable interface as follows
Person
Student Comparable
public class Student extends Person
implements Comparable
Data fields Constructors and
Methods
Implement the compareTo method
public int compareTo(Object object)
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu31
Class Design
1 Identify classes for the system
2 Describe attributes and methods in each class
3 Establish relationships among classes
4 Create classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu32
Example 101 Borrowing Loans
NameName BorrowerBorrowerPersonPersonLoanLoan AddressAddress
Loan
Borrower -loan Loan +Borrower() +Borrower(name Name address Address) +getLoan() Loan +setLoan(loan Loan) 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 67
Person -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 -firstName String -mi char -lastName 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu33
Example 101 Borrowing Loans cont
The following is a test program that uses the classes Name Person Address Borrower and Loan
BorrowLoanBorrowLoan RunRun
LiangIntroduction to Java Programmingrevised by Dai-kaiyu34
Example 102 The Rational Class
RationalRational RunRunTestRationalClassTestRationalClass
1
1 Add Subtract Multiply Divide
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
javalangNumber +byteValue() byte +shortValue() short +intValue() int +longVlaue() long +floatValue() float +doubleValue()double
javalangComparable compareTo(Object) int
LiangIntroduction to Java Programmingrevised by Dai-kaiyu35
Class Design Guidelines
Designing a Single Class
Using Modifiers public protected private and static
Using Inheritance or Aggregation
Using Interfaces or Abstract Classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu36
Designing a Class
A class should describe a single entity or a set of similar operations
A single entity with too many responsibilities can be broken into several classes to separate responsibilities
The String class StringBuffer class and StringTokenizer class all deal with strings for example but have different responsibilities
SystemTextStringBuilder in jdk50
LiangIntroduction to Java Programmingrevised by Dai-kaiyu37
Designing a Class cont
Classes are usually designed for use by many different customers To make a class useful in a wide range of applications the class should provide a variety of ways for customization through properties and methods
Provide a public no-arg constructor and override the equals method and the toString method defined in the Object class whenever possible
LiangIntroduction to Java Programmingrevised by Dai-kaiyu38
Designing a Class cont
Follow standard Java programming style and naming conventions
Choose informative names for classes data fields and methods
Always place the data declaration before the constructor and place constructors before methods
Always provide a constructor and initialize variables to avoid programming errors
LiangIntroduction to Java Programmingrevised by Dai-kaiyu39
Using Visibility Modifiers
Each class can present two contracts ndash one for the users of the class and one for the extenders of the class
Make the fields private and accessor methods public if they are intended for the users of the class Make the fields or method protected if they are intended for extenders of the class
A class should also hide methods not intended for client use but only for internal use within the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu40
Using the static Modifier
A property that is shared by all the instances of the class should be declared as a static property
A method that is not dependent on a specific instance should
be declared as a static method
LiangIntroduction to Java Programmingrevised by Dai-kaiyu41
Using Inheritance or Composition cont
public class Cylinder private Circle circle Constructors Methods
If polymorphism is desirable you need to use the inheritance design If you donrsquot care about polymorphism the aggregation design gives more flexibility because the classes are less
dependent using aggregation than using inheritance
public class Cylinder extends Circle Constructors Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu42
Using Interfaces or Abstract Classes
Both interfaces and abstract classes can be used to generalize common features
a strong is-an-extension-of relationship that clearly describes a parent-child relationship should be modeled using classes
since an orange is a fruit their relationship should be modeled using class inheritance
A weak is-an-extension-of relationship can be modeled using interfaces
all strings are comparable so the String class implements the Comparable interface
LiangIntroduction to Java Programmingrevised by Dai-kaiyu43
Using Interfaces or Abstract Classes cont
Interfaces are more flexible than abstract classes because a subclass can extend only one superclass but
implement any number of interfaces However interfaces cannot contain concrete methods
You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface So you can use the interface or its companion class whichever is more convenient
LiangIntroduction to Java Programmingrevised by Dai-kaiyu44
Supplement P Designing Generic Matrix 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu11
UML- Use-Case
Describe system behavior and show how to capture it in a model
LiangIntroduction to Java Programmingrevised by Dai-kaiyu12
UML- Use-Case
LiangIntroduction to Java Programmingrevised by Dai-kaiyu13
UML-Activity DiagramIt is essentially a flow chart showing flow of control from activity to activity
LiangIntroduction to Java Programmingrevised by Dai-kaiyu14
UML-Class Diagrams
Describe the static view of the system and show how to capture it in a model
LiangIntroduction to Java Programmingrevised by Dai-kaiyu15
Sequence diagramsSequence diagrams describe interactions among objects by depicting the time ordering of method invocations The diagram shows1048707 The objects participating in the interaction1048707 The sequence of messages exchanged
anObject TheClass Class role
Method Invocation
Activation
anotherObject TheOtherClass
Method InvocationanotherMethod()
aMethod()
LiangIntroduction to Java Programmingrevised by Dai-kaiyu16
Sequence diagrams cont
name Name BorrowLoan address Address loan Loan borrower Borrower
setFirstName
setMi
setLastName
setStreet
setCity
setState
setZip
setAnnualInterestRate
setNumOfYears
setLoanAmount
setName
setAddress
setLoan
LiangIntroduction to Java Programmingrevised by Dai-kaiyu17
UML-Collaboration Diagram
LiangIntroduction to Java Programmingrevised by Dai-kaiyu18
UML-Collaboration Diagram
LiangIntroduction to Java Programmingrevised by Dai-kaiyu19
Statechart diagrams
Statechart diagrams describe flow of control of the object
IndicateInitial State
Transition
State1
State2
LiangIntroduction to Java Programmingrevised by Dai-kaiyu20
Statechart diagrams cont
Class Loaded
JVM loads the class for the object
Use the new operator to create the object
Object Created
Invoke the finalize method on the object
Object Destroyed
LiangIntroduction to Java Programmingrevised by Dai-kaiyu21
UML- Component Diagram
A diagram that shows the organization of and the dependencies among a set of components
LiangIntroduction to Java Programmingrevised by Dai-kaiyu22
UML-Deployment DiagramThe deployment diagram shows
Configuration of processing nodes at run-time
Communication links between these nodes
Component instances and objects that reside on them
LiangIntroduction to Java Programmingrevised by Dai-kaiyu23
Relationships among Classes
Association
Aggregation
Composition
Inheritance
LiangIntroduction to Java Programmingrevised by Dai-kaiyu24
Association
Association represents a general binary relationship that describes an
activity between two classes
Student FacultyCourse560Take Teach
03 1Teacher
public class Student
Data fields private Course[]
courseList
Constructors
Methods
public class Course
Data fields private Student[]
classList
private Faculty faculty
Constructors
Methods
public class Faculty
Data fields
private Course[]
courseList
Constructors
Methods
An association is usually represented as a data field in the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu25
Association Between Same Class
Association may exist between objects of the same class
For example a person may have a supervisor
Person Supervisor
1
1
LiangIntroduction to Java Programmingrevised by Dai-kaiyu26
Aggregation and CompositionAggregation 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
Name Address Person
Composition Aggregation
LiangIntroduction to Java Programmingrevised by Dai-kaiyu27
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
Constructors
Methods
public class Address
Data fields
Constructors
Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu28
Inner Classes TranslationIf 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu29
Inheritance
Inheritance models the is-an-extension-of
relationship between two classes
Person Faculty
public class Student extends Person
Data fields
Constructors
Methods
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu30
Weak Inheritance RelationshipA weak is-an-extension-of relationship can be represented using
interfaces For example the weak is-an-extension-of relationship
ldquostudents are comparable based on their gradesrdquo can be represented
by implementing the Comparable interface as follows
Person
Student Comparable
public class Student extends Person
implements Comparable
Data fields Constructors and
Methods
Implement the compareTo method
public int compareTo(Object object)
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu31
Class Design
1 Identify classes for the system
2 Describe attributes and methods in each class
3 Establish relationships among classes
4 Create classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu32
Example 101 Borrowing Loans
NameName BorrowerBorrowerPersonPersonLoanLoan AddressAddress
Loan
Borrower -loan Loan +Borrower() +Borrower(name Name address Address) +getLoan() Loan +setLoan(loan Loan) 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 67
Person -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 -firstName String -mi char -lastName 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu33
Example 101 Borrowing Loans cont
The following is a test program that uses the classes Name Person Address Borrower and Loan
BorrowLoanBorrowLoan RunRun
LiangIntroduction to Java Programmingrevised by Dai-kaiyu34
Example 102 The Rational Class
RationalRational RunRunTestRationalClassTestRationalClass
1
1 Add Subtract Multiply Divide
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
javalangNumber +byteValue() byte +shortValue() short +intValue() int +longVlaue() long +floatValue() float +doubleValue()double
javalangComparable compareTo(Object) int
LiangIntroduction to Java Programmingrevised by Dai-kaiyu35
Class Design Guidelines
Designing a Single Class
Using Modifiers public protected private and static
Using Inheritance or Aggregation
Using Interfaces or Abstract Classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu36
Designing a Class
A class should describe a single entity or a set of similar operations
A single entity with too many responsibilities can be broken into several classes to separate responsibilities
The String class StringBuffer class and StringTokenizer class all deal with strings for example but have different responsibilities
SystemTextStringBuilder in jdk50
LiangIntroduction to Java Programmingrevised by Dai-kaiyu37
Designing a Class cont
Classes are usually designed for use by many different customers To make a class useful in a wide range of applications the class should provide a variety of ways for customization through properties and methods
Provide a public no-arg constructor and override the equals method and the toString method defined in the Object class whenever possible
LiangIntroduction to Java Programmingrevised by Dai-kaiyu38
Designing a Class cont
Follow standard Java programming style and naming conventions
Choose informative names for classes data fields and methods
Always place the data declaration before the constructor and place constructors before methods
Always provide a constructor and initialize variables to avoid programming errors
LiangIntroduction to Java Programmingrevised by Dai-kaiyu39
Using Visibility Modifiers
Each class can present two contracts ndash one for the users of the class and one for the extenders of the class
Make the fields private and accessor methods public if they are intended for the users of the class Make the fields or method protected if they are intended for extenders of the class
A class should also hide methods not intended for client use but only for internal use within the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu40
Using the static Modifier
A property that is shared by all the instances of the class should be declared as a static property
A method that is not dependent on a specific instance should
be declared as a static method
LiangIntroduction to Java Programmingrevised by Dai-kaiyu41
Using Inheritance or Composition cont
public class Cylinder private Circle circle Constructors Methods
If polymorphism is desirable you need to use the inheritance design If you donrsquot care about polymorphism the aggregation design gives more flexibility because the classes are less
dependent using aggregation than using inheritance
public class Cylinder extends Circle Constructors Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu42
Using Interfaces or Abstract Classes
Both interfaces and abstract classes can be used to generalize common features
a strong is-an-extension-of relationship that clearly describes a parent-child relationship should be modeled using classes
since an orange is a fruit their relationship should be modeled using class inheritance
A weak is-an-extension-of relationship can be modeled using interfaces
all strings are comparable so the String class implements the Comparable interface
LiangIntroduction to Java Programmingrevised by Dai-kaiyu43
Using Interfaces or Abstract Classes cont
Interfaces are more flexible than abstract classes because a subclass can extend only one superclass but
implement any number of interfaces However interfaces cannot contain concrete methods
You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface So you can use the interface or its companion class whichever is more convenient
LiangIntroduction to Java Programmingrevised by Dai-kaiyu44
Supplement P Designing Generic Matrix 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu12
UML- Use-Case
LiangIntroduction to Java Programmingrevised by Dai-kaiyu13
UML-Activity DiagramIt is essentially a flow chart showing flow of control from activity to activity
LiangIntroduction to Java Programmingrevised by Dai-kaiyu14
UML-Class Diagrams
Describe the static view of the system and show how to capture it in a model
LiangIntroduction to Java Programmingrevised by Dai-kaiyu15
Sequence diagramsSequence diagrams describe interactions among objects by depicting the time ordering of method invocations The diagram shows1048707 The objects participating in the interaction1048707 The sequence of messages exchanged
anObject TheClass Class role
Method Invocation
Activation
anotherObject TheOtherClass
Method InvocationanotherMethod()
aMethod()
LiangIntroduction to Java Programmingrevised by Dai-kaiyu16
Sequence diagrams cont
name Name BorrowLoan address Address loan Loan borrower Borrower
setFirstName
setMi
setLastName
setStreet
setCity
setState
setZip
setAnnualInterestRate
setNumOfYears
setLoanAmount
setName
setAddress
setLoan
LiangIntroduction to Java Programmingrevised by Dai-kaiyu17
UML-Collaboration Diagram
LiangIntroduction to Java Programmingrevised by Dai-kaiyu18
UML-Collaboration Diagram
LiangIntroduction to Java Programmingrevised by Dai-kaiyu19
Statechart diagrams
Statechart diagrams describe flow of control of the object
IndicateInitial State
Transition
State1
State2
LiangIntroduction to Java Programmingrevised by Dai-kaiyu20
Statechart diagrams cont
Class Loaded
JVM loads the class for the object
Use the new operator to create the object
Object Created
Invoke the finalize method on the object
Object Destroyed
LiangIntroduction to Java Programmingrevised by Dai-kaiyu21
UML- Component Diagram
A diagram that shows the organization of and the dependencies among a set of components
LiangIntroduction to Java Programmingrevised by Dai-kaiyu22
UML-Deployment DiagramThe deployment diagram shows
Configuration of processing nodes at run-time
Communication links between these nodes
Component instances and objects that reside on them
LiangIntroduction to Java Programmingrevised by Dai-kaiyu23
Relationships among Classes
Association
Aggregation
Composition
Inheritance
LiangIntroduction to Java Programmingrevised by Dai-kaiyu24
Association
Association represents a general binary relationship that describes an
activity between two classes
Student FacultyCourse560Take Teach
03 1Teacher
public class Student
Data fields private Course[]
courseList
Constructors
Methods
public class Course
Data fields private Student[]
classList
private Faculty faculty
Constructors
Methods
public class Faculty
Data fields
private Course[]
courseList
Constructors
Methods
An association is usually represented as a data field in the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu25
Association Between Same Class
Association may exist between objects of the same class
For example a person may have a supervisor
Person Supervisor
1
1
LiangIntroduction to Java Programmingrevised by Dai-kaiyu26
Aggregation and CompositionAggregation 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
Name Address Person
Composition Aggregation
LiangIntroduction to Java Programmingrevised by Dai-kaiyu27
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
Constructors
Methods
public class Address
Data fields
Constructors
Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu28
Inner Classes TranslationIf 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu29
Inheritance
Inheritance models the is-an-extension-of
relationship between two classes
Person Faculty
public class Student extends Person
Data fields
Constructors
Methods
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu30
Weak Inheritance RelationshipA weak is-an-extension-of relationship can be represented using
interfaces For example the weak is-an-extension-of relationship
ldquostudents are comparable based on their gradesrdquo can be represented
by implementing the Comparable interface as follows
Person
Student Comparable
public class Student extends Person
implements Comparable
Data fields Constructors and
Methods
Implement the compareTo method
public int compareTo(Object object)
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu31
Class Design
1 Identify classes for the system
2 Describe attributes and methods in each class
3 Establish relationships among classes
4 Create classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu32
Example 101 Borrowing Loans
NameName BorrowerBorrowerPersonPersonLoanLoan AddressAddress
Loan
Borrower -loan Loan +Borrower() +Borrower(name Name address Address) +getLoan() Loan +setLoan(loan Loan) 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 67
Person -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 -firstName String -mi char -lastName 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu33
Example 101 Borrowing Loans cont
The following is a test program that uses the classes Name Person Address Borrower and Loan
BorrowLoanBorrowLoan RunRun
LiangIntroduction to Java Programmingrevised by Dai-kaiyu34
Example 102 The Rational Class
RationalRational RunRunTestRationalClassTestRationalClass
1
1 Add Subtract Multiply Divide
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
javalangNumber +byteValue() byte +shortValue() short +intValue() int +longVlaue() long +floatValue() float +doubleValue()double
javalangComparable compareTo(Object) int
LiangIntroduction to Java Programmingrevised by Dai-kaiyu35
Class Design Guidelines
Designing a Single Class
Using Modifiers public protected private and static
Using Inheritance or Aggregation
Using Interfaces or Abstract Classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu36
Designing a Class
A class should describe a single entity or a set of similar operations
A single entity with too many responsibilities can be broken into several classes to separate responsibilities
The String class StringBuffer class and StringTokenizer class all deal with strings for example but have different responsibilities
SystemTextStringBuilder in jdk50
LiangIntroduction to Java Programmingrevised by Dai-kaiyu37
Designing a Class cont
Classes are usually designed for use by many different customers To make a class useful in a wide range of applications the class should provide a variety of ways for customization through properties and methods
Provide a public no-arg constructor and override the equals method and the toString method defined in the Object class whenever possible
LiangIntroduction to Java Programmingrevised by Dai-kaiyu38
Designing a Class cont
Follow standard Java programming style and naming conventions
Choose informative names for classes data fields and methods
Always place the data declaration before the constructor and place constructors before methods
Always provide a constructor and initialize variables to avoid programming errors
LiangIntroduction to Java Programmingrevised by Dai-kaiyu39
Using Visibility Modifiers
Each class can present two contracts ndash one for the users of the class and one for the extenders of the class
Make the fields private and accessor methods public if they are intended for the users of the class Make the fields or method protected if they are intended for extenders of the class
A class should also hide methods not intended for client use but only for internal use within the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu40
Using the static Modifier
A property that is shared by all the instances of the class should be declared as a static property
A method that is not dependent on a specific instance should
be declared as a static method
LiangIntroduction to Java Programmingrevised by Dai-kaiyu41
Using Inheritance or Composition cont
public class Cylinder private Circle circle Constructors Methods
If polymorphism is desirable you need to use the inheritance design If you donrsquot care about polymorphism the aggregation design gives more flexibility because the classes are less
dependent using aggregation than using inheritance
public class Cylinder extends Circle Constructors Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu42
Using Interfaces or Abstract Classes
Both interfaces and abstract classes can be used to generalize common features
a strong is-an-extension-of relationship that clearly describes a parent-child relationship should be modeled using classes
since an orange is a fruit their relationship should be modeled using class inheritance
A weak is-an-extension-of relationship can be modeled using interfaces
all strings are comparable so the String class implements the Comparable interface
LiangIntroduction to Java Programmingrevised by Dai-kaiyu43
Using Interfaces or Abstract Classes cont
Interfaces are more flexible than abstract classes because a subclass can extend only one superclass but
implement any number of interfaces However interfaces cannot contain concrete methods
You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface So you can use the interface or its companion class whichever is more convenient
LiangIntroduction to Java Programmingrevised by Dai-kaiyu44
Supplement P Designing Generic Matrix 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu13
UML-Activity DiagramIt is essentially a flow chart showing flow of control from activity to activity
LiangIntroduction to Java Programmingrevised by Dai-kaiyu14
UML-Class Diagrams
Describe the static view of the system and show how to capture it in a model
LiangIntroduction to Java Programmingrevised by Dai-kaiyu15
Sequence diagramsSequence diagrams describe interactions among objects by depicting the time ordering of method invocations The diagram shows1048707 The objects participating in the interaction1048707 The sequence of messages exchanged
anObject TheClass Class role
Method Invocation
Activation
anotherObject TheOtherClass
Method InvocationanotherMethod()
aMethod()
LiangIntroduction to Java Programmingrevised by Dai-kaiyu16
Sequence diagrams cont
name Name BorrowLoan address Address loan Loan borrower Borrower
setFirstName
setMi
setLastName
setStreet
setCity
setState
setZip
setAnnualInterestRate
setNumOfYears
setLoanAmount
setName
setAddress
setLoan
LiangIntroduction to Java Programmingrevised by Dai-kaiyu17
UML-Collaboration Diagram
LiangIntroduction to Java Programmingrevised by Dai-kaiyu18
UML-Collaboration Diagram
LiangIntroduction to Java Programmingrevised by Dai-kaiyu19
Statechart diagrams
Statechart diagrams describe flow of control of the object
IndicateInitial State
Transition
State1
State2
LiangIntroduction to Java Programmingrevised by Dai-kaiyu20
Statechart diagrams cont
Class Loaded
JVM loads the class for the object
Use the new operator to create the object
Object Created
Invoke the finalize method on the object
Object Destroyed
LiangIntroduction to Java Programmingrevised by Dai-kaiyu21
UML- Component Diagram
A diagram that shows the organization of and the dependencies among a set of components
LiangIntroduction to Java Programmingrevised by Dai-kaiyu22
UML-Deployment DiagramThe deployment diagram shows
Configuration of processing nodes at run-time
Communication links between these nodes
Component instances and objects that reside on them
LiangIntroduction to Java Programmingrevised by Dai-kaiyu23
Relationships among Classes
Association
Aggregation
Composition
Inheritance
LiangIntroduction to Java Programmingrevised by Dai-kaiyu24
Association
Association represents a general binary relationship that describes an
activity between two classes
Student FacultyCourse560Take Teach
03 1Teacher
public class Student
Data fields private Course[]
courseList
Constructors
Methods
public class Course
Data fields private Student[]
classList
private Faculty faculty
Constructors
Methods
public class Faculty
Data fields
private Course[]
courseList
Constructors
Methods
An association is usually represented as a data field in the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu25
Association Between Same Class
Association may exist between objects of the same class
For example a person may have a supervisor
Person Supervisor
1
1
LiangIntroduction to Java Programmingrevised by Dai-kaiyu26
Aggregation and CompositionAggregation 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
Name Address Person
Composition Aggregation
LiangIntroduction to Java Programmingrevised by Dai-kaiyu27
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
Constructors
Methods
public class Address
Data fields
Constructors
Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu28
Inner Classes TranslationIf 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu29
Inheritance
Inheritance models the is-an-extension-of
relationship between two classes
Person Faculty
public class Student extends Person
Data fields
Constructors
Methods
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu30
Weak Inheritance RelationshipA weak is-an-extension-of relationship can be represented using
interfaces For example the weak is-an-extension-of relationship
ldquostudents are comparable based on their gradesrdquo can be represented
by implementing the Comparable interface as follows
Person
Student Comparable
public class Student extends Person
implements Comparable
Data fields Constructors and
Methods
Implement the compareTo method
public int compareTo(Object object)
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu31
Class Design
1 Identify classes for the system
2 Describe attributes and methods in each class
3 Establish relationships among classes
4 Create classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu32
Example 101 Borrowing Loans
NameName BorrowerBorrowerPersonPersonLoanLoan AddressAddress
Loan
Borrower -loan Loan +Borrower() +Borrower(name Name address Address) +getLoan() Loan +setLoan(loan Loan) 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 67
Person -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 -firstName String -mi char -lastName 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu33
Example 101 Borrowing Loans cont
The following is a test program that uses the classes Name Person Address Borrower and Loan
BorrowLoanBorrowLoan RunRun
LiangIntroduction to Java Programmingrevised by Dai-kaiyu34
Example 102 The Rational Class
RationalRational RunRunTestRationalClassTestRationalClass
1
1 Add Subtract Multiply Divide
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
javalangNumber +byteValue() byte +shortValue() short +intValue() int +longVlaue() long +floatValue() float +doubleValue()double
javalangComparable compareTo(Object) int
LiangIntroduction to Java Programmingrevised by Dai-kaiyu35
Class Design Guidelines
Designing a Single Class
Using Modifiers public protected private and static
Using Inheritance or Aggregation
Using Interfaces or Abstract Classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu36
Designing a Class
A class should describe a single entity or a set of similar operations
A single entity with too many responsibilities can be broken into several classes to separate responsibilities
The String class StringBuffer class and StringTokenizer class all deal with strings for example but have different responsibilities
SystemTextStringBuilder in jdk50
LiangIntroduction to Java Programmingrevised by Dai-kaiyu37
Designing a Class cont
Classes are usually designed for use by many different customers To make a class useful in a wide range of applications the class should provide a variety of ways for customization through properties and methods
Provide a public no-arg constructor and override the equals method and the toString method defined in the Object class whenever possible
LiangIntroduction to Java Programmingrevised by Dai-kaiyu38
Designing a Class cont
Follow standard Java programming style and naming conventions
Choose informative names for classes data fields and methods
Always place the data declaration before the constructor and place constructors before methods
Always provide a constructor and initialize variables to avoid programming errors
LiangIntroduction to Java Programmingrevised by Dai-kaiyu39
Using Visibility Modifiers
Each class can present two contracts ndash one for the users of the class and one for the extenders of the class
Make the fields private and accessor methods public if they are intended for the users of the class Make the fields or method protected if they are intended for extenders of the class
A class should also hide methods not intended for client use but only for internal use within the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu40
Using the static Modifier
A property that is shared by all the instances of the class should be declared as a static property
A method that is not dependent on a specific instance should
be declared as a static method
LiangIntroduction to Java Programmingrevised by Dai-kaiyu41
Using Inheritance or Composition cont
public class Cylinder private Circle circle Constructors Methods
If polymorphism is desirable you need to use the inheritance design If you donrsquot care about polymorphism the aggregation design gives more flexibility because the classes are less
dependent using aggregation than using inheritance
public class Cylinder extends Circle Constructors Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu42
Using Interfaces or Abstract Classes
Both interfaces and abstract classes can be used to generalize common features
a strong is-an-extension-of relationship that clearly describes a parent-child relationship should be modeled using classes
since an orange is a fruit their relationship should be modeled using class inheritance
A weak is-an-extension-of relationship can be modeled using interfaces
all strings are comparable so the String class implements the Comparable interface
LiangIntroduction to Java Programmingrevised by Dai-kaiyu43
Using Interfaces or Abstract Classes cont
Interfaces are more flexible than abstract classes because a subclass can extend only one superclass but
implement any number of interfaces However interfaces cannot contain concrete methods
You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface So you can use the interface or its companion class whichever is more convenient
LiangIntroduction to Java Programmingrevised by Dai-kaiyu44
Supplement P Designing Generic Matrix 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu14
UML-Class Diagrams
Describe the static view of the system and show how to capture it in a model
LiangIntroduction to Java Programmingrevised by Dai-kaiyu15
Sequence diagramsSequence diagrams describe interactions among objects by depicting the time ordering of method invocations The diagram shows1048707 The objects participating in the interaction1048707 The sequence of messages exchanged
anObject TheClass Class role
Method Invocation
Activation
anotherObject TheOtherClass
Method InvocationanotherMethod()
aMethod()
LiangIntroduction to Java Programmingrevised by Dai-kaiyu16
Sequence diagrams cont
name Name BorrowLoan address Address loan Loan borrower Borrower
setFirstName
setMi
setLastName
setStreet
setCity
setState
setZip
setAnnualInterestRate
setNumOfYears
setLoanAmount
setName
setAddress
setLoan
LiangIntroduction to Java Programmingrevised by Dai-kaiyu17
UML-Collaboration Diagram
LiangIntroduction to Java Programmingrevised by Dai-kaiyu18
UML-Collaboration Diagram
LiangIntroduction to Java Programmingrevised by Dai-kaiyu19
Statechart diagrams
Statechart diagrams describe flow of control of the object
IndicateInitial State
Transition
State1
State2
LiangIntroduction to Java Programmingrevised by Dai-kaiyu20
Statechart diagrams cont
Class Loaded
JVM loads the class for the object
Use the new operator to create the object
Object Created
Invoke the finalize method on the object
Object Destroyed
LiangIntroduction to Java Programmingrevised by Dai-kaiyu21
UML- Component Diagram
A diagram that shows the organization of and the dependencies among a set of components
LiangIntroduction to Java Programmingrevised by Dai-kaiyu22
UML-Deployment DiagramThe deployment diagram shows
Configuration of processing nodes at run-time
Communication links between these nodes
Component instances and objects that reside on them
LiangIntroduction to Java Programmingrevised by Dai-kaiyu23
Relationships among Classes
Association
Aggregation
Composition
Inheritance
LiangIntroduction to Java Programmingrevised by Dai-kaiyu24
Association
Association represents a general binary relationship that describes an
activity between two classes
Student FacultyCourse560Take Teach
03 1Teacher
public class Student
Data fields private Course[]
courseList
Constructors
Methods
public class Course
Data fields private Student[]
classList
private Faculty faculty
Constructors
Methods
public class Faculty
Data fields
private Course[]
courseList
Constructors
Methods
An association is usually represented as a data field in the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu25
Association Between Same Class
Association may exist between objects of the same class
For example a person may have a supervisor
Person Supervisor
1
1
LiangIntroduction to Java Programmingrevised by Dai-kaiyu26
Aggregation and CompositionAggregation 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
Name Address Person
Composition Aggregation
LiangIntroduction to Java Programmingrevised by Dai-kaiyu27
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
Constructors
Methods
public class Address
Data fields
Constructors
Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu28
Inner Classes TranslationIf 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu29
Inheritance
Inheritance models the is-an-extension-of
relationship between two classes
Person Faculty
public class Student extends Person
Data fields
Constructors
Methods
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu30
Weak Inheritance RelationshipA weak is-an-extension-of relationship can be represented using
interfaces For example the weak is-an-extension-of relationship
ldquostudents are comparable based on their gradesrdquo can be represented
by implementing the Comparable interface as follows
Person
Student Comparable
public class Student extends Person
implements Comparable
Data fields Constructors and
Methods
Implement the compareTo method
public int compareTo(Object object)
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu31
Class Design
1 Identify classes for the system
2 Describe attributes and methods in each class
3 Establish relationships among classes
4 Create classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu32
Example 101 Borrowing Loans
NameName BorrowerBorrowerPersonPersonLoanLoan AddressAddress
Loan
Borrower -loan Loan +Borrower() +Borrower(name Name address Address) +getLoan() Loan +setLoan(loan Loan) 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 67
Person -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 -firstName String -mi char -lastName 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu33
Example 101 Borrowing Loans cont
The following is a test program that uses the classes Name Person Address Borrower and Loan
BorrowLoanBorrowLoan RunRun
LiangIntroduction to Java Programmingrevised by Dai-kaiyu34
Example 102 The Rational Class
RationalRational RunRunTestRationalClassTestRationalClass
1
1 Add Subtract Multiply Divide
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
javalangNumber +byteValue() byte +shortValue() short +intValue() int +longVlaue() long +floatValue() float +doubleValue()double
javalangComparable compareTo(Object) int
LiangIntroduction to Java Programmingrevised by Dai-kaiyu35
Class Design Guidelines
Designing a Single Class
Using Modifiers public protected private and static
Using Inheritance or Aggregation
Using Interfaces or Abstract Classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu36
Designing a Class
A class should describe a single entity or a set of similar operations
A single entity with too many responsibilities can be broken into several classes to separate responsibilities
The String class StringBuffer class and StringTokenizer class all deal with strings for example but have different responsibilities
SystemTextStringBuilder in jdk50
LiangIntroduction to Java Programmingrevised by Dai-kaiyu37
Designing a Class cont
Classes are usually designed for use by many different customers To make a class useful in a wide range of applications the class should provide a variety of ways for customization through properties and methods
Provide a public no-arg constructor and override the equals method and the toString method defined in the Object class whenever possible
LiangIntroduction to Java Programmingrevised by Dai-kaiyu38
Designing a Class cont
Follow standard Java programming style and naming conventions
Choose informative names for classes data fields and methods
Always place the data declaration before the constructor and place constructors before methods
Always provide a constructor and initialize variables to avoid programming errors
LiangIntroduction to Java Programmingrevised by Dai-kaiyu39
Using Visibility Modifiers
Each class can present two contracts ndash one for the users of the class and one for the extenders of the class
Make the fields private and accessor methods public if they are intended for the users of the class Make the fields or method protected if they are intended for extenders of the class
A class should also hide methods not intended for client use but only for internal use within the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu40
Using the static Modifier
A property that is shared by all the instances of the class should be declared as a static property
A method that is not dependent on a specific instance should
be declared as a static method
LiangIntroduction to Java Programmingrevised by Dai-kaiyu41
Using Inheritance or Composition cont
public class Cylinder private Circle circle Constructors Methods
If polymorphism is desirable you need to use the inheritance design If you donrsquot care about polymorphism the aggregation design gives more flexibility because the classes are less
dependent using aggregation than using inheritance
public class Cylinder extends Circle Constructors Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu42
Using Interfaces or Abstract Classes
Both interfaces and abstract classes can be used to generalize common features
a strong is-an-extension-of relationship that clearly describes a parent-child relationship should be modeled using classes
since an orange is a fruit their relationship should be modeled using class inheritance
A weak is-an-extension-of relationship can be modeled using interfaces
all strings are comparable so the String class implements the Comparable interface
LiangIntroduction to Java Programmingrevised by Dai-kaiyu43
Using Interfaces or Abstract Classes cont
Interfaces are more flexible than abstract classes because a subclass can extend only one superclass but
implement any number of interfaces However interfaces cannot contain concrete methods
You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface So you can use the interface or its companion class whichever is more convenient
LiangIntroduction to Java Programmingrevised by Dai-kaiyu44
Supplement P Designing Generic Matrix 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu15
Sequence diagramsSequence diagrams describe interactions among objects by depicting the time ordering of method invocations The diagram shows1048707 The objects participating in the interaction1048707 The sequence of messages exchanged
anObject TheClass Class role
Method Invocation
Activation
anotherObject TheOtherClass
Method InvocationanotherMethod()
aMethod()
LiangIntroduction to Java Programmingrevised by Dai-kaiyu16
Sequence diagrams cont
name Name BorrowLoan address Address loan Loan borrower Borrower
setFirstName
setMi
setLastName
setStreet
setCity
setState
setZip
setAnnualInterestRate
setNumOfYears
setLoanAmount
setName
setAddress
setLoan
LiangIntroduction to Java Programmingrevised by Dai-kaiyu17
UML-Collaboration Diagram
LiangIntroduction to Java Programmingrevised by Dai-kaiyu18
UML-Collaboration Diagram
LiangIntroduction to Java Programmingrevised by Dai-kaiyu19
Statechart diagrams
Statechart diagrams describe flow of control of the object
IndicateInitial State
Transition
State1
State2
LiangIntroduction to Java Programmingrevised by Dai-kaiyu20
Statechart diagrams cont
Class Loaded
JVM loads the class for the object
Use the new operator to create the object
Object Created
Invoke the finalize method on the object
Object Destroyed
LiangIntroduction to Java Programmingrevised by Dai-kaiyu21
UML- Component Diagram
A diagram that shows the organization of and the dependencies among a set of components
LiangIntroduction to Java Programmingrevised by Dai-kaiyu22
UML-Deployment DiagramThe deployment diagram shows
Configuration of processing nodes at run-time
Communication links between these nodes
Component instances and objects that reside on them
LiangIntroduction to Java Programmingrevised by Dai-kaiyu23
Relationships among Classes
Association
Aggregation
Composition
Inheritance
LiangIntroduction to Java Programmingrevised by Dai-kaiyu24
Association
Association represents a general binary relationship that describes an
activity between two classes
Student FacultyCourse560Take Teach
03 1Teacher
public class Student
Data fields private Course[]
courseList
Constructors
Methods
public class Course
Data fields private Student[]
classList
private Faculty faculty
Constructors
Methods
public class Faculty
Data fields
private Course[]
courseList
Constructors
Methods
An association is usually represented as a data field in the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu25
Association Between Same Class
Association may exist between objects of the same class
For example a person may have a supervisor
Person Supervisor
1
1
LiangIntroduction to Java Programmingrevised by Dai-kaiyu26
Aggregation and CompositionAggregation 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
Name Address Person
Composition Aggregation
LiangIntroduction to Java Programmingrevised by Dai-kaiyu27
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
Constructors
Methods
public class Address
Data fields
Constructors
Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu28
Inner Classes TranslationIf 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu29
Inheritance
Inheritance models the is-an-extension-of
relationship between two classes
Person Faculty
public class Student extends Person
Data fields
Constructors
Methods
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu30
Weak Inheritance RelationshipA weak is-an-extension-of relationship can be represented using
interfaces For example the weak is-an-extension-of relationship
ldquostudents are comparable based on their gradesrdquo can be represented
by implementing the Comparable interface as follows
Person
Student Comparable
public class Student extends Person
implements Comparable
Data fields Constructors and
Methods
Implement the compareTo method
public int compareTo(Object object)
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu31
Class Design
1 Identify classes for the system
2 Describe attributes and methods in each class
3 Establish relationships among classes
4 Create classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu32
Example 101 Borrowing Loans
NameName BorrowerBorrowerPersonPersonLoanLoan AddressAddress
Loan
Borrower -loan Loan +Borrower() +Borrower(name Name address Address) +getLoan() Loan +setLoan(loan Loan) 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 67
Person -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 -firstName String -mi char -lastName 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu33
Example 101 Borrowing Loans cont
The following is a test program that uses the classes Name Person Address Borrower and Loan
BorrowLoanBorrowLoan RunRun
LiangIntroduction to Java Programmingrevised by Dai-kaiyu34
Example 102 The Rational Class
RationalRational RunRunTestRationalClassTestRationalClass
1
1 Add Subtract Multiply Divide
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
javalangNumber +byteValue() byte +shortValue() short +intValue() int +longVlaue() long +floatValue() float +doubleValue()double
javalangComparable compareTo(Object) int
LiangIntroduction to Java Programmingrevised by Dai-kaiyu35
Class Design Guidelines
Designing a Single Class
Using Modifiers public protected private and static
Using Inheritance or Aggregation
Using Interfaces or Abstract Classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu36
Designing a Class
A class should describe a single entity or a set of similar operations
A single entity with too many responsibilities can be broken into several classes to separate responsibilities
The String class StringBuffer class and StringTokenizer class all deal with strings for example but have different responsibilities
SystemTextStringBuilder in jdk50
LiangIntroduction to Java Programmingrevised by Dai-kaiyu37
Designing a Class cont
Classes are usually designed for use by many different customers To make a class useful in a wide range of applications the class should provide a variety of ways for customization through properties and methods
Provide a public no-arg constructor and override the equals method and the toString method defined in the Object class whenever possible
LiangIntroduction to Java Programmingrevised by Dai-kaiyu38
Designing a Class cont
Follow standard Java programming style and naming conventions
Choose informative names for classes data fields and methods
Always place the data declaration before the constructor and place constructors before methods
Always provide a constructor and initialize variables to avoid programming errors
LiangIntroduction to Java Programmingrevised by Dai-kaiyu39
Using Visibility Modifiers
Each class can present two contracts ndash one for the users of the class and one for the extenders of the class
Make the fields private and accessor methods public if they are intended for the users of the class Make the fields or method protected if they are intended for extenders of the class
A class should also hide methods not intended for client use but only for internal use within the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu40
Using the static Modifier
A property that is shared by all the instances of the class should be declared as a static property
A method that is not dependent on a specific instance should
be declared as a static method
LiangIntroduction to Java Programmingrevised by Dai-kaiyu41
Using Inheritance or Composition cont
public class Cylinder private Circle circle Constructors Methods
If polymorphism is desirable you need to use the inheritance design If you donrsquot care about polymorphism the aggregation design gives more flexibility because the classes are less
dependent using aggregation than using inheritance
public class Cylinder extends Circle Constructors Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu42
Using Interfaces or Abstract Classes
Both interfaces and abstract classes can be used to generalize common features
a strong is-an-extension-of relationship that clearly describes a parent-child relationship should be modeled using classes
since an orange is a fruit their relationship should be modeled using class inheritance
A weak is-an-extension-of relationship can be modeled using interfaces
all strings are comparable so the String class implements the Comparable interface
LiangIntroduction to Java Programmingrevised by Dai-kaiyu43
Using Interfaces or Abstract Classes cont
Interfaces are more flexible than abstract classes because a subclass can extend only one superclass but
implement any number of interfaces However interfaces cannot contain concrete methods
You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface So you can use the interface or its companion class whichever is more convenient
LiangIntroduction to Java Programmingrevised by Dai-kaiyu44
Supplement P Designing Generic Matrix 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu16
Sequence diagrams cont
name Name BorrowLoan address Address loan Loan borrower Borrower
setFirstName
setMi
setLastName
setStreet
setCity
setState
setZip
setAnnualInterestRate
setNumOfYears
setLoanAmount
setName
setAddress
setLoan
LiangIntroduction to Java Programmingrevised by Dai-kaiyu17
UML-Collaboration Diagram
LiangIntroduction to Java Programmingrevised by Dai-kaiyu18
UML-Collaboration Diagram
LiangIntroduction to Java Programmingrevised by Dai-kaiyu19
Statechart diagrams
Statechart diagrams describe flow of control of the object
IndicateInitial State
Transition
State1
State2
LiangIntroduction to Java Programmingrevised by Dai-kaiyu20
Statechart diagrams cont
Class Loaded
JVM loads the class for the object
Use the new operator to create the object
Object Created
Invoke the finalize method on the object
Object Destroyed
LiangIntroduction to Java Programmingrevised by Dai-kaiyu21
UML- Component Diagram
A diagram that shows the organization of and the dependencies among a set of components
LiangIntroduction to Java Programmingrevised by Dai-kaiyu22
UML-Deployment DiagramThe deployment diagram shows
Configuration of processing nodes at run-time
Communication links between these nodes
Component instances and objects that reside on them
LiangIntroduction to Java Programmingrevised by Dai-kaiyu23
Relationships among Classes
Association
Aggregation
Composition
Inheritance
LiangIntroduction to Java Programmingrevised by Dai-kaiyu24
Association
Association represents a general binary relationship that describes an
activity between two classes
Student FacultyCourse560Take Teach
03 1Teacher
public class Student
Data fields private Course[]
courseList
Constructors
Methods
public class Course
Data fields private Student[]
classList
private Faculty faculty
Constructors
Methods
public class Faculty
Data fields
private Course[]
courseList
Constructors
Methods
An association is usually represented as a data field in the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu25
Association Between Same Class
Association may exist between objects of the same class
For example a person may have a supervisor
Person Supervisor
1
1
LiangIntroduction to Java Programmingrevised by Dai-kaiyu26
Aggregation and CompositionAggregation 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
Name Address Person
Composition Aggregation
LiangIntroduction to Java Programmingrevised by Dai-kaiyu27
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
Constructors
Methods
public class Address
Data fields
Constructors
Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu28
Inner Classes TranslationIf 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu29
Inheritance
Inheritance models the is-an-extension-of
relationship between two classes
Person Faculty
public class Student extends Person
Data fields
Constructors
Methods
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu30
Weak Inheritance RelationshipA weak is-an-extension-of relationship can be represented using
interfaces For example the weak is-an-extension-of relationship
ldquostudents are comparable based on their gradesrdquo can be represented
by implementing the Comparable interface as follows
Person
Student Comparable
public class Student extends Person
implements Comparable
Data fields Constructors and
Methods
Implement the compareTo method
public int compareTo(Object object)
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu31
Class Design
1 Identify classes for the system
2 Describe attributes and methods in each class
3 Establish relationships among classes
4 Create classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu32
Example 101 Borrowing Loans
NameName BorrowerBorrowerPersonPersonLoanLoan AddressAddress
Loan
Borrower -loan Loan +Borrower() +Borrower(name Name address Address) +getLoan() Loan +setLoan(loan Loan) 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 67
Person -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 -firstName String -mi char -lastName 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu33
Example 101 Borrowing Loans cont
The following is a test program that uses the classes Name Person Address Borrower and Loan
BorrowLoanBorrowLoan RunRun
LiangIntroduction to Java Programmingrevised by Dai-kaiyu34
Example 102 The Rational Class
RationalRational RunRunTestRationalClassTestRationalClass
1
1 Add Subtract Multiply Divide
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
javalangNumber +byteValue() byte +shortValue() short +intValue() int +longVlaue() long +floatValue() float +doubleValue()double
javalangComparable compareTo(Object) int
LiangIntroduction to Java Programmingrevised by Dai-kaiyu35
Class Design Guidelines
Designing a Single Class
Using Modifiers public protected private and static
Using Inheritance or Aggregation
Using Interfaces or Abstract Classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu36
Designing a Class
A class should describe a single entity or a set of similar operations
A single entity with too many responsibilities can be broken into several classes to separate responsibilities
The String class StringBuffer class and StringTokenizer class all deal with strings for example but have different responsibilities
SystemTextStringBuilder in jdk50
LiangIntroduction to Java Programmingrevised by Dai-kaiyu37
Designing a Class cont
Classes are usually designed for use by many different customers To make a class useful in a wide range of applications the class should provide a variety of ways for customization through properties and methods
Provide a public no-arg constructor and override the equals method and the toString method defined in the Object class whenever possible
LiangIntroduction to Java Programmingrevised by Dai-kaiyu38
Designing a Class cont
Follow standard Java programming style and naming conventions
Choose informative names for classes data fields and methods
Always place the data declaration before the constructor and place constructors before methods
Always provide a constructor and initialize variables to avoid programming errors
LiangIntroduction to Java Programmingrevised by Dai-kaiyu39
Using Visibility Modifiers
Each class can present two contracts ndash one for the users of the class and one for the extenders of the class
Make the fields private and accessor methods public if they are intended for the users of the class Make the fields or method protected if they are intended for extenders of the class
A class should also hide methods not intended for client use but only for internal use within the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu40
Using the static Modifier
A property that is shared by all the instances of the class should be declared as a static property
A method that is not dependent on a specific instance should
be declared as a static method
LiangIntroduction to Java Programmingrevised by Dai-kaiyu41
Using Inheritance or Composition cont
public class Cylinder private Circle circle Constructors Methods
If polymorphism is desirable you need to use the inheritance design If you donrsquot care about polymorphism the aggregation design gives more flexibility because the classes are less
dependent using aggregation than using inheritance
public class Cylinder extends Circle Constructors Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu42
Using Interfaces or Abstract Classes
Both interfaces and abstract classes can be used to generalize common features
a strong is-an-extension-of relationship that clearly describes a parent-child relationship should be modeled using classes
since an orange is a fruit their relationship should be modeled using class inheritance
A weak is-an-extension-of relationship can be modeled using interfaces
all strings are comparable so the String class implements the Comparable interface
LiangIntroduction to Java Programmingrevised by Dai-kaiyu43
Using Interfaces or Abstract Classes cont
Interfaces are more flexible than abstract classes because a subclass can extend only one superclass but
implement any number of interfaces However interfaces cannot contain concrete methods
You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface So you can use the interface or its companion class whichever is more convenient
LiangIntroduction to Java Programmingrevised by Dai-kaiyu44
Supplement P Designing Generic Matrix 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu17
UML-Collaboration Diagram
LiangIntroduction to Java Programmingrevised by Dai-kaiyu18
UML-Collaboration Diagram
LiangIntroduction to Java Programmingrevised by Dai-kaiyu19
Statechart diagrams
Statechart diagrams describe flow of control of the object
IndicateInitial State
Transition
State1
State2
LiangIntroduction to Java Programmingrevised by Dai-kaiyu20
Statechart diagrams cont
Class Loaded
JVM loads the class for the object
Use the new operator to create the object
Object Created
Invoke the finalize method on the object
Object Destroyed
LiangIntroduction to Java Programmingrevised by Dai-kaiyu21
UML- Component Diagram
A diagram that shows the organization of and the dependencies among a set of components
LiangIntroduction to Java Programmingrevised by Dai-kaiyu22
UML-Deployment DiagramThe deployment diagram shows
Configuration of processing nodes at run-time
Communication links between these nodes
Component instances and objects that reside on them
LiangIntroduction to Java Programmingrevised by Dai-kaiyu23
Relationships among Classes
Association
Aggregation
Composition
Inheritance
LiangIntroduction to Java Programmingrevised by Dai-kaiyu24
Association
Association represents a general binary relationship that describes an
activity between two classes
Student FacultyCourse560Take Teach
03 1Teacher
public class Student
Data fields private Course[]
courseList
Constructors
Methods
public class Course
Data fields private Student[]
classList
private Faculty faculty
Constructors
Methods
public class Faculty
Data fields
private Course[]
courseList
Constructors
Methods
An association is usually represented as a data field in the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu25
Association Between Same Class
Association may exist between objects of the same class
For example a person may have a supervisor
Person Supervisor
1
1
LiangIntroduction to Java Programmingrevised by Dai-kaiyu26
Aggregation and CompositionAggregation 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
Name Address Person
Composition Aggregation
LiangIntroduction to Java Programmingrevised by Dai-kaiyu27
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
Constructors
Methods
public class Address
Data fields
Constructors
Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu28
Inner Classes TranslationIf 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu29
Inheritance
Inheritance models the is-an-extension-of
relationship between two classes
Person Faculty
public class Student extends Person
Data fields
Constructors
Methods
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu30
Weak Inheritance RelationshipA weak is-an-extension-of relationship can be represented using
interfaces For example the weak is-an-extension-of relationship
ldquostudents are comparable based on their gradesrdquo can be represented
by implementing the Comparable interface as follows
Person
Student Comparable
public class Student extends Person
implements Comparable
Data fields Constructors and
Methods
Implement the compareTo method
public int compareTo(Object object)
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu31
Class Design
1 Identify classes for the system
2 Describe attributes and methods in each class
3 Establish relationships among classes
4 Create classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu32
Example 101 Borrowing Loans
NameName BorrowerBorrowerPersonPersonLoanLoan AddressAddress
Loan
Borrower -loan Loan +Borrower() +Borrower(name Name address Address) +getLoan() Loan +setLoan(loan Loan) 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 67
Person -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 -firstName String -mi char -lastName 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu33
Example 101 Borrowing Loans cont
The following is a test program that uses the classes Name Person Address Borrower and Loan
BorrowLoanBorrowLoan RunRun
LiangIntroduction to Java Programmingrevised by Dai-kaiyu34
Example 102 The Rational Class
RationalRational RunRunTestRationalClassTestRationalClass
1
1 Add Subtract Multiply Divide
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
javalangNumber +byteValue() byte +shortValue() short +intValue() int +longVlaue() long +floatValue() float +doubleValue()double
javalangComparable compareTo(Object) int
LiangIntroduction to Java Programmingrevised by Dai-kaiyu35
Class Design Guidelines
Designing a Single Class
Using Modifiers public protected private and static
Using Inheritance or Aggregation
Using Interfaces or Abstract Classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu36
Designing a Class
A class should describe a single entity or a set of similar operations
A single entity with too many responsibilities can be broken into several classes to separate responsibilities
The String class StringBuffer class and StringTokenizer class all deal with strings for example but have different responsibilities
SystemTextStringBuilder in jdk50
LiangIntroduction to Java Programmingrevised by Dai-kaiyu37
Designing a Class cont
Classes are usually designed for use by many different customers To make a class useful in a wide range of applications the class should provide a variety of ways for customization through properties and methods
Provide a public no-arg constructor and override the equals method and the toString method defined in the Object class whenever possible
LiangIntroduction to Java Programmingrevised by Dai-kaiyu38
Designing a Class cont
Follow standard Java programming style and naming conventions
Choose informative names for classes data fields and methods
Always place the data declaration before the constructor and place constructors before methods
Always provide a constructor and initialize variables to avoid programming errors
LiangIntroduction to Java Programmingrevised by Dai-kaiyu39
Using Visibility Modifiers
Each class can present two contracts ndash one for the users of the class and one for the extenders of the class
Make the fields private and accessor methods public if they are intended for the users of the class Make the fields or method protected if they are intended for extenders of the class
A class should also hide methods not intended for client use but only for internal use within the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu40
Using the static Modifier
A property that is shared by all the instances of the class should be declared as a static property
A method that is not dependent on a specific instance should
be declared as a static method
LiangIntroduction to Java Programmingrevised by Dai-kaiyu41
Using Inheritance or Composition cont
public class Cylinder private Circle circle Constructors Methods
If polymorphism is desirable you need to use the inheritance design If you donrsquot care about polymorphism the aggregation design gives more flexibility because the classes are less
dependent using aggregation than using inheritance
public class Cylinder extends Circle Constructors Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu42
Using Interfaces or Abstract Classes
Both interfaces and abstract classes can be used to generalize common features
a strong is-an-extension-of relationship that clearly describes a parent-child relationship should be modeled using classes
since an orange is a fruit their relationship should be modeled using class inheritance
A weak is-an-extension-of relationship can be modeled using interfaces
all strings are comparable so the String class implements the Comparable interface
LiangIntroduction to Java Programmingrevised by Dai-kaiyu43
Using Interfaces or Abstract Classes cont
Interfaces are more flexible than abstract classes because a subclass can extend only one superclass but
implement any number of interfaces However interfaces cannot contain concrete methods
You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface So you can use the interface or its companion class whichever is more convenient
LiangIntroduction to Java Programmingrevised by Dai-kaiyu44
Supplement P Designing Generic Matrix 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu18
UML-Collaboration Diagram
LiangIntroduction to Java Programmingrevised by Dai-kaiyu19
Statechart diagrams
Statechart diagrams describe flow of control of the object
IndicateInitial State
Transition
State1
State2
LiangIntroduction to Java Programmingrevised by Dai-kaiyu20
Statechart diagrams cont
Class Loaded
JVM loads the class for the object
Use the new operator to create the object
Object Created
Invoke the finalize method on the object
Object Destroyed
LiangIntroduction to Java Programmingrevised by Dai-kaiyu21
UML- Component Diagram
A diagram that shows the organization of and the dependencies among a set of components
LiangIntroduction to Java Programmingrevised by Dai-kaiyu22
UML-Deployment DiagramThe deployment diagram shows
Configuration of processing nodes at run-time
Communication links between these nodes
Component instances and objects that reside on them
LiangIntroduction to Java Programmingrevised by Dai-kaiyu23
Relationships among Classes
Association
Aggregation
Composition
Inheritance
LiangIntroduction to Java Programmingrevised by Dai-kaiyu24
Association
Association represents a general binary relationship that describes an
activity between two classes
Student FacultyCourse560Take Teach
03 1Teacher
public class Student
Data fields private Course[]
courseList
Constructors
Methods
public class Course
Data fields private Student[]
classList
private Faculty faculty
Constructors
Methods
public class Faculty
Data fields
private Course[]
courseList
Constructors
Methods
An association is usually represented as a data field in the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu25
Association Between Same Class
Association may exist between objects of the same class
For example a person may have a supervisor
Person Supervisor
1
1
LiangIntroduction to Java Programmingrevised by Dai-kaiyu26
Aggregation and CompositionAggregation 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
Name Address Person
Composition Aggregation
LiangIntroduction to Java Programmingrevised by Dai-kaiyu27
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
Constructors
Methods
public class Address
Data fields
Constructors
Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu28
Inner Classes TranslationIf 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu29
Inheritance
Inheritance models the is-an-extension-of
relationship between two classes
Person Faculty
public class Student extends Person
Data fields
Constructors
Methods
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu30
Weak Inheritance RelationshipA weak is-an-extension-of relationship can be represented using
interfaces For example the weak is-an-extension-of relationship
ldquostudents are comparable based on their gradesrdquo can be represented
by implementing the Comparable interface as follows
Person
Student Comparable
public class Student extends Person
implements Comparable
Data fields Constructors and
Methods
Implement the compareTo method
public int compareTo(Object object)
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu31
Class Design
1 Identify classes for the system
2 Describe attributes and methods in each class
3 Establish relationships among classes
4 Create classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu32
Example 101 Borrowing Loans
NameName BorrowerBorrowerPersonPersonLoanLoan AddressAddress
Loan
Borrower -loan Loan +Borrower() +Borrower(name Name address Address) +getLoan() Loan +setLoan(loan Loan) 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 67
Person -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 -firstName String -mi char -lastName 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu33
Example 101 Borrowing Loans cont
The following is a test program that uses the classes Name Person Address Borrower and Loan
BorrowLoanBorrowLoan RunRun
LiangIntroduction to Java Programmingrevised by Dai-kaiyu34
Example 102 The Rational Class
RationalRational RunRunTestRationalClassTestRationalClass
1
1 Add Subtract Multiply Divide
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
javalangNumber +byteValue() byte +shortValue() short +intValue() int +longVlaue() long +floatValue() float +doubleValue()double
javalangComparable compareTo(Object) int
LiangIntroduction to Java Programmingrevised by Dai-kaiyu35
Class Design Guidelines
Designing a Single Class
Using Modifiers public protected private and static
Using Inheritance or Aggregation
Using Interfaces or Abstract Classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu36
Designing a Class
A class should describe a single entity or a set of similar operations
A single entity with too many responsibilities can be broken into several classes to separate responsibilities
The String class StringBuffer class and StringTokenizer class all deal with strings for example but have different responsibilities
SystemTextStringBuilder in jdk50
LiangIntroduction to Java Programmingrevised by Dai-kaiyu37
Designing a Class cont
Classes are usually designed for use by many different customers To make a class useful in a wide range of applications the class should provide a variety of ways for customization through properties and methods
Provide a public no-arg constructor and override the equals method and the toString method defined in the Object class whenever possible
LiangIntroduction to Java Programmingrevised by Dai-kaiyu38
Designing a Class cont
Follow standard Java programming style and naming conventions
Choose informative names for classes data fields and methods
Always place the data declaration before the constructor and place constructors before methods
Always provide a constructor and initialize variables to avoid programming errors
LiangIntroduction to Java Programmingrevised by Dai-kaiyu39
Using Visibility Modifiers
Each class can present two contracts ndash one for the users of the class and one for the extenders of the class
Make the fields private and accessor methods public if they are intended for the users of the class Make the fields or method protected if they are intended for extenders of the class
A class should also hide methods not intended for client use but only for internal use within the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu40
Using the static Modifier
A property that is shared by all the instances of the class should be declared as a static property
A method that is not dependent on a specific instance should
be declared as a static method
LiangIntroduction to Java Programmingrevised by Dai-kaiyu41
Using Inheritance or Composition cont
public class Cylinder private Circle circle Constructors Methods
If polymorphism is desirable you need to use the inheritance design If you donrsquot care about polymorphism the aggregation design gives more flexibility because the classes are less
dependent using aggregation than using inheritance
public class Cylinder extends Circle Constructors Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu42
Using Interfaces or Abstract Classes
Both interfaces and abstract classes can be used to generalize common features
a strong is-an-extension-of relationship that clearly describes a parent-child relationship should be modeled using classes
since an orange is a fruit their relationship should be modeled using class inheritance
A weak is-an-extension-of relationship can be modeled using interfaces
all strings are comparable so the String class implements the Comparable interface
LiangIntroduction to Java Programmingrevised by Dai-kaiyu43
Using Interfaces or Abstract Classes cont
Interfaces are more flexible than abstract classes because a subclass can extend only one superclass but
implement any number of interfaces However interfaces cannot contain concrete methods
You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface So you can use the interface or its companion class whichever is more convenient
LiangIntroduction to Java Programmingrevised by Dai-kaiyu44
Supplement P Designing Generic Matrix 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu19
Statechart diagrams
Statechart diagrams describe flow of control of the object
IndicateInitial State
Transition
State1
State2
LiangIntroduction to Java Programmingrevised by Dai-kaiyu20
Statechart diagrams cont
Class Loaded
JVM loads the class for the object
Use the new operator to create the object
Object Created
Invoke the finalize method on the object
Object Destroyed
LiangIntroduction to Java Programmingrevised by Dai-kaiyu21
UML- Component Diagram
A diagram that shows the organization of and the dependencies among a set of components
LiangIntroduction to Java Programmingrevised by Dai-kaiyu22
UML-Deployment DiagramThe deployment diagram shows
Configuration of processing nodes at run-time
Communication links between these nodes
Component instances and objects that reside on them
LiangIntroduction to Java Programmingrevised by Dai-kaiyu23
Relationships among Classes
Association
Aggregation
Composition
Inheritance
LiangIntroduction to Java Programmingrevised by Dai-kaiyu24
Association
Association represents a general binary relationship that describes an
activity between two classes
Student FacultyCourse560Take Teach
03 1Teacher
public class Student
Data fields private Course[]
courseList
Constructors
Methods
public class Course
Data fields private Student[]
classList
private Faculty faculty
Constructors
Methods
public class Faculty
Data fields
private Course[]
courseList
Constructors
Methods
An association is usually represented as a data field in the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu25
Association Between Same Class
Association may exist between objects of the same class
For example a person may have a supervisor
Person Supervisor
1
1
LiangIntroduction to Java Programmingrevised by Dai-kaiyu26
Aggregation and CompositionAggregation 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
Name Address Person
Composition Aggregation
LiangIntroduction to Java Programmingrevised by Dai-kaiyu27
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
Constructors
Methods
public class Address
Data fields
Constructors
Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu28
Inner Classes TranslationIf 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu29
Inheritance
Inheritance models the is-an-extension-of
relationship between two classes
Person Faculty
public class Student extends Person
Data fields
Constructors
Methods
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu30
Weak Inheritance RelationshipA weak is-an-extension-of relationship can be represented using
interfaces For example the weak is-an-extension-of relationship
ldquostudents are comparable based on their gradesrdquo can be represented
by implementing the Comparable interface as follows
Person
Student Comparable
public class Student extends Person
implements Comparable
Data fields Constructors and
Methods
Implement the compareTo method
public int compareTo(Object object)
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu31
Class Design
1 Identify classes for the system
2 Describe attributes and methods in each class
3 Establish relationships among classes
4 Create classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu32
Example 101 Borrowing Loans
NameName BorrowerBorrowerPersonPersonLoanLoan AddressAddress
Loan
Borrower -loan Loan +Borrower() +Borrower(name Name address Address) +getLoan() Loan +setLoan(loan Loan) 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 67
Person -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 -firstName String -mi char -lastName 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu33
Example 101 Borrowing Loans cont
The following is a test program that uses the classes Name Person Address Borrower and Loan
BorrowLoanBorrowLoan RunRun
LiangIntroduction to Java Programmingrevised by Dai-kaiyu34
Example 102 The Rational Class
RationalRational RunRunTestRationalClassTestRationalClass
1
1 Add Subtract Multiply Divide
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
javalangNumber +byteValue() byte +shortValue() short +intValue() int +longVlaue() long +floatValue() float +doubleValue()double
javalangComparable compareTo(Object) int
LiangIntroduction to Java Programmingrevised by Dai-kaiyu35
Class Design Guidelines
Designing a Single Class
Using Modifiers public protected private and static
Using Inheritance or Aggregation
Using Interfaces or Abstract Classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu36
Designing a Class
A class should describe a single entity or a set of similar operations
A single entity with too many responsibilities can be broken into several classes to separate responsibilities
The String class StringBuffer class and StringTokenizer class all deal with strings for example but have different responsibilities
SystemTextStringBuilder in jdk50
LiangIntroduction to Java Programmingrevised by Dai-kaiyu37
Designing a Class cont
Classes are usually designed for use by many different customers To make a class useful in a wide range of applications the class should provide a variety of ways for customization through properties and methods
Provide a public no-arg constructor and override the equals method and the toString method defined in the Object class whenever possible
LiangIntroduction to Java Programmingrevised by Dai-kaiyu38
Designing a Class cont
Follow standard Java programming style and naming conventions
Choose informative names for classes data fields and methods
Always place the data declaration before the constructor and place constructors before methods
Always provide a constructor and initialize variables to avoid programming errors
LiangIntroduction to Java Programmingrevised by Dai-kaiyu39
Using Visibility Modifiers
Each class can present two contracts ndash one for the users of the class and one for the extenders of the class
Make the fields private and accessor methods public if they are intended for the users of the class Make the fields or method protected if they are intended for extenders of the class
A class should also hide methods not intended for client use but only for internal use within the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu40
Using the static Modifier
A property that is shared by all the instances of the class should be declared as a static property
A method that is not dependent on a specific instance should
be declared as a static method
LiangIntroduction to Java Programmingrevised by Dai-kaiyu41
Using Inheritance or Composition cont
public class Cylinder private Circle circle Constructors Methods
If polymorphism is desirable you need to use the inheritance design If you donrsquot care about polymorphism the aggregation design gives more flexibility because the classes are less
dependent using aggregation than using inheritance
public class Cylinder extends Circle Constructors Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu42
Using Interfaces or Abstract Classes
Both interfaces and abstract classes can be used to generalize common features
a strong is-an-extension-of relationship that clearly describes a parent-child relationship should be modeled using classes
since an orange is a fruit their relationship should be modeled using class inheritance
A weak is-an-extension-of relationship can be modeled using interfaces
all strings are comparable so the String class implements the Comparable interface
LiangIntroduction to Java Programmingrevised by Dai-kaiyu43
Using Interfaces or Abstract Classes cont
Interfaces are more flexible than abstract classes because a subclass can extend only one superclass but
implement any number of interfaces However interfaces cannot contain concrete methods
You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface So you can use the interface or its companion class whichever is more convenient
LiangIntroduction to Java Programmingrevised by Dai-kaiyu44
Supplement P Designing Generic Matrix 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu20
Statechart diagrams cont
Class Loaded
JVM loads the class for the object
Use the new operator to create the object
Object Created
Invoke the finalize method on the object
Object Destroyed
LiangIntroduction to Java Programmingrevised by Dai-kaiyu21
UML- Component Diagram
A diagram that shows the organization of and the dependencies among a set of components
LiangIntroduction to Java Programmingrevised by Dai-kaiyu22
UML-Deployment DiagramThe deployment diagram shows
Configuration of processing nodes at run-time
Communication links between these nodes
Component instances and objects that reside on them
LiangIntroduction to Java Programmingrevised by Dai-kaiyu23
Relationships among Classes
Association
Aggregation
Composition
Inheritance
LiangIntroduction to Java Programmingrevised by Dai-kaiyu24
Association
Association represents a general binary relationship that describes an
activity between two classes
Student FacultyCourse560Take Teach
03 1Teacher
public class Student
Data fields private Course[]
courseList
Constructors
Methods
public class Course
Data fields private Student[]
classList
private Faculty faculty
Constructors
Methods
public class Faculty
Data fields
private Course[]
courseList
Constructors
Methods
An association is usually represented as a data field in the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu25
Association Between Same Class
Association may exist between objects of the same class
For example a person may have a supervisor
Person Supervisor
1
1
LiangIntroduction to Java Programmingrevised by Dai-kaiyu26
Aggregation and CompositionAggregation 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
Name Address Person
Composition Aggregation
LiangIntroduction to Java Programmingrevised by Dai-kaiyu27
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
Constructors
Methods
public class Address
Data fields
Constructors
Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu28
Inner Classes TranslationIf 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu29
Inheritance
Inheritance models the is-an-extension-of
relationship between two classes
Person Faculty
public class Student extends Person
Data fields
Constructors
Methods
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu30
Weak Inheritance RelationshipA weak is-an-extension-of relationship can be represented using
interfaces For example the weak is-an-extension-of relationship
ldquostudents are comparable based on their gradesrdquo can be represented
by implementing the Comparable interface as follows
Person
Student Comparable
public class Student extends Person
implements Comparable
Data fields Constructors and
Methods
Implement the compareTo method
public int compareTo(Object object)
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu31
Class Design
1 Identify classes for the system
2 Describe attributes and methods in each class
3 Establish relationships among classes
4 Create classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu32
Example 101 Borrowing Loans
NameName BorrowerBorrowerPersonPersonLoanLoan AddressAddress
Loan
Borrower -loan Loan +Borrower() +Borrower(name Name address Address) +getLoan() Loan +setLoan(loan Loan) 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 67
Person -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 -firstName String -mi char -lastName 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu33
Example 101 Borrowing Loans cont
The following is a test program that uses the classes Name Person Address Borrower and Loan
BorrowLoanBorrowLoan RunRun
LiangIntroduction to Java Programmingrevised by Dai-kaiyu34
Example 102 The Rational Class
RationalRational RunRunTestRationalClassTestRationalClass
1
1 Add Subtract Multiply Divide
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
javalangNumber +byteValue() byte +shortValue() short +intValue() int +longVlaue() long +floatValue() float +doubleValue()double
javalangComparable compareTo(Object) int
LiangIntroduction to Java Programmingrevised by Dai-kaiyu35
Class Design Guidelines
Designing a Single Class
Using Modifiers public protected private and static
Using Inheritance or Aggregation
Using Interfaces or Abstract Classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu36
Designing a Class
A class should describe a single entity or a set of similar operations
A single entity with too many responsibilities can be broken into several classes to separate responsibilities
The String class StringBuffer class and StringTokenizer class all deal with strings for example but have different responsibilities
SystemTextStringBuilder in jdk50
LiangIntroduction to Java Programmingrevised by Dai-kaiyu37
Designing a Class cont
Classes are usually designed for use by many different customers To make a class useful in a wide range of applications the class should provide a variety of ways for customization through properties and methods
Provide a public no-arg constructor and override the equals method and the toString method defined in the Object class whenever possible
LiangIntroduction to Java Programmingrevised by Dai-kaiyu38
Designing a Class cont
Follow standard Java programming style and naming conventions
Choose informative names for classes data fields and methods
Always place the data declaration before the constructor and place constructors before methods
Always provide a constructor and initialize variables to avoid programming errors
LiangIntroduction to Java Programmingrevised by Dai-kaiyu39
Using Visibility Modifiers
Each class can present two contracts ndash one for the users of the class and one for the extenders of the class
Make the fields private and accessor methods public if they are intended for the users of the class Make the fields or method protected if they are intended for extenders of the class
A class should also hide methods not intended for client use but only for internal use within the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu40
Using the static Modifier
A property that is shared by all the instances of the class should be declared as a static property
A method that is not dependent on a specific instance should
be declared as a static method
LiangIntroduction to Java Programmingrevised by Dai-kaiyu41
Using Inheritance or Composition cont
public class Cylinder private Circle circle Constructors Methods
If polymorphism is desirable you need to use the inheritance design If you donrsquot care about polymorphism the aggregation design gives more flexibility because the classes are less
dependent using aggregation than using inheritance
public class Cylinder extends Circle Constructors Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu42
Using Interfaces or Abstract Classes
Both interfaces and abstract classes can be used to generalize common features
a strong is-an-extension-of relationship that clearly describes a parent-child relationship should be modeled using classes
since an orange is a fruit their relationship should be modeled using class inheritance
A weak is-an-extension-of relationship can be modeled using interfaces
all strings are comparable so the String class implements the Comparable interface
LiangIntroduction to Java Programmingrevised by Dai-kaiyu43
Using Interfaces or Abstract Classes cont
Interfaces are more flexible than abstract classes because a subclass can extend only one superclass but
implement any number of interfaces However interfaces cannot contain concrete methods
You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface So you can use the interface or its companion class whichever is more convenient
LiangIntroduction to Java Programmingrevised by Dai-kaiyu44
Supplement P Designing Generic Matrix 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu21
UML- Component Diagram
A diagram that shows the organization of and the dependencies among a set of components
LiangIntroduction to Java Programmingrevised by Dai-kaiyu22
UML-Deployment DiagramThe deployment diagram shows
Configuration of processing nodes at run-time
Communication links between these nodes
Component instances and objects that reside on them
LiangIntroduction to Java Programmingrevised by Dai-kaiyu23
Relationships among Classes
Association
Aggregation
Composition
Inheritance
LiangIntroduction to Java Programmingrevised by Dai-kaiyu24
Association
Association represents a general binary relationship that describes an
activity between two classes
Student FacultyCourse560Take Teach
03 1Teacher
public class Student
Data fields private Course[]
courseList
Constructors
Methods
public class Course
Data fields private Student[]
classList
private Faculty faculty
Constructors
Methods
public class Faculty
Data fields
private Course[]
courseList
Constructors
Methods
An association is usually represented as a data field in the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu25
Association Between Same Class
Association may exist between objects of the same class
For example a person may have a supervisor
Person Supervisor
1
1
LiangIntroduction to Java Programmingrevised by Dai-kaiyu26
Aggregation and CompositionAggregation 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
Name Address Person
Composition Aggregation
LiangIntroduction to Java Programmingrevised by Dai-kaiyu27
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
Constructors
Methods
public class Address
Data fields
Constructors
Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu28
Inner Classes TranslationIf 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu29
Inheritance
Inheritance models the is-an-extension-of
relationship between two classes
Person Faculty
public class Student extends Person
Data fields
Constructors
Methods
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu30
Weak Inheritance RelationshipA weak is-an-extension-of relationship can be represented using
interfaces For example the weak is-an-extension-of relationship
ldquostudents are comparable based on their gradesrdquo can be represented
by implementing the Comparable interface as follows
Person
Student Comparable
public class Student extends Person
implements Comparable
Data fields Constructors and
Methods
Implement the compareTo method
public int compareTo(Object object)
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu31
Class Design
1 Identify classes for the system
2 Describe attributes and methods in each class
3 Establish relationships among classes
4 Create classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu32
Example 101 Borrowing Loans
NameName BorrowerBorrowerPersonPersonLoanLoan AddressAddress
Loan
Borrower -loan Loan +Borrower() +Borrower(name Name address Address) +getLoan() Loan +setLoan(loan Loan) 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 67
Person -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 -firstName String -mi char -lastName 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu33
Example 101 Borrowing Loans cont
The following is a test program that uses the classes Name Person Address Borrower and Loan
BorrowLoanBorrowLoan RunRun
LiangIntroduction to Java Programmingrevised by Dai-kaiyu34
Example 102 The Rational Class
RationalRational RunRunTestRationalClassTestRationalClass
1
1 Add Subtract Multiply Divide
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
javalangNumber +byteValue() byte +shortValue() short +intValue() int +longVlaue() long +floatValue() float +doubleValue()double
javalangComparable compareTo(Object) int
LiangIntroduction to Java Programmingrevised by Dai-kaiyu35
Class Design Guidelines
Designing a Single Class
Using Modifiers public protected private and static
Using Inheritance or Aggregation
Using Interfaces or Abstract Classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu36
Designing a Class
A class should describe a single entity or a set of similar operations
A single entity with too many responsibilities can be broken into several classes to separate responsibilities
The String class StringBuffer class and StringTokenizer class all deal with strings for example but have different responsibilities
SystemTextStringBuilder in jdk50
LiangIntroduction to Java Programmingrevised by Dai-kaiyu37
Designing a Class cont
Classes are usually designed for use by many different customers To make a class useful in a wide range of applications the class should provide a variety of ways for customization through properties and methods
Provide a public no-arg constructor and override the equals method and the toString method defined in the Object class whenever possible
LiangIntroduction to Java Programmingrevised by Dai-kaiyu38
Designing a Class cont
Follow standard Java programming style and naming conventions
Choose informative names for classes data fields and methods
Always place the data declaration before the constructor and place constructors before methods
Always provide a constructor and initialize variables to avoid programming errors
LiangIntroduction to Java Programmingrevised by Dai-kaiyu39
Using Visibility Modifiers
Each class can present two contracts ndash one for the users of the class and one for the extenders of the class
Make the fields private and accessor methods public if they are intended for the users of the class Make the fields or method protected if they are intended for extenders of the class
A class should also hide methods not intended for client use but only for internal use within the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu40
Using the static Modifier
A property that is shared by all the instances of the class should be declared as a static property
A method that is not dependent on a specific instance should
be declared as a static method
LiangIntroduction to Java Programmingrevised by Dai-kaiyu41
Using Inheritance or Composition cont
public class Cylinder private Circle circle Constructors Methods
If polymorphism is desirable you need to use the inheritance design If you donrsquot care about polymorphism the aggregation design gives more flexibility because the classes are less
dependent using aggregation than using inheritance
public class Cylinder extends Circle Constructors Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu42
Using Interfaces or Abstract Classes
Both interfaces and abstract classes can be used to generalize common features
a strong is-an-extension-of relationship that clearly describes a parent-child relationship should be modeled using classes
since an orange is a fruit their relationship should be modeled using class inheritance
A weak is-an-extension-of relationship can be modeled using interfaces
all strings are comparable so the String class implements the Comparable interface
LiangIntroduction to Java Programmingrevised by Dai-kaiyu43
Using Interfaces or Abstract Classes cont
Interfaces are more flexible than abstract classes because a subclass can extend only one superclass but
implement any number of interfaces However interfaces cannot contain concrete methods
You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface So you can use the interface or its companion class whichever is more convenient
LiangIntroduction to Java Programmingrevised by Dai-kaiyu44
Supplement P Designing Generic Matrix 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu22
UML-Deployment DiagramThe deployment diagram shows
Configuration of processing nodes at run-time
Communication links between these nodes
Component instances and objects that reside on them
LiangIntroduction to Java Programmingrevised by Dai-kaiyu23
Relationships among Classes
Association
Aggregation
Composition
Inheritance
LiangIntroduction to Java Programmingrevised by Dai-kaiyu24
Association
Association represents a general binary relationship that describes an
activity between two classes
Student FacultyCourse560Take Teach
03 1Teacher
public class Student
Data fields private Course[]
courseList
Constructors
Methods
public class Course
Data fields private Student[]
classList
private Faculty faculty
Constructors
Methods
public class Faculty
Data fields
private Course[]
courseList
Constructors
Methods
An association is usually represented as a data field in the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu25
Association Between Same Class
Association may exist between objects of the same class
For example a person may have a supervisor
Person Supervisor
1
1
LiangIntroduction to Java Programmingrevised by Dai-kaiyu26
Aggregation and CompositionAggregation 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
Name Address Person
Composition Aggregation
LiangIntroduction to Java Programmingrevised by Dai-kaiyu27
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
Constructors
Methods
public class Address
Data fields
Constructors
Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu28
Inner Classes TranslationIf 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu29
Inheritance
Inheritance models the is-an-extension-of
relationship between two classes
Person Faculty
public class Student extends Person
Data fields
Constructors
Methods
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu30
Weak Inheritance RelationshipA weak is-an-extension-of relationship can be represented using
interfaces For example the weak is-an-extension-of relationship
ldquostudents are comparable based on their gradesrdquo can be represented
by implementing the Comparable interface as follows
Person
Student Comparable
public class Student extends Person
implements Comparable
Data fields Constructors and
Methods
Implement the compareTo method
public int compareTo(Object object)
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu31
Class Design
1 Identify classes for the system
2 Describe attributes and methods in each class
3 Establish relationships among classes
4 Create classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu32
Example 101 Borrowing Loans
NameName BorrowerBorrowerPersonPersonLoanLoan AddressAddress
Loan
Borrower -loan Loan +Borrower() +Borrower(name Name address Address) +getLoan() Loan +setLoan(loan Loan) 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 67
Person -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 -firstName String -mi char -lastName 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu33
Example 101 Borrowing Loans cont
The following is a test program that uses the classes Name Person Address Borrower and Loan
BorrowLoanBorrowLoan RunRun
LiangIntroduction to Java Programmingrevised by Dai-kaiyu34
Example 102 The Rational Class
RationalRational RunRunTestRationalClassTestRationalClass
1
1 Add Subtract Multiply Divide
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
javalangNumber +byteValue() byte +shortValue() short +intValue() int +longVlaue() long +floatValue() float +doubleValue()double
javalangComparable compareTo(Object) int
LiangIntroduction to Java Programmingrevised by Dai-kaiyu35
Class Design Guidelines
Designing a Single Class
Using Modifiers public protected private and static
Using Inheritance or Aggregation
Using Interfaces or Abstract Classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu36
Designing a Class
A class should describe a single entity or a set of similar operations
A single entity with too many responsibilities can be broken into several classes to separate responsibilities
The String class StringBuffer class and StringTokenizer class all deal with strings for example but have different responsibilities
SystemTextStringBuilder in jdk50
LiangIntroduction to Java Programmingrevised by Dai-kaiyu37
Designing a Class cont
Classes are usually designed for use by many different customers To make a class useful in a wide range of applications the class should provide a variety of ways for customization through properties and methods
Provide a public no-arg constructor and override the equals method and the toString method defined in the Object class whenever possible
LiangIntroduction to Java Programmingrevised by Dai-kaiyu38
Designing a Class cont
Follow standard Java programming style and naming conventions
Choose informative names for classes data fields and methods
Always place the data declaration before the constructor and place constructors before methods
Always provide a constructor and initialize variables to avoid programming errors
LiangIntroduction to Java Programmingrevised by Dai-kaiyu39
Using Visibility Modifiers
Each class can present two contracts ndash one for the users of the class and one for the extenders of the class
Make the fields private and accessor methods public if they are intended for the users of the class Make the fields or method protected if they are intended for extenders of the class
A class should also hide methods not intended for client use but only for internal use within the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu40
Using the static Modifier
A property that is shared by all the instances of the class should be declared as a static property
A method that is not dependent on a specific instance should
be declared as a static method
LiangIntroduction to Java Programmingrevised by Dai-kaiyu41
Using Inheritance or Composition cont
public class Cylinder private Circle circle Constructors Methods
If polymorphism is desirable you need to use the inheritance design If you donrsquot care about polymorphism the aggregation design gives more flexibility because the classes are less
dependent using aggregation than using inheritance
public class Cylinder extends Circle Constructors Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu42
Using Interfaces or Abstract Classes
Both interfaces and abstract classes can be used to generalize common features
a strong is-an-extension-of relationship that clearly describes a parent-child relationship should be modeled using classes
since an orange is a fruit their relationship should be modeled using class inheritance
A weak is-an-extension-of relationship can be modeled using interfaces
all strings are comparable so the String class implements the Comparable interface
LiangIntroduction to Java Programmingrevised by Dai-kaiyu43
Using Interfaces or Abstract Classes cont
Interfaces are more flexible than abstract classes because a subclass can extend only one superclass but
implement any number of interfaces However interfaces cannot contain concrete methods
You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface So you can use the interface or its companion class whichever is more convenient
LiangIntroduction to Java Programmingrevised by Dai-kaiyu44
Supplement P Designing Generic Matrix 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu23
Relationships among Classes
Association
Aggregation
Composition
Inheritance
LiangIntroduction to Java Programmingrevised by Dai-kaiyu24
Association
Association represents a general binary relationship that describes an
activity between two classes
Student FacultyCourse560Take Teach
03 1Teacher
public class Student
Data fields private Course[]
courseList
Constructors
Methods
public class Course
Data fields private Student[]
classList
private Faculty faculty
Constructors
Methods
public class Faculty
Data fields
private Course[]
courseList
Constructors
Methods
An association is usually represented as a data field in the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu25
Association Between Same Class
Association may exist between objects of the same class
For example a person may have a supervisor
Person Supervisor
1
1
LiangIntroduction to Java Programmingrevised by Dai-kaiyu26
Aggregation and CompositionAggregation 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
Name Address Person
Composition Aggregation
LiangIntroduction to Java Programmingrevised by Dai-kaiyu27
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
Constructors
Methods
public class Address
Data fields
Constructors
Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu28
Inner Classes TranslationIf 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu29
Inheritance
Inheritance models the is-an-extension-of
relationship between two classes
Person Faculty
public class Student extends Person
Data fields
Constructors
Methods
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu30
Weak Inheritance RelationshipA weak is-an-extension-of relationship can be represented using
interfaces For example the weak is-an-extension-of relationship
ldquostudents are comparable based on their gradesrdquo can be represented
by implementing the Comparable interface as follows
Person
Student Comparable
public class Student extends Person
implements Comparable
Data fields Constructors and
Methods
Implement the compareTo method
public int compareTo(Object object)
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu31
Class Design
1 Identify classes for the system
2 Describe attributes and methods in each class
3 Establish relationships among classes
4 Create classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu32
Example 101 Borrowing Loans
NameName BorrowerBorrowerPersonPersonLoanLoan AddressAddress
Loan
Borrower -loan Loan +Borrower() +Borrower(name Name address Address) +getLoan() Loan +setLoan(loan Loan) 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 67
Person -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 -firstName String -mi char -lastName 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu33
Example 101 Borrowing Loans cont
The following is a test program that uses the classes Name Person Address Borrower and Loan
BorrowLoanBorrowLoan RunRun
LiangIntroduction to Java Programmingrevised by Dai-kaiyu34
Example 102 The Rational Class
RationalRational RunRunTestRationalClassTestRationalClass
1
1 Add Subtract Multiply Divide
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
javalangNumber +byteValue() byte +shortValue() short +intValue() int +longVlaue() long +floatValue() float +doubleValue()double
javalangComparable compareTo(Object) int
LiangIntroduction to Java Programmingrevised by Dai-kaiyu35
Class Design Guidelines
Designing a Single Class
Using Modifiers public protected private and static
Using Inheritance or Aggregation
Using Interfaces or Abstract Classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu36
Designing a Class
A class should describe a single entity or a set of similar operations
A single entity with too many responsibilities can be broken into several classes to separate responsibilities
The String class StringBuffer class and StringTokenizer class all deal with strings for example but have different responsibilities
SystemTextStringBuilder in jdk50
LiangIntroduction to Java Programmingrevised by Dai-kaiyu37
Designing a Class cont
Classes are usually designed for use by many different customers To make a class useful in a wide range of applications the class should provide a variety of ways for customization through properties and methods
Provide a public no-arg constructor and override the equals method and the toString method defined in the Object class whenever possible
LiangIntroduction to Java Programmingrevised by Dai-kaiyu38
Designing a Class cont
Follow standard Java programming style and naming conventions
Choose informative names for classes data fields and methods
Always place the data declaration before the constructor and place constructors before methods
Always provide a constructor and initialize variables to avoid programming errors
LiangIntroduction to Java Programmingrevised by Dai-kaiyu39
Using Visibility Modifiers
Each class can present two contracts ndash one for the users of the class and one for the extenders of the class
Make the fields private and accessor methods public if they are intended for the users of the class Make the fields or method protected if they are intended for extenders of the class
A class should also hide methods not intended for client use but only for internal use within the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu40
Using the static Modifier
A property that is shared by all the instances of the class should be declared as a static property
A method that is not dependent on a specific instance should
be declared as a static method
LiangIntroduction to Java Programmingrevised by Dai-kaiyu41
Using Inheritance or Composition cont
public class Cylinder private Circle circle Constructors Methods
If polymorphism is desirable you need to use the inheritance design If you donrsquot care about polymorphism the aggregation design gives more flexibility because the classes are less
dependent using aggregation than using inheritance
public class Cylinder extends Circle Constructors Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu42
Using Interfaces or Abstract Classes
Both interfaces and abstract classes can be used to generalize common features
a strong is-an-extension-of relationship that clearly describes a parent-child relationship should be modeled using classes
since an orange is a fruit their relationship should be modeled using class inheritance
A weak is-an-extension-of relationship can be modeled using interfaces
all strings are comparable so the String class implements the Comparable interface
LiangIntroduction to Java Programmingrevised by Dai-kaiyu43
Using Interfaces or Abstract Classes cont
Interfaces are more flexible than abstract classes because a subclass can extend only one superclass but
implement any number of interfaces However interfaces cannot contain concrete methods
You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface So you can use the interface or its companion class whichever is more convenient
LiangIntroduction to Java Programmingrevised by Dai-kaiyu44
Supplement P Designing Generic Matrix 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu24
Association
Association represents a general binary relationship that describes an
activity between two classes
Student FacultyCourse560Take Teach
03 1Teacher
public class Student
Data fields private Course[]
courseList
Constructors
Methods
public class Course
Data fields private Student[]
classList
private Faculty faculty
Constructors
Methods
public class Faculty
Data fields
private Course[]
courseList
Constructors
Methods
An association is usually represented as a data field in the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu25
Association Between Same Class
Association may exist between objects of the same class
For example a person may have a supervisor
Person Supervisor
1
1
LiangIntroduction to Java Programmingrevised by Dai-kaiyu26
Aggregation and CompositionAggregation 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
Name Address Person
Composition Aggregation
LiangIntroduction to Java Programmingrevised by Dai-kaiyu27
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
Constructors
Methods
public class Address
Data fields
Constructors
Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu28
Inner Classes TranslationIf 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu29
Inheritance
Inheritance models the is-an-extension-of
relationship between two classes
Person Faculty
public class Student extends Person
Data fields
Constructors
Methods
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu30
Weak Inheritance RelationshipA weak is-an-extension-of relationship can be represented using
interfaces For example the weak is-an-extension-of relationship
ldquostudents are comparable based on their gradesrdquo can be represented
by implementing the Comparable interface as follows
Person
Student Comparable
public class Student extends Person
implements Comparable
Data fields Constructors and
Methods
Implement the compareTo method
public int compareTo(Object object)
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu31
Class Design
1 Identify classes for the system
2 Describe attributes and methods in each class
3 Establish relationships among classes
4 Create classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu32
Example 101 Borrowing Loans
NameName BorrowerBorrowerPersonPersonLoanLoan AddressAddress
Loan
Borrower -loan Loan +Borrower() +Borrower(name Name address Address) +getLoan() Loan +setLoan(loan Loan) 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 67
Person -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 -firstName String -mi char -lastName 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu33
Example 101 Borrowing Loans cont
The following is a test program that uses the classes Name Person Address Borrower and Loan
BorrowLoanBorrowLoan RunRun
LiangIntroduction to Java Programmingrevised by Dai-kaiyu34
Example 102 The Rational Class
RationalRational RunRunTestRationalClassTestRationalClass
1
1 Add Subtract Multiply Divide
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
javalangNumber +byteValue() byte +shortValue() short +intValue() int +longVlaue() long +floatValue() float +doubleValue()double
javalangComparable compareTo(Object) int
LiangIntroduction to Java Programmingrevised by Dai-kaiyu35
Class Design Guidelines
Designing a Single Class
Using Modifiers public protected private and static
Using Inheritance or Aggregation
Using Interfaces or Abstract Classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu36
Designing a Class
A class should describe a single entity or a set of similar operations
A single entity with too many responsibilities can be broken into several classes to separate responsibilities
The String class StringBuffer class and StringTokenizer class all deal with strings for example but have different responsibilities
SystemTextStringBuilder in jdk50
LiangIntroduction to Java Programmingrevised by Dai-kaiyu37
Designing a Class cont
Classes are usually designed for use by many different customers To make a class useful in a wide range of applications the class should provide a variety of ways for customization through properties and methods
Provide a public no-arg constructor and override the equals method and the toString method defined in the Object class whenever possible
LiangIntroduction to Java Programmingrevised by Dai-kaiyu38
Designing a Class cont
Follow standard Java programming style and naming conventions
Choose informative names for classes data fields and methods
Always place the data declaration before the constructor and place constructors before methods
Always provide a constructor and initialize variables to avoid programming errors
LiangIntroduction to Java Programmingrevised by Dai-kaiyu39
Using Visibility Modifiers
Each class can present two contracts ndash one for the users of the class and one for the extenders of the class
Make the fields private and accessor methods public if they are intended for the users of the class Make the fields or method protected if they are intended for extenders of the class
A class should also hide methods not intended for client use but only for internal use within the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu40
Using the static Modifier
A property that is shared by all the instances of the class should be declared as a static property
A method that is not dependent on a specific instance should
be declared as a static method
LiangIntroduction to Java Programmingrevised by Dai-kaiyu41
Using Inheritance or Composition cont
public class Cylinder private Circle circle Constructors Methods
If polymorphism is desirable you need to use the inheritance design If you donrsquot care about polymorphism the aggregation design gives more flexibility because the classes are less
dependent using aggregation than using inheritance
public class Cylinder extends Circle Constructors Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu42
Using Interfaces or Abstract Classes
Both interfaces and abstract classes can be used to generalize common features
a strong is-an-extension-of relationship that clearly describes a parent-child relationship should be modeled using classes
since an orange is a fruit their relationship should be modeled using class inheritance
A weak is-an-extension-of relationship can be modeled using interfaces
all strings are comparable so the String class implements the Comparable interface
LiangIntroduction to Java Programmingrevised by Dai-kaiyu43
Using Interfaces or Abstract Classes cont
Interfaces are more flexible than abstract classes because a subclass can extend only one superclass but
implement any number of interfaces However interfaces cannot contain concrete methods
You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface So you can use the interface or its companion class whichever is more convenient
LiangIntroduction to Java Programmingrevised by Dai-kaiyu44
Supplement P Designing Generic Matrix 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu25
Association Between Same Class
Association may exist between objects of the same class
For example a person may have a supervisor
Person Supervisor
1
1
LiangIntroduction to Java Programmingrevised by Dai-kaiyu26
Aggregation and CompositionAggregation 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
Name Address Person
Composition Aggregation
LiangIntroduction to Java Programmingrevised by Dai-kaiyu27
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
Constructors
Methods
public class Address
Data fields
Constructors
Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu28
Inner Classes TranslationIf 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu29
Inheritance
Inheritance models the is-an-extension-of
relationship between two classes
Person Faculty
public class Student extends Person
Data fields
Constructors
Methods
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu30
Weak Inheritance RelationshipA weak is-an-extension-of relationship can be represented using
interfaces For example the weak is-an-extension-of relationship
ldquostudents are comparable based on their gradesrdquo can be represented
by implementing the Comparable interface as follows
Person
Student Comparable
public class Student extends Person
implements Comparable
Data fields Constructors and
Methods
Implement the compareTo method
public int compareTo(Object object)
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu31
Class Design
1 Identify classes for the system
2 Describe attributes and methods in each class
3 Establish relationships among classes
4 Create classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu32
Example 101 Borrowing Loans
NameName BorrowerBorrowerPersonPersonLoanLoan AddressAddress
Loan
Borrower -loan Loan +Borrower() +Borrower(name Name address Address) +getLoan() Loan +setLoan(loan Loan) 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 67
Person -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 -firstName String -mi char -lastName 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu33
Example 101 Borrowing Loans cont
The following is a test program that uses the classes Name Person Address Borrower and Loan
BorrowLoanBorrowLoan RunRun
LiangIntroduction to Java Programmingrevised by Dai-kaiyu34
Example 102 The Rational Class
RationalRational RunRunTestRationalClassTestRationalClass
1
1 Add Subtract Multiply Divide
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
javalangNumber +byteValue() byte +shortValue() short +intValue() int +longVlaue() long +floatValue() float +doubleValue()double
javalangComparable compareTo(Object) int
LiangIntroduction to Java Programmingrevised by Dai-kaiyu35
Class Design Guidelines
Designing a Single Class
Using Modifiers public protected private and static
Using Inheritance or Aggregation
Using Interfaces or Abstract Classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu36
Designing a Class
A class should describe a single entity or a set of similar operations
A single entity with too many responsibilities can be broken into several classes to separate responsibilities
The String class StringBuffer class and StringTokenizer class all deal with strings for example but have different responsibilities
SystemTextStringBuilder in jdk50
LiangIntroduction to Java Programmingrevised by Dai-kaiyu37
Designing a Class cont
Classes are usually designed for use by many different customers To make a class useful in a wide range of applications the class should provide a variety of ways for customization through properties and methods
Provide a public no-arg constructor and override the equals method and the toString method defined in the Object class whenever possible
LiangIntroduction to Java Programmingrevised by Dai-kaiyu38
Designing a Class cont
Follow standard Java programming style and naming conventions
Choose informative names for classes data fields and methods
Always place the data declaration before the constructor and place constructors before methods
Always provide a constructor and initialize variables to avoid programming errors
LiangIntroduction to Java Programmingrevised by Dai-kaiyu39
Using Visibility Modifiers
Each class can present two contracts ndash one for the users of the class and one for the extenders of the class
Make the fields private and accessor methods public if they are intended for the users of the class Make the fields or method protected if they are intended for extenders of the class
A class should also hide methods not intended for client use but only for internal use within the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu40
Using the static Modifier
A property that is shared by all the instances of the class should be declared as a static property
A method that is not dependent on a specific instance should
be declared as a static method
LiangIntroduction to Java Programmingrevised by Dai-kaiyu41
Using Inheritance or Composition cont
public class Cylinder private Circle circle Constructors Methods
If polymorphism is desirable you need to use the inheritance design If you donrsquot care about polymorphism the aggregation design gives more flexibility because the classes are less
dependent using aggregation than using inheritance
public class Cylinder extends Circle Constructors Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu42
Using Interfaces or Abstract Classes
Both interfaces and abstract classes can be used to generalize common features
a strong is-an-extension-of relationship that clearly describes a parent-child relationship should be modeled using classes
since an orange is a fruit their relationship should be modeled using class inheritance
A weak is-an-extension-of relationship can be modeled using interfaces
all strings are comparable so the String class implements the Comparable interface
LiangIntroduction to Java Programmingrevised by Dai-kaiyu43
Using Interfaces or Abstract Classes cont
Interfaces are more flexible than abstract classes because a subclass can extend only one superclass but
implement any number of interfaces However interfaces cannot contain concrete methods
You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface So you can use the interface or its companion class whichever is more convenient
LiangIntroduction to Java Programmingrevised by Dai-kaiyu44
Supplement P Designing Generic Matrix 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu26
Aggregation and CompositionAggregation 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
Name Address Person
Composition Aggregation
LiangIntroduction to Java Programmingrevised by Dai-kaiyu27
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
Constructors
Methods
public class Address
Data fields
Constructors
Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu28
Inner Classes TranslationIf 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu29
Inheritance
Inheritance models the is-an-extension-of
relationship between two classes
Person Faculty
public class Student extends Person
Data fields
Constructors
Methods
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu30
Weak Inheritance RelationshipA weak is-an-extension-of relationship can be represented using
interfaces For example the weak is-an-extension-of relationship
ldquostudents are comparable based on their gradesrdquo can be represented
by implementing the Comparable interface as follows
Person
Student Comparable
public class Student extends Person
implements Comparable
Data fields Constructors and
Methods
Implement the compareTo method
public int compareTo(Object object)
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu31
Class Design
1 Identify classes for the system
2 Describe attributes and methods in each class
3 Establish relationships among classes
4 Create classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu32
Example 101 Borrowing Loans
NameName BorrowerBorrowerPersonPersonLoanLoan AddressAddress
Loan
Borrower -loan Loan +Borrower() +Borrower(name Name address Address) +getLoan() Loan +setLoan(loan Loan) 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 67
Person -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 -firstName String -mi char -lastName 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu33
Example 101 Borrowing Loans cont
The following is a test program that uses the classes Name Person Address Borrower and Loan
BorrowLoanBorrowLoan RunRun
LiangIntroduction to Java Programmingrevised by Dai-kaiyu34
Example 102 The Rational Class
RationalRational RunRunTestRationalClassTestRationalClass
1
1 Add Subtract Multiply Divide
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
javalangNumber +byteValue() byte +shortValue() short +intValue() int +longVlaue() long +floatValue() float +doubleValue()double
javalangComparable compareTo(Object) int
LiangIntroduction to Java Programmingrevised by Dai-kaiyu35
Class Design Guidelines
Designing a Single Class
Using Modifiers public protected private and static
Using Inheritance or Aggregation
Using Interfaces or Abstract Classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu36
Designing a Class
A class should describe a single entity or a set of similar operations
A single entity with too many responsibilities can be broken into several classes to separate responsibilities
The String class StringBuffer class and StringTokenizer class all deal with strings for example but have different responsibilities
SystemTextStringBuilder in jdk50
LiangIntroduction to Java Programmingrevised by Dai-kaiyu37
Designing a Class cont
Classes are usually designed for use by many different customers To make a class useful in a wide range of applications the class should provide a variety of ways for customization through properties and methods
Provide a public no-arg constructor and override the equals method and the toString method defined in the Object class whenever possible
LiangIntroduction to Java Programmingrevised by Dai-kaiyu38
Designing a Class cont
Follow standard Java programming style and naming conventions
Choose informative names for classes data fields and methods
Always place the data declaration before the constructor and place constructors before methods
Always provide a constructor and initialize variables to avoid programming errors
LiangIntroduction to Java Programmingrevised by Dai-kaiyu39
Using Visibility Modifiers
Each class can present two contracts ndash one for the users of the class and one for the extenders of the class
Make the fields private and accessor methods public if they are intended for the users of the class Make the fields or method protected if they are intended for extenders of the class
A class should also hide methods not intended for client use but only for internal use within the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu40
Using the static Modifier
A property that is shared by all the instances of the class should be declared as a static property
A method that is not dependent on a specific instance should
be declared as a static method
LiangIntroduction to Java Programmingrevised by Dai-kaiyu41
Using Inheritance or Composition cont
public class Cylinder private Circle circle Constructors Methods
If polymorphism is desirable you need to use the inheritance design If you donrsquot care about polymorphism the aggregation design gives more flexibility because the classes are less
dependent using aggregation than using inheritance
public class Cylinder extends Circle Constructors Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu42
Using Interfaces or Abstract Classes
Both interfaces and abstract classes can be used to generalize common features
a strong is-an-extension-of relationship that clearly describes a parent-child relationship should be modeled using classes
since an orange is a fruit their relationship should be modeled using class inheritance
A weak is-an-extension-of relationship can be modeled using interfaces
all strings are comparable so the String class implements the Comparable interface
LiangIntroduction to Java Programmingrevised by Dai-kaiyu43
Using Interfaces or Abstract Classes cont
Interfaces are more flexible than abstract classes because a subclass can extend only one superclass but
implement any number of interfaces However interfaces cannot contain concrete methods
You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface So you can use the interface or its companion class whichever is more convenient
LiangIntroduction to Java Programmingrevised by Dai-kaiyu44
Supplement P Designing Generic Matrix 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu27
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
Constructors
Methods
public class Address
Data fields
Constructors
Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu28
Inner Classes TranslationIf 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu29
Inheritance
Inheritance models the is-an-extension-of
relationship between two classes
Person Faculty
public class Student extends Person
Data fields
Constructors
Methods
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu30
Weak Inheritance RelationshipA weak is-an-extension-of relationship can be represented using
interfaces For example the weak is-an-extension-of relationship
ldquostudents are comparable based on their gradesrdquo can be represented
by implementing the Comparable interface as follows
Person
Student Comparable
public class Student extends Person
implements Comparable
Data fields Constructors and
Methods
Implement the compareTo method
public int compareTo(Object object)
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu31
Class Design
1 Identify classes for the system
2 Describe attributes and methods in each class
3 Establish relationships among classes
4 Create classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu32
Example 101 Borrowing Loans
NameName BorrowerBorrowerPersonPersonLoanLoan AddressAddress
Loan
Borrower -loan Loan +Borrower() +Borrower(name Name address Address) +getLoan() Loan +setLoan(loan Loan) 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 67
Person -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 -firstName String -mi char -lastName 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu33
Example 101 Borrowing Loans cont
The following is a test program that uses the classes Name Person Address Borrower and Loan
BorrowLoanBorrowLoan RunRun
LiangIntroduction to Java Programmingrevised by Dai-kaiyu34
Example 102 The Rational Class
RationalRational RunRunTestRationalClassTestRationalClass
1
1 Add Subtract Multiply Divide
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
javalangNumber +byteValue() byte +shortValue() short +intValue() int +longVlaue() long +floatValue() float +doubleValue()double
javalangComparable compareTo(Object) int
LiangIntroduction to Java Programmingrevised by Dai-kaiyu35
Class Design Guidelines
Designing a Single Class
Using Modifiers public protected private and static
Using Inheritance or Aggregation
Using Interfaces or Abstract Classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu36
Designing a Class
A class should describe a single entity or a set of similar operations
A single entity with too many responsibilities can be broken into several classes to separate responsibilities
The String class StringBuffer class and StringTokenizer class all deal with strings for example but have different responsibilities
SystemTextStringBuilder in jdk50
LiangIntroduction to Java Programmingrevised by Dai-kaiyu37
Designing a Class cont
Classes are usually designed for use by many different customers To make a class useful in a wide range of applications the class should provide a variety of ways for customization through properties and methods
Provide a public no-arg constructor and override the equals method and the toString method defined in the Object class whenever possible
LiangIntroduction to Java Programmingrevised by Dai-kaiyu38
Designing a Class cont
Follow standard Java programming style and naming conventions
Choose informative names for classes data fields and methods
Always place the data declaration before the constructor and place constructors before methods
Always provide a constructor and initialize variables to avoid programming errors
LiangIntroduction to Java Programmingrevised by Dai-kaiyu39
Using Visibility Modifiers
Each class can present two contracts ndash one for the users of the class and one for the extenders of the class
Make the fields private and accessor methods public if they are intended for the users of the class Make the fields or method protected if they are intended for extenders of the class
A class should also hide methods not intended for client use but only for internal use within the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu40
Using the static Modifier
A property that is shared by all the instances of the class should be declared as a static property
A method that is not dependent on a specific instance should
be declared as a static method
LiangIntroduction to Java Programmingrevised by Dai-kaiyu41
Using Inheritance or Composition cont
public class Cylinder private Circle circle Constructors Methods
If polymorphism is desirable you need to use the inheritance design If you donrsquot care about polymorphism the aggregation design gives more flexibility because the classes are less
dependent using aggregation than using inheritance
public class Cylinder extends Circle Constructors Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu42
Using Interfaces or Abstract Classes
Both interfaces and abstract classes can be used to generalize common features
a strong is-an-extension-of relationship that clearly describes a parent-child relationship should be modeled using classes
since an orange is a fruit their relationship should be modeled using class inheritance
A weak is-an-extension-of relationship can be modeled using interfaces
all strings are comparable so the String class implements the Comparable interface
LiangIntroduction to Java Programmingrevised by Dai-kaiyu43
Using Interfaces or Abstract Classes cont
Interfaces are more flexible than abstract classes because a subclass can extend only one superclass but
implement any number of interfaces However interfaces cannot contain concrete methods
You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface So you can use the interface or its companion class whichever is more convenient
LiangIntroduction to Java Programmingrevised by Dai-kaiyu44
Supplement P Designing Generic Matrix 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu28
Inner Classes TranslationIf 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu29
Inheritance
Inheritance models the is-an-extension-of
relationship between two classes
Person Faculty
public class Student extends Person
Data fields
Constructors
Methods
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu30
Weak Inheritance RelationshipA weak is-an-extension-of relationship can be represented using
interfaces For example the weak is-an-extension-of relationship
ldquostudents are comparable based on their gradesrdquo can be represented
by implementing the Comparable interface as follows
Person
Student Comparable
public class Student extends Person
implements Comparable
Data fields Constructors and
Methods
Implement the compareTo method
public int compareTo(Object object)
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu31
Class Design
1 Identify classes for the system
2 Describe attributes and methods in each class
3 Establish relationships among classes
4 Create classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu32
Example 101 Borrowing Loans
NameName BorrowerBorrowerPersonPersonLoanLoan AddressAddress
Loan
Borrower -loan Loan +Borrower() +Borrower(name Name address Address) +getLoan() Loan +setLoan(loan Loan) 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 67
Person -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 -firstName String -mi char -lastName 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu33
Example 101 Borrowing Loans cont
The following is a test program that uses the classes Name Person Address Borrower and Loan
BorrowLoanBorrowLoan RunRun
LiangIntroduction to Java Programmingrevised by Dai-kaiyu34
Example 102 The Rational Class
RationalRational RunRunTestRationalClassTestRationalClass
1
1 Add Subtract Multiply Divide
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
javalangNumber +byteValue() byte +shortValue() short +intValue() int +longVlaue() long +floatValue() float +doubleValue()double
javalangComparable compareTo(Object) int
LiangIntroduction to Java Programmingrevised by Dai-kaiyu35
Class Design Guidelines
Designing a Single Class
Using Modifiers public protected private and static
Using Inheritance or Aggregation
Using Interfaces or Abstract Classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu36
Designing a Class
A class should describe a single entity or a set of similar operations
A single entity with too many responsibilities can be broken into several classes to separate responsibilities
The String class StringBuffer class and StringTokenizer class all deal with strings for example but have different responsibilities
SystemTextStringBuilder in jdk50
LiangIntroduction to Java Programmingrevised by Dai-kaiyu37
Designing a Class cont
Classes are usually designed for use by many different customers To make a class useful in a wide range of applications the class should provide a variety of ways for customization through properties and methods
Provide a public no-arg constructor and override the equals method and the toString method defined in the Object class whenever possible
LiangIntroduction to Java Programmingrevised by Dai-kaiyu38
Designing a Class cont
Follow standard Java programming style and naming conventions
Choose informative names for classes data fields and methods
Always place the data declaration before the constructor and place constructors before methods
Always provide a constructor and initialize variables to avoid programming errors
LiangIntroduction to Java Programmingrevised by Dai-kaiyu39
Using Visibility Modifiers
Each class can present two contracts ndash one for the users of the class and one for the extenders of the class
Make the fields private and accessor methods public if they are intended for the users of the class Make the fields or method protected if they are intended for extenders of the class
A class should also hide methods not intended for client use but only for internal use within the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu40
Using the static Modifier
A property that is shared by all the instances of the class should be declared as a static property
A method that is not dependent on a specific instance should
be declared as a static method
LiangIntroduction to Java Programmingrevised by Dai-kaiyu41
Using Inheritance or Composition cont
public class Cylinder private Circle circle Constructors Methods
If polymorphism is desirable you need to use the inheritance design If you donrsquot care about polymorphism the aggregation design gives more flexibility because the classes are less
dependent using aggregation than using inheritance
public class Cylinder extends Circle Constructors Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu42
Using Interfaces or Abstract Classes
Both interfaces and abstract classes can be used to generalize common features
a strong is-an-extension-of relationship that clearly describes a parent-child relationship should be modeled using classes
since an orange is a fruit their relationship should be modeled using class inheritance
A weak is-an-extension-of relationship can be modeled using interfaces
all strings are comparable so the String class implements the Comparable interface
LiangIntroduction to Java Programmingrevised by Dai-kaiyu43
Using Interfaces or Abstract Classes cont
Interfaces are more flexible than abstract classes because a subclass can extend only one superclass but
implement any number of interfaces However interfaces cannot contain concrete methods
You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface So you can use the interface or its companion class whichever is more convenient
LiangIntroduction to Java Programmingrevised by Dai-kaiyu44
Supplement P Designing Generic Matrix 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu29
Inheritance
Inheritance models the is-an-extension-of
relationship between two classes
Person Faculty
public class Student extends Person
Data fields
Constructors
Methods
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu30
Weak Inheritance RelationshipA weak is-an-extension-of relationship can be represented using
interfaces For example the weak is-an-extension-of relationship
ldquostudents are comparable based on their gradesrdquo can be represented
by implementing the Comparable interface as follows
Person
Student Comparable
public class Student extends Person
implements Comparable
Data fields Constructors and
Methods
Implement the compareTo method
public int compareTo(Object object)
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu31
Class Design
1 Identify classes for the system
2 Describe attributes and methods in each class
3 Establish relationships among classes
4 Create classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu32
Example 101 Borrowing Loans
NameName BorrowerBorrowerPersonPersonLoanLoan AddressAddress
Loan
Borrower -loan Loan +Borrower() +Borrower(name Name address Address) +getLoan() Loan +setLoan(loan Loan) 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 67
Person -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 -firstName String -mi char -lastName 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu33
Example 101 Borrowing Loans cont
The following is a test program that uses the classes Name Person Address Borrower and Loan
BorrowLoanBorrowLoan RunRun
LiangIntroduction to Java Programmingrevised by Dai-kaiyu34
Example 102 The Rational Class
RationalRational RunRunTestRationalClassTestRationalClass
1
1 Add Subtract Multiply Divide
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
javalangNumber +byteValue() byte +shortValue() short +intValue() int +longVlaue() long +floatValue() float +doubleValue()double
javalangComparable compareTo(Object) int
LiangIntroduction to Java Programmingrevised by Dai-kaiyu35
Class Design Guidelines
Designing a Single Class
Using Modifiers public protected private and static
Using Inheritance or Aggregation
Using Interfaces or Abstract Classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu36
Designing a Class
A class should describe a single entity or a set of similar operations
A single entity with too many responsibilities can be broken into several classes to separate responsibilities
The String class StringBuffer class and StringTokenizer class all deal with strings for example but have different responsibilities
SystemTextStringBuilder in jdk50
LiangIntroduction to Java Programmingrevised by Dai-kaiyu37
Designing a Class cont
Classes are usually designed for use by many different customers To make a class useful in a wide range of applications the class should provide a variety of ways for customization through properties and methods
Provide a public no-arg constructor and override the equals method and the toString method defined in the Object class whenever possible
LiangIntroduction to Java Programmingrevised by Dai-kaiyu38
Designing a Class cont
Follow standard Java programming style and naming conventions
Choose informative names for classes data fields and methods
Always place the data declaration before the constructor and place constructors before methods
Always provide a constructor and initialize variables to avoid programming errors
LiangIntroduction to Java Programmingrevised by Dai-kaiyu39
Using Visibility Modifiers
Each class can present two contracts ndash one for the users of the class and one for the extenders of the class
Make the fields private and accessor methods public if they are intended for the users of the class Make the fields or method protected if they are intended for extenders of the class
A class should also hide methods not intended for client use but only for internal use within the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu40
Using the static Modifier
A property that is shared by all the instances of the class should be declared as a static property
A method that is not dependent on a specific instance should
be declared as a static method
LiangIntroduction to Java Programmingrevised by Dai-kaiyu41
Using Inheritance or Composition cont
public class Cylinder private Circle circle Constructors Methods
If polymorphism is desirable you need to use the inheritance design If you donrsquot care about polymorphism the aggregation design gives more flexibility because the classes are less
dependent using aggregation than using inheritance
public class Cylinder extends Circle Constructors Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu42
Using Interfaces or Abstract Classes
Both interfaces and abstract classes can be used to generalize common features
a strong is-an-extension-of relationship that clearly describes a parent-child relationship should be modeled using classes
since an orange is a fruit their relationship should be modeled using class inheritance
A weak is-an-extension-of relationship can be modeled using interfaces
all strings are comparable so the String class implements the Comparable interface
LiangIntroduction to Java Programmingrevised by Dai-kaiyu43
Using Interfaces or Abstract Classes cont
Interfaces are more flexible than abstract classes because a subclass can extend only one superclass but
implement any number of interfaces However interfaces cannot contain concrete methods
You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface So you can use the interface or its companion class whichever is more convenient
LiangIntroduction to Java Programmingrevised by Dai-kaiyu44
Supplement P Designing Generic Matrix 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu30
Weak Inheritance RelationshipA weak is-an-extension-of relationship can be represented using
interfaces For example the weak is-an-extension-of relationship
ldquostudents are comparable based on their gradesrdquo can be represented
by implementing the Comparable interface as follows
Person
Student Comparable
public class Student extends Person
implements Comparable
Data fields Constructors and
Methods
Implement the compareTo method
public int compareTo(Object object)
(A) (B)
LiangIntroduction to Java Programmingrevised by Dai-kaiyu31
Class Design
1 Identify classes for the system
2 Describe attributes and methods in each class
3 Establish relationships among classes
4 Create classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu32
Example 101 Borrowing Loans
NameName BorrowerBorrowerPersonPersonLoanLoan AddressAddress
Loan
Borrower -loan Loan +Borrower() +Borrower(name Name address Address) +getLoan() Loan +setLoan(loan Loan) 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 67
Person -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 -firstName String -mi char -lastName 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu33
Example 101 Borrowing Loans cont
The following is a test program that uses the classes Name Person Address Borrower and Loan
BorrowLoanBorrowLoan RunRun
LiangIntroduction to Java Programmingrevised by Dai-kaiyu34
Example 102 The Rational Class
RationalRational RunRunTestRationalClassTestRationalClass
1
1 Add Subtract Multiply Divide
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
javalangNumber +byteValue() byte +shortValue() short +intValue() int +longVlaue() long +floatValue() float +doubleValue()double
javalangComparable compareTo(Object) int
LiangIntroduction to Java Programmingrevised by Dai-kaiyu35
Class Design Guidelines
Designing a Single Class
Using Modifiers public protected private and static
Using Inheritance or Aggregation
Using Interfaces or Abstract Classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu36
Designing a Class
A class should describe a single entity or a set of similar operations
A single entity with too many responsibilities can be broken into several classes to separate responsibilities
The String class StringBuffer class and StringTokenizer class all deal with strings for example but have different responsibilities
SystemTextStringBuilder in jdk50
LiangIntroduction to Java Programmingrevised by Dai-kaiyu37
Designing a Class cont
Classes are usually designed for use by many different customers To make a class useful in a wide range of applications the class should provide a variety of ways for customization through properties and methods
Provide a public no-arg constructor and override the equals method and the toString method defined in the Object class whenever possible
LiangIntroduction to Java Programmingrevised by Dai-kaiyu38
Designing a Class cont
Follow standard Java programming style and naming conventions
Choose informative names for classes data fields and methods
Always place the data declaration before the constructor and place constructors before methods
Always provide a constructor and initialize variables to avoid programming errors
LiangIntroduction to Java Programmingrevised by Dai-kaiyu39
Using Visibility Modifiers
Each class can present two contracts ndash one for the users of the class and one for the extenders of the class
Make the fields private and accessor methods public if they are intended for the users of the class Make the fields or method protected if they are intended for extenders of the class
A class should also hide methods not intended for client use but only for internal use within the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu40
Using the static Modifier
A property that is shared by all the instances of the class should be declared as a static property
A method that is not dependent on a specific instance should
be declared as a static method
LiangIntroduction to Java Programmingrevised by Dai-kaiyu41
Using Inheritance or Composition cont
public class Cylinder private Circle circle Constructors Methods
If polymorphism is desirable you need to use the inheritance design If you donrsquot care about polymorphism the aggregation design gives more flexibility because the classes are less
dependent using aggregation than using inheritance
public class Cylinder extends Circle Constructors Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu42
Using Interfaces or Abstract Classes
Both interfaces and abstract classes can be used to generalize common features
a strong is-an-extension-of relationship that clearly describes a parent-child relationship should be modeled using classes
since an orange is a fruit their relationship should be modeled using class inheritance
A weak is-an-extension-of relationship can be modeled using interfaces
all strings are comparable so the String class implements the Comparable interface
LiangIntroduction to Java Programmingrevised by Dai-kaiyu43
Using Interfaces or Abstract Classes cont
Interfaces are more flexible than abstract classes because a subclass can extend only one superclass but
implement any number of interfaces However interfaces cannot contain concrete methods
You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface So you can use the interface or its companion class whichever is more convenient
LiangIntroduction to Java Programmingrevised by Dai-kaiyu44
Supplement P Designing Generic Matrix 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu31
Class Design
1 Identify classes for the system
2 Describe attributes and methods in each class
3 Establish relationships among classes
4 Create classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu32
Example 101 Borrowing Loans
NameName BorrowerBorrowerPersonPersonLoanLoan AddressAddress
Loan
Borrower -loan Loan +Borrower() +Borrower(name Name address Address) +getLoan() Loan +setLoan(loan Loan) 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 67
Person -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 -firstName String -mi char -lastName 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu33
Example 101 Borrowing Loans cont
The following is a test program that uses the classes Name Person Address Borrower and Loan
BorrowLoanBorrowLoan RunRun
LiangIntroduction to Java Programmingrevised by Dai-kaiyu34
Example 102 The Rational Class
RationalRational RunRunTestRationalClassTestRationalClass
1
1 Add Subtract Multiply Divide
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
javalangNumber +byteValue() byte +shortValue() short +intValue() int +longVlaue() long +floatValue() float +doubleValue()double
javalangComparable compareTo(Object) int
LiangIntroduction to Java Programmingrevised by Dai-kaiyu35
Class Design Guidelines
Designing a Single Class
Using Modifiers public protected private and static
Using Inheritance or Aggregation
Using Interfaces or Abstract Classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu36
Designing a Class
A class should describe a single entity or a set of similar operations
A single entity with too many responsibilities can be broken into several classes to separate responsibilities
The String class StringBuffer class and StringTokenizer class all deal with strings for example but have different responsibilities
SystemTextStringBuilder in jdk50
LiangIntroduction to Java Programmingrevised by Dai-kaiyu37
Designing a Class cont
Classes are usually designed for use by many different customers To make a class useful in a wide range of applications the class should provide a variety of ways for customization through properties and methods
Provide a public no-arg constructor and override the equals method and the toString method defined in the Object class whenever possible
LiangIntroduction to Java Programmingrevised by Dai-kaiyu38
Designing a Class cont
Follow standard Java programming style and naming conventions
Choose informative names for classes data fields and methods
Always place the data declaration before the constructor and place constructors before methods
Always provide a constructor and initialize variables to avoid programming errors
LiangIntroduction to Java Programmingrevised by Dai-kaiyu39
Using Visibility Modifiers
Each class can present two contracts ndash one for the users of the class and one for the extenders of the class
Make the fields private and accessor methods public if they are intended for the users of the class Make the fields or method protected if they are intended for extenders of the class
A class should also hide methods not intended for client use but only for internal use within the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu40
Using the static Modifier
A property that is shared by all the instances of the class should be declared as a static property
A method that is not dependent on a specific instance should
be declared as a static method
LiangIntroduction to Java Programmingrevised by Dai-kaiyu41
Using Inheritance or Composition cont
public class Cylinder private Circle circle Constructors Methods
If polymorphism is desirable you need to use the inheritance design If you donrsquot care about polymorphism the aggregation design gives more flexibility because the classes are less
dependent using aggregation than using inheritance
public class Cylinder extends Circle Constructors Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu42
Using Interfaces or Abstract Classes
Both interfaces and abstract classes can be used to generalize common features
a strong is-an-extension-of relationship that clearly describes a parent-child relationship should be modeled using classes
since an orange is a fruit their relationship should be modeled using class inheritance
A weak is-an-extension-of relationship can be modeled using interfaces
all strings are comparable so the String class implements the Comparable interface
LiangIntroduction to Java Programmingrevised by Dai-kaiyu43
Using Interfaces or Abstract Classes cont
Interfaces are more flexible than abstract classes because a subclass can extend only one superclass but
implement any number of interfaces However interfaces cannot contain concrete methods
You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface So you can use the interface or its companion class whichever is more convenient
LiangIntroduction to Java Programmingrevised by Dai-kaiyu44
Supplement P Designing Generic Matrix 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu32
Example 101 Borrowing Loans
NameName BorrowerBorrowerPersonPersonLoanLoan AddressAddress
Loan
Borrower -loan Loan +Borrower() +Borrower(name Name address Address) +getLoan() Loan +setLoan(loan Loan) 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 67
Person -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 -firstName String -mi char -lastName 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu33
Example 101 Borrowing Loans cont
The following is a test program that uses the classes Name Person Address Borrower and Loan
BorrowLoanBorrowLoan RunRun
LiangIntroduction to Java Programmingrevised by Dai-kaiyu34
Example 102 The Rational Class
RationalRational RunRunTestRationalClassTestRationalClass
1
1 Add Subtract Multiply Divide
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
javalangNumber +byteValue() byte +shortValue() short +intValue() int +longVlaue() long +floatValue() float +doubleValue()double
javalangComparable compareTo(Object) int
LiangIntroduction to Java Programmingrevised by Dai-kaiyu35
Class Design Guidelines
Designing a Single Class
Using Modifiers public protected private and static
Using Inheritance or Aggregation
Using Interfaces or Abstract Classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu36
Designing a Class
A class should describe a single entity or a set of similar operations
A single entity with too many responsibilities can be broken into several classes to separate responsibilities
The String class StringBuffer class and StringTokenizer class all deal with strings for example but have different responsibilities
SystemTextStringBuilder in jdk50
LiangIntroduction to Java Programmingrevised by Dai-kaiyu37
Designing a Class cont
Classes are usually designed for use by many different customers To make a class useful in a wide range of applications the class should provide a variety of ways for customization through properties and methods
Provide a public no-arg constructor and override the equals method and the toString method defined in the Object class whenever possible
LiangIntroduction to Java Programmingrevised by Dai-kaiyu38
Designing a Class cont
Follow standard Java programming style and naming conventions
Choose informative names for classes data fields and methods
Always place the data declaration before the constructor and place constructors before methods
Always provide a constructor and initialize variables to avoid programming errors
LiangIntroduction to Java Programmingrevised by Dai-kaiyu39
Using Visibility Modifiers
Each class can present two contracts ndash one for the users of the class and one for the extenders of the class
Make the fields private and accessor methods public if they are intended for the users of the class Make the fields or method protected if they are intended for extenders of the class
A class should also hide methods not intended for client use but only for internal use within the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu40
Using the static Modifier
A property that is shared by all the instances of the class should be declared as a static property
A method that is not dependent on a specific instance should
be declared as a static method
LiangIntroduction to Java Programmingrevised by Dai-kaiyu41
Using Inheritance or Composition cont
public class Cylinder private Circle circle Constructors Methods
If polymorphism is desirable you need to use the inheritance design If you donrsquot care about polymorphism the aggregation design gives more flexibility because the classes are less
dependent using aggregation than using inheritance
public class Cylinder extends Circle Constructors Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu42
Using Interfaces or Abstract Classes
Both interfaces and abstract classes can be used to generalize common features
a strong is-an-extension-of relationship that clearly describes a parent-child relationship should be modeled using classes
since an orange is a fruit their relationship should be modeled using class inheritance
A weak is-an-extension-of relationship can be modeled using interfaces
all strings are comparable so the String class implements the Comparable interface
LiangIntroduction to Java Programmingrevised by Dai-kaiyu43
Using Interfaces or Abstract Classes cont
Interfaces are more flexible than abstract classes because a subclass can extend only one superclass but
implement any number of interfaces However interfaces cannot contain concrete methods
You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface So you can use the interface or its companion class whichever is more convenient
LiangIntroduction to Java Programmingrevised by Dai-kaiyu44
Supplement P Designing Generic Matrix 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu33
Example 101 Borrowing Loans cont
The following is a test program that uses the classes Name Person Address Borrower and Loan
BorrowLoanBorrowLoan RunRun
LiangIntroduction to Java Programmingrevised by Dai-kaiyu34
Example 102 The Rational Class
RationalRational RunRunTestRationalClassTestRationalClass
1
1 Add Subtract Multiply Divide
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
javalangNumber +byteValue() byte +shortValue() short +intValue() int +longVlaue() long +floatValue() float +doubleValue()double
javalangComparable compareTo(Object) int
LiangIntroduction to Java Programmingrevised by Dai-kaiyu35
Class Design Guidelines
Designing a Single Class
Using Modifiers public protected private and static
Using Inheritance or Aggregation
Using Interfaces or Abstract Classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu36
Designing a Class
A class should describe a single entity or a set of similar operations
A single entity with too many responsibilities can be broken into several classes to separate responsibilities
The String class StringBuffer class and StringTokenizer class all deal with strings for example but have different responsibilities
SystemTextStringBuilder in jdk50
LiangIntroduction to Java Programmingrevised by Dai-kaiyu37
Designing a Class cont
Classes are usually designed for use by many different customers To make a class useful in a wide range of applications the class should provide a variety of ways for customization through properties and methods
Provide a public no-arg constructor and override the equals method and the toString method defined in the Object class whenever possible
LiangIntroduction to Java Programmingrevised by Dai-kaiyu38
Designing a Class cont
Follow standard Java programming style and naming conventions
Choose informative names for classes data fields and methods
Always place the data declaration before the constructor and place constructors before methods
Always provide a constructor and initialize variables to avoid programming errors
LiangIntroduction to Java Programmingrevised by Dai-kaiyu39
Using Visibility Modifiers
Each class can present two contracts ndash one for the users of the class and one for the extenders of the class
Make the fields private and accessor methods public if they are intended for the users of the class Make the fields or method protected if they are intended for extenders of the class
A class should also hide methods not intended for client use but only for internal use within the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu40
Using the static Modifier
A property that is shared by all the instances of the class should be declared as a static property
A method that is not dependent on a specific instance should
be declared as a static method
LiangIntroduction to Java Programmingrevised by Dai-kaiyu41
Using Inheritance or Composition cont
public class Cylinder private Circle circle Constructors Methods
If polymorphism is desirable you need to use the inheritance design If you donrsquot care about polymorphism the aggregation design gives more flexibility because the classes are less
dependent using aggregation than using inheritance
public class Cylinder extends Circle Constructors Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu42
Using Interfaces or Abstract Classes
Both interfaces and abstract classes can be used to generalize common features
a strong is-an-extension-of relationship that clearly describes a parent-child relationship should be modeled using classes
since an orange is a fruit their relationship should be modeled using class inheritance
A weak is-an-extension-of relationship can be modeled using interfaces
all strings are comparable so the String class implements the Comparable interface
LiangIntroduction to Java Programmingrevised by Dai-kaiyu43
Using Interfaces or Abstract Classes cont
Interfaces are more flexible than abstract classes because a subclass can extend only one superclass but
implement any number of interfaces However interfaces cannot contain concrete methods
You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface So you can use the interface or its companion class whichever is more convenient
LiangIntroduction to Java Programmingrevised by Dai-kaiyu44
Supplement P Designing Generic Matrix 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu34
Example 102 The Rational Class
RationalRational RunRunTestRationalClassTestRationalClass
1
1 Add Subtract Multiply Divide
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
javalangNumber +byteValue() byte +shortValue() short +intValue() int +longVlaue() long +floatValue() float +doubleValue()double
javalangComparable compareTo(Object) int
LiangIntroduction to Java Programmingrevised by Dai-kaiyu35
Class Design Guidelines
Designing a Single Class
Using Modifiers public protected private and static
Using Inheritance or Aggregation
Using Interfaces or Abstract Classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu36
Designing a Class
A class should describe a single entity or a set of similar operations
A single entity with too many responsibilities can be broken into several classes to separate responsibilities
The String class StringBuffer class and StringTokenizer class all deal with strings for example but have different responsibilities
SystemTextStringBuilder in jdk50
LiangIntroduction to Java Programmingrevised by Dai-kaiyu37
Designing a Class cont
Classes are usually designed for use by many different customers To make a class useful in a wide range of applications the class should provide a variety of ways for customization through properties and methods
Provide a public no-arg constructor and override the equals method and the toString method defined in the Object class whenever possible
LiangIntroduction to Java Programmingrevised by Dai-kaiyu38
Designing a Class cont
Follow standard Java programming style and naming conventions
Choose informative names for classes data fields and methods
Always place the data declaration before the constructor and place constructors before methods
Always provide a constructor and initialize variables to avoid programming errors
LiangIntroduction to Java Programmingrevised by Dai-kaiyu39
Using Visibility Modifiers
Each class can present two contracts ndash one for the users of the class and one for the extenders of the class
Make the fields private and accessor methods public if they are intended for the users of the class Make the fields or method protected if they are intended for extenders of the class
A class should also hide methods not intended for client use but only for internal use within the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu40
Using the static Modifier
A property that is shared by all the instances of the class should be declared as a static property
A method that is not dependent on a specific instance should
be declared as a static method
LiangIntroduction to Java Programmingrevised by Dai-kaiyu41
Using Inheritance or Composition cont
public class Cylinder private Circle circle Constructors Methods
If polymorphism is desirable you need to use the inheritance design If you donrsquot care about polymorphism the aggregation design gives more flexibility because the classes are less
dependent using aggregation than using inheritance
public class Cylinder extends Circle Constructors Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu42
Using Interfaces or Abstract Classes
Both interfaces and abstract classes can be used to generalize common features
a strong is-an-extension-of relationship that clearly describes a parent-child relationship should be modeled using classes
since an orange is a fruit their relationship should be modeled using class inheritance
A weak is-an-extension-of relationship can be modeled using interfaces
all strings are comparable so the String class implements the Comparable interface
LiangIntroduction to Java Programmingrevised by Dai-kaiyu43
Using Interfaces or Abstract Classes cont
Interfaces are more flexible than abstract classes because a subclass can extend only one superclass but
implement any number of interfaces However interfaces cannot contain concrete methods
You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface So you can use the interface or its companion class whichever is more convenient
LiangIntroduction to Java Programmingrevised by Dai-kaiyu44
Supplement P Designing Generic Matrix 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu35
Class Design Guidelines
Designing a Single Class
Using Modifiers public protected private and static
Using Inheritance or Aggregation
Using Interfaces or Abstract Classes
LiangIntroduction to Java Programmingrevised by Dai-kaiyu36
Designing a Class
A class should describe a single entity or a set of similar operations
A single entity with too many responsibilities can be broken into several classes to separate responsibilities
The String class StringBuffer class and StringTokenizer class all deal with strings for example but have different responsibilities
SystemTextStringBuilder in jdk50
LiangIntroduction to Java Programmingrevised by Dai-kaiyu37
Designing a Class cont
Classes are usually designed for use by many different customers To make a class useful in a wide range of applications the class should provide a variety of ways for customization through properties and methods
Provide a public no-arg constructor and override the equals method and the toString method defined in the Object class whenever possible
LiangIntroduction to Java Programmingrevised by Dai-kaiyu38
Designing a Class cont
Follow standard Java programming style and naming conventions
Choose informative names for classes data fields and methods
Always place the data declaration before the constructor and place constructors before methods
Always provide a constructor and initialize variables to avoid programming errors
LiangIntroduction to Java Programmingrevised by Dai-kaiyu39
Using Visibility Modifiers
Each class can present two contracts ndash one for the users of the class and one for the extenders of the class
Make the fields private and accessor methods public if they are intended for the users of the class Make the fields or method protected if they are intended for extenders of the class
A class should also hide methods not intended for client use but only for internal use within the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu40
Using the static Modifier
A property that is shared by all the instances of the class should be declared as a static property
A method that is not dependent on a specific instance should
be declared as a static method
LiangIntroduction to Java Programmingrevised by Dai-kaiyu41
Using Inheritance or Composition cont
public class Cylinder private Circle circle Constructors Methods
If polymorphism is desirable you need to use the inheritance design If you donrsquot care about polymorphism the aggregation design gives more flexibility because the classes are less
dependent using aggregation than using inheritance
public class Cylinder extends Circle Constructors Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu42
Using Interfaces or Abstract Classes
Both interfaces and abstract classes can be used to generalize common features
a strong is-an-extension-of relationship that clearly describes a parent-child relationship should be modeled using classes
since an orange is a fruit their relationship should be modeled using class inheritance
A weak is-an-extension-of relationship can be modeled using interfaces
all strings are comparable so the String class implements the Comparable interface
LiangIntroduction to Java Programmingrevised by Dai-kaiyu43
Using Interfaces or Abstract Classes cont
Interfaces are more flexible than abstract classes because a subclass can extend only one superclass but
implement any number of interfaces However interfaces cannot contain concrete methods
You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface So you can use the interface or its companion class whichever is more convenient
LiangIntroduction to Java Programmingrevised by Dai-kaiyu44
Supplement P Designing Generic Matrix 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu36
Designing a Class
A class should describe a single entity or a set of similar operations
A single entity with too many responsibilities can be broken into several classes to separate responsibilities
The String class StringBuffer class and StringTokenizer class all deal with strings for example but have different responsibilities
SystemTextStringBuilder in jdk50
LiangIntroduction to Java Programmingrevised by Dai-kaiyu37
Designing a Class cont
Classes are usually designed for use by many different customers To make a class useful in a wide range of applications the class should provide a variety of ways for customization through properties and methods
Provide a public no-arg constructor and override the equals method and the toString method defined in the Object class whenever possible
LiangIntroduction to Java Programmingrevised by Dai-kaiyu38
Designing a Class cont
Follow standard Java programming style and naming conventions
Choose informative names for classes data fields and methods
Always place the data declaration before the constructor and place constructors before methods
Always provide a constructor and initialize variables to avoid programming errors
LiangIntroduction to Java Programmingrevised by Dai-kaiyu39
Using Visibility Modifiers
Each class can present two contracts ndash one for the users of the class and one for the extenders of the class
Make the fields private and accessor methods public if they are intended for the users of the class Make the fields or method protected if they are intended for extenders of the class
A class should also hide methods not intended for client use but only for internal use within the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu40
Using the static Modifier
A property that is shared by all the instances of the class should be declared as a static property
A method that is not dependent on a specific instance should
be declared as a static method
LiangIntroduction to Java Programmingrevised by Dai-kaiyu41
Using Inheritance or Composition cont
public class Cylinder private Circle circle Constructors Methods
If polymorphism is desirable you need to use the inheritance design If you donrsquot care about polymorphism the aggregation design gives more flexibility because the classes are less
dependent using aggregation than using inheritance
public class Cylinder extends Circle Constructors Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu42
Using Interfaces or Abstract Classes
Both interfaces and abstract classes can be used to generalize common features
a strong is-an-extension-of relationship that clearly describes a parent-child relationship should be modeled using classes
since an orange is a fruit their relationship should be modeled using class inheritance
A weak is-an-extension-of relationship can be modeled using interfaces
all strings are comparable so the String class implements the Comparable interface
LiangIntroduction to Java Programmingrevised by Dai-kaiyu43
Using Interfaces or Abstract Classes cont
Interfaces are more flexible than abstract classes because a subclass can extend only one superclass but
implement any number of interfaces However interfaces cannot contain concrete methods
You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface So you can use the interface or its companion class whichever is more convenient
LiangIntroduction to Java Programmingrevised by Dai-kaiyu44
Supplement P Designing Generic Matrix 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu37
Designing a Class cont
Classes are usually designed for use by many different customers To make a class useful in a wide range of applications the class should provide a variety of ways for customization through properties and methods
Provide a public no-arg constructor and override the equals method and the toString method defined in the Object class whenever possible
LiangIntroduction to Java Programmingrevised by Dai-kaiyu38
Designing a Class cont
Follow standard Java programming style and naming conventions
Choose informative names for classes data fields and methods
Always place the data declaration before the constructor and place constructors before methods
Always provide a constructor and initialize variables to avoid programming errors
LiangIntroduction to Java Programmingrevised by Dai-kaiyu39
Using Visibility Modifiers
Each class can present two contracts ndash one for the users of the class and one for the extenders of the class
Make the fields private and accessor methods public if they are intended for the users of the class Make the fields or method protected if they are intended for extenders of the class
A class should also hide methods not intended for client use but only for internal use within the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu40
Using the static Modifier
A property that is shared by all the instances of the class should be declared as a static property
A method that is not dependent on a specific instance should
be declared as a static method
LiangIntroduction to Java Programmingrevised by Dai-kaiyu41
Using Inheritance or Composition cont
public class Cylinder private Circle circle Constructors Methods
If polymorphism is desirable you need to use the inheritance design If you donrsquot care about polymorphism the aggregation design gives more flexibility because the classes are less
dependent using aggregation than using inheritance
public class Cylinder extends Circle Constructors Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu42
Using Interfaces or Abstract Classes
Both interfaces and abstract classes can be used to generalize common features
a strong is-an-extension-of relationship that clearly describes a parent-child relationship should be modeled using classes
since an orange is a fruit their relationship should be modeled using class inheritance
A weak is-an-extension-of relationship can be modeled using interfaces
all strings are comparable so the String class implements the Comparable interface
LiangIntroduction to Java Programmingrevised by Dai-kaiyu43
Using Interfaces or Abstract Classes cont
Interfaces are more flexible than abstract classes because a subclass can extend only one superclass but
implement any number of interfaces However interfaces cannot contain concrete methods
You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface So you can use the interface or its companion class whichever is more convenient
LiangIntroduction to Java Programmingrevised by Dai-kaiyu44
Supplement P Designing Generic Matrix 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu38
Designing a Class cont
Follow standard Java programming style and naming conventions
Choose informative names for classes data fields and methods
Always place the data declaration before the constructor and place constructors before methods
Always provide a constructor and initialize variables to avoid programming errors
LiangIntroduction to Java Programmingrevised by Dai-kaiyu39
Using Visibility Modifiers
Each class can present two contracts ndash one for the users of the class and one for the extenders of the class
Make the fields private and accessor methods public if they are intended for the users of the class Make the fields or method protected if they are intended for extenders of the class
A class should also hide methods not intended for client use but only for internal use within the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu40
Using the static Modifier
A property that is shared by all the instances of the class should be declared as a static property
A method that is not dependent on a specific instance should
be declared as a static method
LiangIntroduction to Java Programmingrevised by Dai-kaiyu41
Using Inheritance or Composition cont
public class Cylinder private Circle circle Constructors Methods
If polymorphism is desirable you need to use the inheritance design If you donrsquot care about polymorphism the aggregation design gives more flexibility because the classes are less
dependent using aggregation than using inheritance
public class Cylinder extends Circle Constructors Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu42
Using Interfaces or Abstract Classes
Both interfaces and abstract classes can be used to generalize common features
a strong is-an-extension-of relationship that clearly describes a parent-child relationship should be modeled using classes
since an orange is a fruit their relationship should be modeled using class inheritance
A weak is-an-extension-of relationship can be modeled using interfaces
all strings are comparable so the String class implements the Comparable interface
LiangIntroduction to Java Programmingrevised by Dai-kaiyu43
Using Interfaces or Abstract Classes cont
Interfaces are more flexible than abstract classes because a subclass can extend only one superclass but
implement any number of interfaces However interfaces cannot contain concrete methods
You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface So you can use the interface or its companion class whichever is more convenient
LiangIntroduction to Java Programmingrevised by Dai-kaiyu44
Supplement P Designing Generic Matrix 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu39
Using Visibility Modifiers
Each class can present two contracts ndash one for the users of the class and one for the extenders of the class
Make the fields private and accessor methods public if they are intended for the users of the class Make the fields or method protected if they are intended for extenders of the class
A class should also hide methods not intended for client use but only for internal use within the class
LiangIntroduction to Java Programmingrevised by Dai-kaiyu40
Using the static Modifier
A property that is shared by all the instances of the class should be declared as a static property
A method that is not dependent on a specific instance should
be declared as a static method
LiangIntroduction to Java Programmingrevised by Dai-kaiyu41
Using Inheritance or Composition cont
public class Cylinder private Circle circle Constructors Methods
If polymorphism is desirable you need to use the inheritance design If you donrsquot care about polymorphism the aggregation design gives more flexibility because the classes are less
dependent using aggregation than using inheritance
public class Cylinder extends Circle Constructors Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu42
Using Interfaces or Abstract Classes
Both interfaces and abstract classes can be used to generalize common features
a strong is-an-extension-of relationship that clearly describes a parent-child relationship should be modeled using classes
since an orange is a fruit their relationship should be modeled using class inheritance
A weak is-an-extension-of relationship can be modeled using interfaces
all strings are comparable so the String class implements the Comparable interface
LiangIntroduction to Java Programmingrevised by Dai-kaiyu43
Using Interfaces or Abstract Classes cont
Interfaces are more flexible than abstract classes because a subclass can extend only one superclass but
implement any number of interfaces However interfaces cannot contain concrete methods
You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface So you can use the interface or its companion class whichever is more convenient
LiangIntroduction to Java Programmingrevised by Dai-kaiyu44
Supplement P Designing Generic Matrix 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu40
Using the static Modifier
A property that is shared by all the instances of the class should be declared as a static property
A method that is not dependent on a specific instance should
be declared as a static method
LiangIntroduction to Java Programmingrevised by Dai-kaiyu41
Using Inheritance or Composition cont
public class Cylinder private Circle circle Constructors Methods
If polymorphism is desirable you need to use the inheritance design If you donrsquot care about polymorphism the aggregation design gives more flexibility because the classes are less
dependent using aggregation than using inheritance
public class Cylinder extends Circle Constructors Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu42
Using Interfaces or Abstract Classes
Both interfaces and abstract classes can be used to generalize common features
a strong is-an-extension-of relationship that clearly describes a parent-child relationship should be modeled using classes
since an orange is a fruit their relationship should be modeled using class inheritance
A weak is-an-extension-of relationship can be modeled using interfaces
all strings are comparable so the String class implements the Comparable interface
LiangIntroduction to Java Programmingrevised by Dai-kaiyu43
Using Interfaces or Abstract Classes cont
Interfaces are more flexible than abstract classes because a subclass can extend only one superclass but
implement any number of interfaces However interfaces cannot contain concrete methods
You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface So you can use the interface or its companion class whichever is more convenient
LiangIntroduction to Java Programmingrevised by Dai-kaiyu44
Supplement P Designing Generic Matrix 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu41
Using Inheritance or Composition cont
public class Cylinder private Circle circle Constructors Methods
If polymorphism is desirable you need to use the inheritance design If you donrsquot care about polymorphism the aggregation design gives more flexibility because the classes are less
dependent using aggregation than using inheritance
public class Cylinder extends Circle Constructors Methods
LiangIntroduction to Java Programmingrevised by Dai-kaiyu42
Using Interfaces or Abstract Classes
Both interfaces and abstract classes can be used to generalize common features
a strong is-an-extension-of relationship that clearly describes a parent-child relationship should be modeled using classes
since an orange is a fruit their relationship should be modeled using class inheritance
A weak is-an-extension-of relationship can be modeled using interfaces
all strings are comparable so the String class implements the Comparable interface
LiangIntroduction to Java Programmingrevised by Dai-kaiyu43
Using Interfaces or Abstract Classes cont
Interfaces are more flexible than abstract classes because a subclass can extend only one superclass but
implement any number of interfaces However interfaces cannot contain concrete methods
You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface So you can use the interface or its companion class whichever is more convenient
LiangIntroduction to Java Programmingrevised by Dai-kaiyu44
Supplement P Designing Generic Matrix 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu42
Using Interfaces or Abstract Classes
Both interfaces and abstract classes can be used to generalize common features
a strong is-an-extension-of relationship that clearly describes a parent-child relationship should be modeled using classes
since an orange is a fruit their relationship should be modeled using class inheritance
A weak is-an-extension-of relationship can be modeled using interfaces
all strings are comparable so the String class implements the Comparable interface
LiangIntroduction to Java Programmingrevised by Dai-kaiyu43
Using Interfaces or Abstract Classes cont
Interfaces are more flexible than abstract classes because a subclass can extend only one superclass but
implement any number of interfaces However interfaces cannot contain concrete methods
You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface So you can use the interface or its companion class whichever is more convenient
LiangIntroduction to Java Programmingrevised by Dai-kaiyu44
Supplement P Designing Generic Matrix 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu43
Using Interfaces or Abstract Classes cont
Interfaces are more flexible than abstract classes because a subclass can extend only one superclass but
implement any number of interfaces However interfaces cannot contain concrete methods
You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface So you can use the interface or its companion class whichever is more convenient
LiangIntroduction to Java Programmingrevised by Dai-kaiyu44
Supplement P Designing Generic Matrix 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu44
Supplement P Designing Generic Matrix 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu45
Example 103 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
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu46
Example 103 cont
Objective This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic
TestIntegerMatrixTestIntegerMatrix RunRun
TestRationalMatrixTestRationalMatrix RunRunRationalMatrixRationalMatrix
IntegerMatrixIntegerMatrix
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu47
Framework-Based Programming
The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java
You have to use these classes and interfaces and follow their conventions and rules to create applications This is referred to as framework-based programming
Once you understand the concept of Java and object-orient programming the most important lesson from now on is learning how to use the API to develop useful programs
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16
LiangIntroduction to Java Programmingrevised by Dai-kaiyu48
Framework-Based Programming cont
the framework-based programming using the Java API in our course
Java GUI programming framework in Chapters 11 12 13 and 14
the Java exception handling framework in Chapter 15
the Java IO framework in Chapter 16