How to use fields from other methods?
I am writing a program that has the following methods:
Code:
class Employee
{
//declare variables
private String name;
private int id;
private double salary;
private int age;
private String position;
public static double ratePercent;
//create constructor with four arguments
Employee(String nm, int empid, double sa, int ag)
{
name = nm;
id = empid;
salary = sa;
age = ag;
}
//create constructor with no arguments
Employee(){}
//create getFedTax() method
double getFedTax()
{
double fedTax = (salary - 800) * 0.17;
return ((int)fedTax);
}
//create getSsTax(rate) method
double getSsTax(int rate)
{
ratePercent = rate / 100;
double ssTax = salary * ratePercent;
return ((int)ssTax);
}
double getHealthFee(int rate)
{
ratePercent = rate / 100;
double empContribution = salary * ratePercent;
return ((int)empContribution);
}
double getInsurance()
{
double rate = 0;
if (age < 40) rate = salary * 0.03;
else if (age >= 40 && age < 50) rate = salary * 0.04;
else if (age >=50 && age <=60) rate = salary * 0.05;
else if (age > 60) rate = salary * 0.06;
double empInsContribution = salary - rate;
return ((int)empInsContribution);
}
}
I need to add one more method, called getNetPay()
which returns the net pay after deducting the amounts found in the previous methods. The problem is that I can't figure out how to do this. I tried this:
Code:
double getNetPay()
{
double deductions = ssTax + empContribution + fedTax + empInsContribution;
double netPay = salary - deductions;
return (netPay);
}
but it didn't work. I believe that it didn't work because the variables I attempted to use are only recognized inside of the methods they were created in. That being the case, how do pull that information out of the methods they are found in so that I can input them into the getNetPay method?