Docsity

Object oriented programming in java, Lecture notes of Object Oriented Programming

Partial preview of the text

Download Object oriented programming in java and more Lecture notes Object Oriented Programming in PDF only on Docsity!

CS-212: Object Oriented

Programming

Composition & Aggregation

Software Design-Process

  • (^) You have been asked to write a payroll processing application for a company. There are two types of employees ─ commission, and base salaried plus commission employees. Commission employees (are paid a percentage of their sales, while base-salaried plus commission employees receive a base salary plus a percentage of their sales.
  • (^) All commission employees have the following attributes: first name, last name, social security number, commission rate and gross (i.e., total) sales amount. While each base- salaried plus commission employee possesses the following attributes: first name, last name, social security number, commission rate, gross sales amount and base salary. Include method earnings and toString for each employee class to calculate and print the payroll information.
  • (^) Write a test program for your application that demonstrates the capability of your classes.

Real World Relationships

  • (^) Manager is an employee of a trading company.
  • (^) Manager has a team of workers who work under him.
  • (^) Manager has a bank account.

Effective Software Design-Reusing

Classes

  • (^) Code Reuse: Instead of creating new classes from scratch, you use existing classes that someone has already built and debugged.
  • (^) Two ways to accomplish this: 1. Inheritance 2. Composition

Composition

  • (^) A class can have references to objects of other
classes as members. This is called composition and
is sometimes referred to as a has-a relationship.
  • (^) Two classes have composition relationship when
one class completely depends upon other class
and it does not have existence outside the
owning class. When owning class is destroyed,
then the owned class also gets destroyed.

Composition -Example

  • (^) For our composition example, consider following few scenarios:  (^) A Wall class which contains Window object i.e. a Wall HAS-A Window  (^) The Window cannot exist without a wall !!  (^) So whenever a wall is demolished, window also does not exist.

Composition-Concept Check

class Employee{ String name; Date dob; int age; } Do you see a composition relationship in the Employee class?

Composition-Example

Every person has a date of birth.

Self-Review Exercise

  • (^) Write a class to represent a Musical Band. A

musical band may have different members such

as guitarist, drummer and a vocalist.

class Band{ Guitarist gtr; Drummer drm; Vocalist voc; }

Composition -Reusability

  • (^) Example: Car Door Tyre has-a Aeroplane has-a

Composition vs. Aggregation

  • (^) Aggregation is a closely related concept to composition.
  • (^) Composition: The internal objects are exclusively owned by the main object. If you destroy the main object then internal objects are also destroyed.
  • (^) Aggregation: The internal objects are not owned by the main object. They can exist independently from the main object.

Self-Review Exercise

  • (^) Suppose you need to process course related information for SE-9. Each course has a name and has certain number of students enrolled in it. A new course object can be created using a parameterized constructor that accepts the course name. Write methods that allow new students to be added to the course and also for deletion of a student from the course. Introduce a method getEnrolledStudents that returns all the students currently taking the course.
  • (^) Write a test program for any 2 courses being offered to SE-9.