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

Implementation of Java Program To Pass Arguments To A Method and Return Value

Uploaded by

harsita
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Implementation of Java Program To Pass Arguments To A Method and Return Value

Uploaded by

harsita
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

IMPLEMENTATION OF JAVA PROGRAM TO PASS ARGUMENTS TO A

METHOD AND RETURN VALUE


PROGRAM CODE:
class Salary {
double m_salary;
double other;
Salary(double m, double o) {
this.m_salary = m;
this.other = o;
}
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;
}
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;
}
double savings_earned() {
return tsalary - twages;
}
}
public class Exp {
public static void main(String[] args) {
Salary sal = new Salary(32000, 5000);
Wages wag = new Wages(20000, 3000, 5000);
Savings sav = new Savings(sal.m_salary + sal.other, wag.home_wages +
wag.repair_charges + wag.medicinal_wages);
double ts = sal.totalsalary();
double tw = wag.totalwages();
double se = sav.savings_earned();
System.out.println("TOTAL SALARY: " + ts);
System.out.println("TOTAL WAGES: " + tw);
System.out.println("TOTAL SAVINGS: " + se);
}
}
OUTPUT:
IMPLEMENTATION OF JAVA PROGRAM TO PASS ARGUMENTS TO A
METHOD AND RETURN VALUE
PROGRAM CODE:
class Salary {
double m_salary;
double other;
Salary(double m, double o) {
this.m_salary = m;
this.other = o;
}
double totalsalary() {
return m_salary + other;
}
static double totalSalaryFromArray(Salary[] salaries) {
double total = 0;
for (Salary salary : salaries) {
total += salary.totalsalary();
}
return total;
}
}
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;
}
double totalwages() {
return home_wages + repair_charges + medicinal_wages;
}
static double totalWagesFromArray(Wages[] wagesArray) {
double total = 0;
for (Wages wages : wagesArray) {
total += wages.totalwages();
}
return total;
}
}
class Savings {
double tsalary;
double twages;
Savings(double sala, double wage) {
this.tsalary = sala;
this.twages = wage;
}
double savingsEarned() {
return tsalary - twages;
}
static Savings calculateSavings(Salary[] salaries, Wages[] wagesArray) {
double totalSalary = Salary.totalSalaryFromArray(salaries);
double totalWages = Wages.totalWagesFromArray(wagesArray);
return new Savings(totalSalary, totalWages);
}
}
class ExpenseTracker {
double totalExpenses = 0;
void addExpense(double amount) {
totalExpenses += amount;
}
double getTotalExpenses() {
return totalExpenses;
}
}
public class Exp1 {
public static void main(String[] args) {
Salary[] salaries = {
new Salary(32000, 5000),
new Salary(35000, 7000)
};
Wages[] wagesArray = {
new Wages(20000, 3000, 5000),
new Wages(15000, 2000, 3000)
};
Savings sav = Savings.calculateSavings(salaries, wagesArray);
double ts = Salary.totalSalaryFromArray(salaries);
double tw = Wages.totalWagesFromArray(wagesArray);
double se = sav.savingsEarned();
ExpenseTracker expenseTracker = new ExpenseTracker();
expenseTracker.addExpense(1500);
expenseTracker.addExpense(300);
double totalExpenses = expenseTracker.getTotalExpenses();
System.out.println("TOTAL SALARY: " + ts);
System.out.println("TOTAL WAGES: " + tw);
System.out.println("TOTAL SAVINGS: " + se);
System.out.println("TOTAL EXPENSES: " + totalExpenses);
}
}
OUTPUT:

You might also like