0

在 h.getNetYearlyIncome() 失败,似乎 5k 太高了。我整天都在研究它,但无法弄清楚。

为什么 h.getNetYearlyIncome()= h.getGrossYearlyIncome() - h.getTaxesWithheld() 不等于 yearlyIncome-taxesWitheld

public void testSalariedEmployeeMakingOver100K() {
SalariedEmployee h = new SalariedEmployeeImpl("John", "Doe", "Mechanical Turk", 1111, 9166.75);

    assertEquals(h.getFirstName(), "John");
    assertEquals(h.getLastName(), "Doe");
    assertEquals(h.getJobTitle(), "Mechanical Turk");
    assertEquals(h.getID(), 1111);
    assertEquals(h.getMonthlySalary(), 9166.75, 0.005);
    assertEquals(h.getGrossYearlyIncome(), 9166.75*12, 0.005);
    assertEquals(h.getTaxableIncome(), h.getGrossYearlyIncome(), 0.005);
    assertEquals(h.getTaxesWithheld(), 15000.25, 0.005);
    assertEquals(h.getNetYearlyIncome(), h.getGrossYearlyIncome()-h.getTaxesWithheld(), 0.005);
}




public class SalariedEmployeeImpl extends EmployeeImpl implements
    SalariedEmployee {
String first_name;
String last_name;
String job_title;
int id;
double monthly_salary = 0.0;
double yearlyIncome = 0.0;
double taxableIncome = 0.0;
double netIncome = 0.0;
double taxesWitheld = 0.0;
double over100k = 0.0;
double over50k= 0.0;

SalariedEmployeeImpl(String first_name, String last_name, String job_title,
        int id, double monthly_salary) {
    this.first_name = first_name;
    this.last_name = last_name;
    this.job_title = job_title;
    this.id = id;
    this.monthly_salary = monthly_salary;
}

public String getFirstName() {

    return first_name;
}

public String getLastName() {

    return last_name;
}

public String getJobTitle() {

    return job_title;
}

public int getID() {

    return id;
}

public double getGrossYearlyIncome() {

    yearlyIncome = (monthly_salary * 12);

    return yearlyIncome;
}



public double getTaxableIncome() {

    taxableIncome = (monthly_salary*12);
    return taxableIncome;
}


    public double getTaxesWithheld() {

    double over100k = 0.0;
double over50k= 0.0;

    if(taxableIncome>100000.0){
        over100k = taxableIncome -100000.0;
        taxableIncome -=over100k;

    }
    if(taxableIncome >50000.0 && taxableIncome <=100000.0){
        over50k = taxableIncome-50000.0;
        taxableIncome -=over50k;


    }



        taxesWitheld = taxesWitheld + (.15 * over50k)+(.25 * over100k)+(.1*taxableIncome);

        return taxesWitheld ;
}
public double getNetYearlyIncome() {

    return yearlyIncome-taxesWitheld;
}
public double getMonthlySalary() {

    return monthly_salary;
}

public void setMonthlySalary(double salary) {
    this.monthly_salary = salary;

}

}

4

1 回答 1

4

您的函数getGrossYearlyIncome不仅仅是一个 getter 函数——它初始化yearlyIncome. 在您调用该函数之前,yearlyIncome其值为 0。但是,getNetYearlyIncome使用yearlyIncome但不调用getGrossYearlyIncome,因此yearlyIncome在第一次调用时未正确初始化。

getTaxableIncome与and有类似的问题——它们getTaxesWithheld不仅获取值,而且在调用时初始化成员字段。

于 2013-04-14T01:26:09.570 回答