package payroll3;
/**
*
* @author Wiccan
*/
//employee class
public class Employee {
//fields
String name;
double rate;
double hours;
double gross;
double fedtax;
double statetax;
double deduction;
double netpay;
// constructor
public Employee(String name, double rate, double hours) {
this.name = name;
this.rate = rate;
this.hours = hours;
}
//returns net pay
public double getNetPay() {
return gross - deduction;
}
public String getName () {
return name;
}
public void setName (String name) {
this.name = name;
}
public double getHours() {
return hours;
}
public void setHours(double hours) {
this.hours = hours;
}
public double getRate() {
return rate;
}
public void setRate(double rate) {
this.rate = rate;
}
public double getGross() {
return hours*rate;
}
public void setGross(double gross) {
this.gross = gross;
}
public double getFedtax() {
return fedtax*gross;
}
public void setFedtax(double fedtax){
this.fedtax = fedtax;
}
public double getStatetax() {
return statetax*gross;
}
public void setStatetax(double statetax) {
this.statetax = statetax;
}
public double getDeduction() {
return statetax+fedtax;
}
public void setDeduction (double deduction) {
this.deduction = deduction;
}
}
I am basically trying to get the variables within this class to function correctly. When I run it with it’s program I am supposed to get a net pay dollar amount. However, when I run it I get a 0.00 for a dollar amount though I should be getting 296.00 (approx depending on input). I was told that I am not calling the function to set the values. How do I go about doing so? I have tried many different ways and thought I had it right but I seem to always get the same output.
You are mixing methods with simple setter/getters. You have an attribute netPay, but getNetPay() doesn’t return it; instead it calculates it from grossPay and deductions. The result of that calculation is returned to the caller, but not saved in the state of the object. You also have a setGross(), but getGross() doesn’t return that.
Decide what are attributes of the class, and what should be calculated. You need to populate the attributes used for the deductions before you call getNetPay:
(You are missing stateTax and fedTax to drive some of the other calculations). Then you can use those calculations:
Anything that is calculated should not have a set function.