0% found this document useful (0 votes)
3 views28 pages

Lec 04 Class Diagram

Uploaded by

right2abhinavrs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views28 pages

Lec 04 Class Diagram

Uploaded by

right2abhinavrs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

19CSE204 Object oriented Programming

19CSE204 Object Oriented Programming


Lecture 9

OO Design with Class Diagram

Nalinadevi Kadiresan
CSE Dept.
Amrita School of Engg.
19CSE204 Object oriented Programming 2

Diagram of one class


• class name in top of box
– write <<interface>> on top of interfaces'
names
– use italics for an abstract class name

• attributes (optional)
– should include all fields of the object

• operations / methods (optional)


– may omit trivial (get/set) methods
• but don't omit any methods from an interface!
– should not include inherited methods

June 2021 Nalinadevi Kadiresan


19CSE204 Object oriented Programming 3

Class attributes
• attributes (fields, instance variables)
– visibility name : type [count] = default_value
– visibility: + public
# protected
- private
~ package (default)
/ derived
– underline static attributes
– derived attribute: not stored, but can
be computed from other attribute values
– attribute example:
- balance : double = 0.00

June 2021 Nalinadevi Kadiresan


19CSE204 Object oriented Programming 4

Class operations / methods


• operations / methods
– visibility name (parameters) : return_type
– visibility: + public
# protected
- private
~ package (default)
– underline static methods
– parameter types listed as (name: type)
– omit return_type on constructors and
when return type is void
– method example:
+ distance(p1: Point, p2: Point): double
June 2021 Nalinadevi Kadiresan
19CSE204 Object oriented Programming 5

Concrete class and its Java code

public class Car {


private String carColor;
private double carPrice = 0.0;
public String getCarColor(String model) { return carColor;}
public double getCarPrice(String model) { return carPrice;}
} June 2021 Nalinadevi Kadiresan
19CSE204 Object oriented Programming 6

Concrete Class and its Java code


public class Employee {
private static String department = "R&D";
private int empId;

private Employee(int employeeId) { this.empId =


employeeId; }
public static String getEmployee(int emplId) {
if (emplId == 1) {
return "idiotechie";
} else {
return "Employee not found";
}
}
public static String getDepartment() { return
department; }
June 2021 } Nalinadevi Kadiresan
19CSE204 Object oriented Programming 7

Relationships btwn. classes


• generalization: an inheritance relationship
– inheritance between classes
– interface implementation

• association: a usage relationship


– dependency
– aggregation
– composition

June 2021 Nalinadevi Kadiresan


19CSE204 Object oriented Programming 8

Generalization relationships
• generalization (inheritance) relationships
– hierarchies drawn top-down with arrows pointing
upward to parent
– line/arrow styles differ, based on whether parent is
a(n):
• class:
solid line, black arrow
• abstract class:
solid line, white arrow
• interface:
dashed line, white arrow
– we often don't draw trivial / obvious generalization
relationships, such as drawing the Object class as
a parent
June 2021 Nalinadevi Kadiresan
19CSE204 Object oriented Programming 9

Associational relationships
• associational (usage) relationships
1. multiplicity (how many are used)
• *  0, 1, or more
• 1  1 exactly
• 2..4  between 2 and 4, inclusive
• 3..*  3 or more
2. name (what relationship the objects have)
3. navigability (direction)

June 2021 Nalinadevi Kadiresan


19CSE204 Object oriented Programming

Multiplicity of associations
 one-to-one
 each student must carry exactly one ID card

 one-to-many
 one rectangle list can contain many rectangles

10
19CSE204 Object oriented Programming 11

Multiplicity

June 2021 Nalinadevi Kadiresan


19CSE204 Object oriented Programming 12

Association: Model to Implementation


* 4 Course
Student
has enrolls
Class Student {
Course enrolls[4];
}

Class Course {
Student have[];
}

June 2021 Nalinadevi Kadiresan


Car
19CSE204 Object oriented Programming 13

1
aggregation
1
Association types Engine

• aggregation: “has-a"
– symbolized by a clear white diamond Book

composition
• composition:”part-off” / "is entirely made 1
of" *
– stronger version of aggregation
Page
– the parts live and die with the whole
– symbolized by a black diamond

• dependency: "uses temporarily" dependency


– symbolized by dotted line
– often is an implementation Lottery Random
detail, Ticket
June 2021 not an intrinsic part of Nalinadevi Kadiresan
that object's state
19CSE204 Object oriented Programming 14

Association Types
• Aggregation implies a relationship where the
child can exist independently of the parent.
• Composition implies a relationship where the
child cannot exist independent of the parent.
• Composition is a strong Association whereas
Aggregation is a weak Association

