You are on page 1of 63

SEH2A3

Object Oriented Programming A

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

byte[] arrBytes; // array of byte

byte[][] arrOfArrBytes; // array of byte[]

Point[] points; // array of Point objects


Creating Array
Use the new keyword, just as you do to create an
object, however arrays doesn'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;
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};
Garbage collectible heap
Array of Object
Dog object Dog object

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!

float[][][] temp = new float[][180][100];


– // Error!
Asymmetric Array
Array in Java can be declared asymmetric
int[][] a = new int[4][];
a[0] = new int[3];
0 0 1 2
a[1] = new int[1]; 1
0

a[3] = new int[5]; 2

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 An attribute is a named property


of a class that describes the
name : String object being modeled.
address : String
birthdate : Date In the class diagram, attributes
Id : integer appear in the second
compartment just below the
operations name-compartment.
Attributes are usually listed in the
form:
attributeName : Type
Class Attributes

Person Attributes can be:


+ (public)
+ name : String
# address : String # (protected)
- birthdate : Date - (private)
age : integer
- id : integer

Normally use private for


operations field
Class Attributes

Person A derived attribute is one that can


be computed from other
- name : String attributes, but doesn‟t actually
- address : String
- birthdate : Date
exist. For example, a Person‟s age
/ age : integer can be computed from his birth
- id : integer date.
A derived attribute is designated
operations
by a preceding „/‟ as in:
/ age : integer
Class Operations

Person Operations describe the


- name : String
class behavior and appear
- address : String in the third compartment
- birthdate : Date
/ age : integer You can specify an
- id : integer operation by stating its
eat(food : String) signature:
sleep(hour : int)
work() : String – listing the name, type, and default
play() value of all parameters, and, in the
case of functions, a return type.
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 eat work()
play play()
SEH2A3
Object Oriented Programming A

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

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.
Association Example

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;

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;
}
}
Example
public class Instructor{
private String name;

public Instructor(String name){ Association


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

learns from teaches


Student Instructor
1..* 1..*
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..*
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; }
} }

public void setHorsePower(int hp) {


this.horsePower = hp;
}
}
Example – Whole Class
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;
}
}
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
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

- String type - String title

+ Scrollbar (String type) + Titlebar (String title)


Example – Constituent Class
public class Scrollbar { public class Titlebar {
public String type; public String title;

public Scrollbar(String type){ public Titlebar(String title){


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

public class Menu {


private String title;
private String type;

public Menu(String title, String type){


this.title = title;
this.type = type;
}
}
Example – Whole Class
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);
}
}
Question?
Excersice
Specify the appropriate relation to each case:
1. A person and a car that he wants to buy
2. A car in a parking lot
3. A mall and its basement
4. Wheels in a car
5. A department and a company
6. A department and an employee
7. A canteen and a department
8. A pond and fishes
Assignment - String name
Reviewer

1..*
+ Reviewer (String name)
+ Setter
+ Getter
+ giveReview (Book b, String review)

Book 1..* Author


- String title
- int year = 0 - String name
- Author[3] author + Author (String name)
- Chapter[5] chapter + Setter
- String[5] review + Getter
- int numAuthor = 0
- int numChapter = 0
- int numReview = 0
+ Book (String title, int year)
+ addAuthor (Author author)
+ createChapter (String chapterTitle, int page)
+ Setter 1..*
+ Getter Chapter
+ getAuthor (int x): Author
+ getChapter (int x): Chapter - String chapterTitle
+ String toString() - int page = 0
+ Chapter (String chapterTitle, int page)
+ Setter
+ Getter
Encapsulation
• Reviewer
– Name tidak boleh kosong, jika kosong beri nama default
“anonymous”
• Book
– Title tidak boleh kosong, jika kosong beri default “no title”
– Year harus > 0
• Author
– Name tidak boleh kosong, jika kosong beri nama default
“anonymous”
• Chapter
– chapterTitle tidak boleh kosong, jika kosong beri default
“unknown”
– Page harus > 0
Buat driver-nya dengan skenario:
1. Buat sebuah objek Book b 4. Buat sebuah objek Reviewer r
dengan judul "OOP", tahun dengan nama "Brown"
2016 5. Reviewer r memberi review
2. Buat dua objek Author dengan terhadap Book b sebanyak dua
nama "John While" dan "David kali
Do". Tambahkan dua author tsb 6. Tampilkan semua data Book b
ke Book b.
3. Buat enam chapter dengan
judul dan halaman sebagai
berikut:
– Introduction, 1
– Class and Object, 20
– Encapsulation, 30
– Class Relationship, 40
– Inheritance, 50
– Interface, 60
Submission
• Compress all .java files into
Tugas2PBO_NIM_Kelas.rar/ .zip
• Submit compressed file to IDEA
THANK YOU

You might also like