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

Implementation of Java Program To Demonstrate Method Overloading and Constructor Overloading

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 DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Implementation of Java Program To Demonstrate Method Overloading and Constructor Overloading

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 DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

IMPLEMENTATION OF JAVA PROGRAM TO DEMONSTRATE METHOD

OVERLOADING AND CONSTRUCTOR OVERLOADING


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_salary = m;
this.other = 0;
}

double totalsalary() {
return m_salary + other;
}

double totalsalary(double bonus) {


return m_salary + other + bonus;
}
}

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.home_wages = wages;
this.repair_charges = 0;
this.medicinal_wages = 0;
}

double totalwages() {
return home_wages + repair_charges + medicinal_wages;
}

double totalwages(double additionalExpenses) {


return home_wages + repair_charges + medicinal_wages + additionalExpenses;
}
}

class Savings {
double tsalary;
double twages;

Savings(double sala, double wage) {


this.tsalary = sala;
this.twages = wage;
}

Savings(double sala) {
this.tsalary = sala;
this.twages = 0;
}

double savings_earned() {
return tsalary - twages;
}

double savings_earned(double interest) {


return (tsalary - twages) + interest;
}
}

public class Exp3 {


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);

double ts1 = sal.totalsalary();


double ts2 = sal2.totalsalary(2000);
double tw1 = wag.totalwages();
double tw2 = wag2.totalwages(1000);

Savings sav = new Savings(ts1, tw1);


double se1 = sav.savings_earned();
double se2 = sav.savings_earned(500);

System.out.println("TOTAL SALARY (with two arguments): " + ts1);


System.out.println("TOTAL SALARY (with bonus): " + ts2);
System.out.println("TOTAL WAGES (with three arguments): " + tw1);
System.out.println("TOTAL WAGES (with additional expenses): " + tw2);
System.out.println("TOTAL SAVINGS (without interest): " + se1);
System.out.println("TOTAL SAVINGS (with interest): " + se2);
}
}
OUTPUT:
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, Savings savings) {
this.worker = worker;
this.savings = savings;
}
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);
Savings sav1 = new Savings(worker1.totalIncome());
Savings sav2 = new Savings(worker2.totalIncome(), 1000);
Finance finance1 = new Finance(worker1, sav1);
Finance finance2 = new Finance(worker2, sav2);
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