Class Relationship
Class Relationship
Array
-RSM-
Array
the second kind of reference types in Java
an ordered collection, or numbered list, of values.
The values can be primitive values,
objects, or even other arrays,
but all of the values in an array
must be of the same type.
Declaring Array
byte b; // primitive type
Dog[] pets
pets = new Dog[7]
pets[0] = new Dog()
pets[5] = new Dog()
0 1 2 3 4 5 6
Dog Dog Dog Dog Dog Dog Dog
pets
Dog array object (Dog[])
Dog[]
Multidimensional Array
int[][] products;
Each of the pairs of square brackets represents one
dimension, so this is a two-dimensional array.
To access a single int element of this two-dimensional
array, you must specify two index values, one for each
dimension.
To create a new multidimensional array, use the new
keyword and specify the size of both dimensions of the
array.
int[][] products = new int[10][10];
Multidimensional Array
The new keyword performs this additional
initialization automatically for you.
It works with arrays with more than two
dimensions as well:
– float[][][] temp = new float[360][180][100];
Multidimensional Array
When using new with multidimensional arrays,
you do not have to specify a size for all
dimensions of the array, only the leftmost
dimension(s).
float[][][] temp = new float[360][][];
float[][][] temp = new float[360][180][];
Multidimensional Array
The first line creates a single-dimensional array, where each
element of the array can hold a float[][].
The second line creates a two-dimensional array, where
each element of the array is a float[].
If you specify a size for only some of the dimensions of an
array, however, those dimensions must be the leftmost
ones. The following lines are not legal:
float[][][] temp = new float[360][][100];
– // Error!
3
0 1 2 3 4
SEH2A3
Object Oriented Programming A
Class Diagram
-RSM-
Unified Modeling Language
Unified Modeling Language (UML) is a
standardized general-purpose modeling language
in the field of software engineering. The standard
is managed, and was created by, the Object
Management Group.
UML includes a set of graphic notation techniques
to create visual models of software-intensive
systems.
What is UML? And why we use UML?
Language: express idea, not a methodology
Modeling: Describing a software system at a high
level of abstraction
Unified: UML has become a world standard
– Object Management Group (OMG): www.omg.org
Class
ClassName
A class is a description of a set
of objects that share the same
attributes, operations,
attributes
relationships, and semantics.
operations
ClassName
Person
The name of the class is the
only required tag in the
graphical representation of a
attributes
class.
It always appears in the top-
operations most compartment.
Class Attributes
Person
Person Person
name : String
birthdate : Date
Person ssn : Id
Class Relationship
-RSM-
Class Association
Basic Relationship
Association
Aggregation
Composition
Association
Association Relationships
If two classes in a model need to communicate
with each other, there must be link between
them.
An association denotes that link.
Student Instructor
Student
Instructor
- String name
- String assignment - String name
+ Student (String name) + Instructor (String name)
+ Setter
+ Getter + giveAssignment (Student s, String assignment)
Example
public class Student{
private String name;
private String assignment;
Student Instructor
1..*
Association Relationships
The example indicates that every Instructor has
one or more Students:
Student Instructor
1..*
Association Relationships
We can also indicate the behavior of an object in
an association (i.e., the role of an object) using
role names.
membership
Student Team
1..* 1..*
Association Relationships
We can specify dual associations.
member of
1..* 1..*
Student Team
1 president of 1..*
Association Relationships
We can constrain the association relationship by
defining the navigability of the association.
– Here, a Router object requests services from a DNS object
by sending messages to (invoking the operations of) the
server.
– The direction of the association indicates that the server
has no knowledge of the Router.
Router DomainNameServer
Association Relationships
Associations can also be objects themselves,
called link classes or an association classes.
Registration
modelNumber
serialNumber
warrentyCode
Product Warranty
Association Relationships
A class can have a self association.
next
LinkedListNode
previous
Aggregation
Aggregation Relationships
We can model objects that contain other objects
by way of special associations called aggregations
and compositions.
An aggregation specifies a whole-part relationship
between an aggregate (a whole) and a constituent
part, where the part can exist independently from
the aggregate.
Aggregation Relationships
Aggregation is a variant of the "has a" or
association relationship.
Aggregations are denoted by a hollow-diamond
adornment on the association.
Engine
Car
Transmission
Aggregation Example
Car Engine
- String name - String name
- Engine engine - Int housePower
- Transmission transmission
+ Engine (String name)
+ Car (String name) + Setter
+ addEngine (Engine e) + Getter
+ addTransmission (Transmission t)
Transmission
- String type
+ Setter
+ Getter
Example – Constituent Class
public class Engine { public class Transmission {
private String name; private String type;
private int horsePower;
public String getType() {
public Engine(String name){ return type;
this.name = name; }
}
public void setType(String type) {
public int getHorsePower() { this.type = type;
return horsePower; }
} }
c.addEngine(v1000);
c.addTransmission(auto);
}
}
Composition
Composition Relationships
Composition is a stronger variant of the "owns a"
or association relationship
A composition indicates a strong ownership and
coincident lifetime of parts by the whole (i.e., they
live and die as a whole).
Composition Relationships
Compositions are denoted by a filled-diamond
adornment on the association.
Scrollbar
1 1
Window Titlebar
1 1
Menu
1 1 ..
*
Composition Example
Window
Menu
- Scrollbar scBar
- Titlebar tlBar - String title
- Menu[] menu - String type
+ Window (String scType, String ttlTitle, int numMenu) + Menu (String title, String type)
Scrollbar Titlebar
1..*
+ Reviewer (String name)
+ Setter
+ Getter
+ giveReview (Book b, String review)