0% found this document useful (0 votes)
37 views

Class Relationship: Object Oriented Programming

The document discusses arrays in Java. It defines an array as an ordered collection of values of the same type. It describes how to declare and create arrays, including multidimensional arrays. It also discusses array literals and compatibility with C/C++ syntax. UML class diagrams are presented as a way to visually represent classes and relationships in object-oriented systems.

Uploaded by

Muhitha Adha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Class Relationship: Object Oriented Programming

The document discusses arrays in Java. It defines an array as an ordered collection of values of the same type. It describes how to declare and create arrays, including multidimensional arrays. It also discusses array literals and compatibility with C/C++ syntax. UML class diagrams are presented as a way to visually represent classes and relationships in object-oriented systems.

Uploaded by

Muhitha Adha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

9/30/2019

Object Oriented Programming

CLASS RELATIONSHIP

Object Oriented Programming

Array in Java

1
9/30/2019

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; // byte is a primitive type
• byte[] arrayOfBytes; // byte[] is an array type: array of byte
• byte[][] arrayOfArrayOfBytes; // byte[][] is another type: array of byte[]
• Point[] points; // Point[] is an array of Point objects

2
9/30/2019

Array
Dog[] pets;
pets = new Dog[7];
pets[0] = new Dog()
pets[1] = new Dog()

Compatibility with C and C++


• byte arrayOfBytes[]; // Same as byte[] arrayOfBytes
• byte arrayOfArrayOfBytes[][]; // Same as byte[][] arrayOfArrayOfBytes
• byte[] arrayOfArrayOfBytes[]; // Same as byte[][] arrayOfArrayOfBytes

• This is almost always a confusing syntax, however, and it is not


recommended.

3
9/30/2019

Creating Array
• Use the new keyword, just as you do to create an object,
however arrays don't need to be initialized like objects do
• Must specify the length, once created the array size cannot be
changed

• byte[] buffer = new byte[1024];


• String[] lines = new String[50];

Array Literals
• The null literal used to represent the absence of an object can
also be used to represent the absence of an array.
– char[] password = null;

• combines the creation of the array object with the


initialization of the array elements:
– int[] powersOfTwo = {1, 2, 4, 8, 16, 32, 64, 128};

4
9/30/2019

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[][][] globalTemperatureData = new float[360][180][100];

5
9/30/2019

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 or dimensions.

• float[][][] globalTemperatureData = new float[360][][];


• float[][][] globalTemperatureData = 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[][][] globalTemperatureData = new float[360][][100];


// Error!
• float[][][] globalTemperatureData = new float[][180][100];
// Error!

6
9/30/2019

CSG2H3
Object Oriented Programming
Class Diagram

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.

7
9/30/2019

What is UML and Why we use UML?

 UML → “Unified Modeling Language”


 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

Classes
 A class is a description of a set of
ClassName  objects that share the same attributes,
 operations, relationships, and semantics.
attributes
 Graphically, a class is rendered as a
 rectangle, usually including its name,
operations  attributes, and operations in separate,
 designated compartments.

Class
Name Window
size: Size
Attributes visibility: boolean

display()
Operations hide()

8
9/30/2019

Class Names

 The name of the class is the only required


ClassName tag in the graphical representation of a
class. It always appears in the top-most
compartment.
attributes

operations

Class Attributes

Person

 An attribute is a named property of a


name : String
address : Address
 class that describes the object being modeled.
birthdate : Date  In the class diagram, attributes appear in
ssn : Id  the second compartment just below the
 name-compartment.

9
9/30/2019

Class Attributes
 Attributes are usually listed in the form:

Person  attributeName : Type

 A derived attribute is one that can be


name : String
address : Address  computed from other attributes, but
birthdate : Date  doesn’t actually exist. For example,
/ age : Date
 a Person’s age can be computed from
ssn : Id
 his birth date. A derived attribute is
 designated by a preceding ‘/’ as in:

 / age : Date

Class Attributes

Person

 Attributes can be:


+ name : String  + public
# address : String  # protected
- birthdate : Date
age : integer  - private
- idNumber : integer
 Normally use private for field

10
9/30/2019

Class Operations

Person

name : String
address : String
birthdate : Date
age : integer
idNumber : integer

 Operations describe the class behavior


eat
sleep
 and appear in the third compartment.
work
play

Class Operations

PhoneBook

newEntry (n : Name, a : Address, p : PhoneNumber, d : Description)