June 2021 Nalinadevi Kadiresan


19CSE204 Object oriented Programming 15

Association

public class Customer {


private String name;
private String address;
private String contactNumber;
}
public class Car {
private String modelNumber;
private Customer owner;
}
June 2021 Nalinadevi Kadiresan
19CSE204 Object oriented Programming 16

Association

public class Customer {


private String name;
private String address;
private String contactNumber;
private Car car;
} Allows bidirectional
public class Car {
private String modelNumber; association
Juneprivate
2021 Customer owner; Nalinadevi Kadiresan
19CSE204 Object oriented Programming 17

Multiplicity

public class Car { public class Customer {


private String brand; private Car[] vehicles;
public Car(String brands){ this.brand = ArrayList<Car> carList = new
brands; } ArrayList<Car>();
public Customer(){
public Car() { } vehicles = new Car[2];
vehicles[0] = new Car("Audi");
public String getBrand() { return vehicles[1] = new Car("Mercedes");
brand;} carList.add(new Car("BMW"));
carList.add(new Car("Chevy"));
public void setBrand(String brand) }
{ this.brand
June 2021 = brand;} Nalinadevi Kadiresan
}
19CSE204 Object oriented Programming 18

Aggregation

[]

public class Student {


}

public class School {


private Student[] student;
Does not allow
} bidirectional association
June 2021 Nalinadevi Kadiresan
19CSE204 Object oriented Programming 19

Composition

public class Employee {


}

public class Company {


private Employee[] employee;
}
June 2021 Nalinadevi Kadiresan
19CSE204 Object oriented Programming 20

Interface Services
.
• Interfaces do not get instantiated. They have no
attributes or state. Rather, they specify the
services offered by a related class

<<interface>>
ControlPanel

getChoices : Choice[]
makeChoice (c : Choice)
getSelection : Selection

June 2021 Nalinadevi Kadiresan


19CSE204 Object oriented Programming 21

Example-1: Car Rental Application


Use Case Name: Release a Vehicle (to a Customer)

Description:
A customer arrives to acquire the vehicle and depart
for desired destination. The vehicle reservation
contract is signed and the vehicle is released to the
customer.

Actors: Front-Desk Clerk, Customer


Preconditions: Vehicle has been assigned to the
customer
June 2019 Nalinadevi Kadiresan
19CSE204 Object oriented Programming 22

Basic Flow ("Sunny Day Scenario"):

1. A customer comes to the office to acquire a vehicle.


2. The clerk locates the vehicle reservation contract by
means of the reservation number and/or customer name.
[Exception: Required vehicle type is not available due to late
arrivals.]
3. The customer signs the contract and the clerk gives the
keys to the vehicle.
4. The clerk then marks the contract active by entering the
vehicle release date (today's date) onto the vehicle reservation
contract. The use case terminates at this point.

June 2019 Nalinadevi Kadiresan


19CSE204 Object oriented Programming 23

•Exceptions ("Rainy Day Scenario"):

–Required vehicle type is not available due to late arrivals:


–Raised when the reserved vehicle is not available due to
late returns. The customer is informed of the situation and
told about the other vehicle types that are available. The
customer is offered an incentive to accept another vehicle
type. If the customer is not satisfied, the reservation is
cancelled without penalty charges. The customer either
accepts another vehicle type or cancels the reservation.

•Postconditions: The customer departs with the vehicle and


the reservation contract is marked active, or the reservation is
cancelled.

June 2019 Nalinadevi Kadiresan


19CSE204 Object oriented Programming 24

Example-1 Car Rental Application


Class Diagram

June 2021 Nalinadevi Kadiresan


19CSE204 Object oriented Programming 25

June 2021 Nalinadevi Kadiresan


19CSE204 Object oriented Programming 26

Example-2 Coin Flip


Class Diagram

June 2021 Nalinadevi Kadiresan


19CSE204 Object oriented Programming 27

Class diagram exercise 1


• Let's do a class diagram for the following casual use case, Start
New Poker Round :
The scenario begins when the player chooses to start a new round
in the UI. The UI asks whether any new players want to join the round;
if so, the new players are added using the UI.
All players' hands are emptied into the deck, which is then
shuffled. The player left of the dealer supplies an ante bet of the
proper amount. Next each player is dealt a hand of two cards from the
deck in a round-robin fashion; one card to each player, then the
second card.
If the player left of the dealer doesn't have enough money
to ante, he/she is removed from the game, and the next player
supplies the ante. If that player also cannot afford the ante, this cycle
continues until such a player is found or all players are removed.

June 2021 Nalinadevi Kadiresan


19CSE204 Object oriented Programming 28

June 2021 Nalinadevi Kadiresan

You might also like