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

Implementation of Java Program To Demonstrate Aggregation and Composition

Uploaded by

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

Implementation of Java Program To Demonstrate Aggregation and Composition

Uploaded by

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

IMPLEMENTATION OF JAVA PROGRAM TO DEMONSTRATE

AGGREGATION AND COMPOSITION


PROGRAM CODE:
class Salary {
double m_salary;
double other;
Salary(double m, double o) {
this.m_salary = m;
this.other = o;
}
Salary(double m) {
this(m, 0);
}
double totalSalary() {
return m_salary + other;
}
}
class Wages {
double home_wages;
double repair_charges;
double medicinal_wages;
Wages(double wages, double charges, double medicine) {
this.home_wages = wages;
this.repair_charges = charges;
this.medicinal_wages = medicine;
}
Wages(double wages) {
this(wages, 0, 0);
}
double totalWages() {
return home_wages + repair_charges + medicinal_wages;
}
}
class Savings {
double tsalary;
double twages;
Savings(double sala, double wage) {
this.tsalary = sala;
this.twages = wage;
}
Savings(double sala) {
this(sala, 0);
}
double savingsEarned() {
return tsalary - twages;
}
}
class Worker {
private Salary salary;
private Wages wages;
Worker(Salary salary, Wages wages) {
this.salary = salary;
this.wages = wages;
}
double totalIncome() {
return salary.totalSalary() + wages.totalWages();
}
}
class Finance {
private Worker worker;
private Savings savings;
Finance(Worker worker, double expenses) {
this.worker = worker;
this.savings =new Savings(worker.totalIncome(), expenses);
}
void displayFinancialSummary() {
double totalIncome = worker.totalIncome();
double totalSavings = savings.savingsEarned();
System.out.println("Total Income: " + totalIncome);
System.out.println("Total Savings: " + totalSavings);
}
}
public class Exp4 {
public static void main(String[] args) {
Salary sal = new Salary(32000, 5000);
Wages wag = new Wages(20000, 3000, 5000);
Salary sal2 = new Salary(35000);
Wages wag2 = new Wages(15000);
Worker worker1 = new Worker(sal, wag);
Worker worker2 = new Worker(sal2, wag2);
Finance finance1 = new Finance(worker1, 33000);
Finance finance2 = new Finance(worker2, 11370);
System.out.println("Financial Summary for Worker 1:");
finance1.displayFinancialSummary();
System.out.println("Financial Summary for Worker 2:");
finance2.displayFinancialSummary();
}}
OUTPUT:

You might also like