You are on page 1of 4

Course Code: CS2301L Object Oriented Programming.

Lab 10: Composition & Aggregation

Aggregation :( whole – part relationship)

Aggregation is a specialized form of Association in which all objects have their own lifecycle.
There is a relation of ownership between 2 objects but the part object can exist even if we
delete the whole object. Let’s say we have a database in which we store information about
various departments and the teachers present in them. Now let us say, we delete the whole
department from our database. In this scenario, the teacher object should not be deleted
because the teacher may belong to some other department in the same university. We can
think of it as “has-a” relationship. In UML, Aggregation is shown with an open diamond.

Example:

Department Teacher

public class Teacher{


private string name;
public Teacher (string name) {
this.name =name;
}
}
public class Department {
List< Teacher > teacherList;
public Department (List< Teacher >teacherList) {
this.teacherList= teacherList;
}
}

Composition:
Composition is again a specialized form of Aggregation. It is a strong type of Aggregation. Part
object does not have their lifecycle and if the whole object is destroyed then all part objects will
also be destroyed along with it. Let’s take an example of relationship between House and
rooms. House can contain multiple rooms. There is no independent life cycle of room and any
room cannot belong to two different houses. If we delete the house, room will automatically be
deleted. In UML, Composition is shown with a filled diamond.

Centre for Advance Studies in Engineering Page 1


Course Code: CS2301L Object Oriented Programming.
Lab 10: Composition & Aggregation

Example:
House Room

class Room {
String roomType;
int length;
int width;
int height;
public Room(String roomType, int length, int height) {
this.roomType = roomType;
……..
}
}
Public class House {
private List<Room> rooms;
private int noOfRooms;
public House(int noOfRooms) {
this.rooms= new List<Room>(noOfRooms);
rooms.add(new Room(“Drawing Room”, 16, 12, 14));
rooms.add(new Room(“Living Room”, 18, 12, 14));
rooms.add(new Room(“Bed Room”, 14, 12, 14));
rooms.add(new Room(“BedRoom Room”, 12, 12, 14));
this.noOfRooms = noOfRooms;
}
}

Question 1:
You are required to design and implement an Airport simulation.
An airport facilitates various flights, each flight has a number of passengers. If airport closes,
the flights will no longer exist. But if the flight gets cancelled, the passengers in that flight will
continue to exist. Therefore, an Airport can be seen as a composition of flights and passengers,
whereas flights have an aggregation of passengers.

Centre for Advance Studies in Engineering Page 2


Course Code: CS2301L Object Oriented Programming.
Lab 10: Composition & Aggregation

Create a Passenger class.


 Attributes: Passenger Name, Passenger ID, Passenger Gender.
 Define overloaded constructor.
 The member functions include the getters and setters for all the member variables and
toString() function that returns the Passenger details.

Create a Flight class.


 Attributes: Airplane Name, Flight ID, Passengers List (ArrayList of Passengers).
 Define overloaded constructor.
 The member functions include the getters and setters for all the member variables.
 Include getter for Passengers list.
 toString() function that returns the Flight details, including all Passengers.
 Member function addPassenger(Passenger p) which adds a new Passenger into the
flight.
 Member function deletePassenger(Passenger p) which takes input an existing
Passenger’s reference from airport and deletes it from Passenger’s list of that flight.

Create an Airport class.


 Attributes: Airport Name, Airport Address, is multi-run way, and ArrayList of Flights
(Flight data type), and ArrayList of Passengers.
 The member functions include the getters and setters for all the member variables.
 Include getters for flight and Passenger list.
 Except setters for flights and Passengers list.
 toString() method that concatenates and returns details of airport including all flights
and Passengers.
 Member function addFlight(airplaneName, flightID) which creates a new flight using
“new operator” and sets the values accordingly. And then add into the list of flights.
 Member function Passenger addPassenger(name, id, gender) which creates a new
Passenger inside using the input attributes and adds it to the Passenger’s list and then
returns its reference.

Centre for Advance Studies in Engineering Page 3


Course Code: CS2301L Object Oriented Programming.
Lab 10: Composition & Aggregation

Create Main Class and implement a menu as described:

Menu should ask the user the following:


1) Add a new flight
2) Add a new Passenger into a specific flight by following steps below
a. Add Passenger into airport by using Passenger addPassenger(name, id, gender)
function of the airport class.
b. Show the list of available flights to choose from.
c. After selection of specific flight, add Passenger to that flight by using
addPassenger(Passenger passenger) method of flight class. Input the Passenger
object reference that was returned by addPassenger method of the airport class
in the addPassenger method of the flight.
3) Delete a Passenger from a flight.
a. Show a list of flights of airport to choose from.
b. After selection of specific flight, show a list of all Passengers inside that flight.
c. After selection of a specific Passenger delete it from the flight by calling the
deletePassenger(Passenger passenger) method of the flight by giving input its
object reference.
4) View Airport details using toString() functions of each class.

Centre for Advance Studies in Engineering Page 4

You might also like