getPhone ( n : Name, a : Address) : PhoneNumber

 You can specify an operation by stating its signature: listing the name,
type, and default value of all parameters, and, in the case of
functions, a return type.

11
9/30/2019

Example
public class Person{
private String name;
private String address;
private Date birthdate;
private int idNumber;

public void eat(String food){


System.out.println("i eat "+food);
}

public void setName(String name){


this.name = name;
}

public String getName(){


return name;
}

Depicting Classes
 When drawing a class, you needn’t show attributes and operation in
every diagram.

Person Person
Person

name : String
birthdate : Date
Person ssn : Id

name Person eat()


address sleep()
birthdate work()
eat play()
play

12
9/30/2019

Object Oriented Programming

Class Relationship

Class Association

13
9/30/2019

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

 An association between two classes indicates that objects at one


end of an association “recognize” objects at the other end and may
send messages to them.

Example
public class Student{
private String name;
private String assignment;

public Student(String name){


this.name = name;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setAssignment(String assignment){
this.assignment = assignment;
}

public String getAssignment(){


return assignment;
}
}

14
9/30/2019

Example
public class Instructor{
private String name;

public Instructor(String name){


this.name = name;
}
public void giveAssignment(Student s, String assignment){
s.setAssignment(assignment);
}
}

public class Driver{


public static void main(String args[]){
Student s1 = new Student("andi");
Instructor i1 = new Instructor("budi");
i1.giveAssignment(s1,"code java OOP");
System.out.println(s1.getAssignment());
}
}

Association Relationships
 We can indicate the multiplicity of an association by adding
multiplicity adornments to the line denoting the association.

 The example indicates that a Student has one or more Instructors:

Student Instructor
1..*

15
9/30/2019

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 rolenames.

teaches learns from


Student Instructor
1..* 1..*

16
9/30/2019

Association Relationships
 We can also name the association.

membership
Student Team
1..* 1..*

Association Relationships
 We can specify dual associations.

member of

1..* 1..*
Student Team

1 president of 1..*

17
9/30/2019

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

18
9/30/2019

Association Relationships

 A class can have a self association.

next

LinkedListNode
previous

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 is a variant
of the "has a" or association relationship.
 Aggregations are denoted by a hollow-diamond adornment on
the association.

Engine
Car
Transmission

19
9/30/2019

Example
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; }
} }

public void setHorsePower(int hp) {


this.horsePower = hp;
}
}

Example
public class Car {
private String name;
private Engine engine;
private Transmission transmission;

public Car(String name){


this.name = name;
}

public void addEngine(Engine e){


engine = e;
}

public void addTransmission(Transmission t){


transmission = t;
}
}

20
9/30/2019

Example
public class Driver {

public static void main(String[] args) {


Car c = new Car("honda");

Engine v1000 = new Engine("v1000");

Transmisson auto = new Transmisson();


auto.setType("Automatic");

c.addEngine(v1000);
c.addTransmission(auto);

}
}

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).
Compositions are denoted by a filled-diamond adornment on the
association.

Scrollbar
1 1

Window Titlebar
1 1

Menu
1 1 .. *

21
9/30/2019

Example
public class Scrollbar {
public String type;

public Scrollbar(String type){


this.type = type;
}
}

public class Titlebar {


public String title;

public Titlebar(String title){


this.title = title;
}
}

Example
public class Menu {
private String title;
private String type;

public Menu(String title, String type){


this.title = title;
this.type = type;
}
}

22
9/30/2019

Example
public class Window {
private Scrollbar scBar;
private Titlebar tlBar;
private Menu[] menu;

public Window(String title, String scrollType, int numMenu){


scBar = new Scrollbar(scrollType);
tlBar = new Titlebar(title);
menu = new Menu[numMenu];
}
}

public class Driver {


public static void main(String[] args) {
Window w = new Window("OOP Window", "vertical", 5);
}
}

Interfaces

 An interface is a named set of


operations that specifies the behavior
of objects without showing their inner
<<interface>> structure. It can be rendered in the
ControlPanel model by a one- or two-compartment
rectangle, with the stereotype
<<interface>> above the interface
name.

23
9/30/2019

Example
Specify the appropriate relation to each case:
• A person and a car that he wants to buy
• A car in a parking lot
• A mall and its parking lot
• Wheels in a car
• A department and a company
• A department and an employee
• A canteen and a department
• A pond and fishes

Exercise

24

You might also like