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

Your function getGrossYearlyIncome is not just a getter function—it initializes yearlyIncome. Until you call that function, yearlyIncome has value 0. However, getNetYearlyIncome uses yearlyIncome but does not call getGrossYearlyIncome, so yearlyIncome is not properly initialized when that is called first.

You have a similar problem with getTaxableIncome and getTaxesWithheld—they not only get a value but initialize member fields when called.

于 2013-04-14T01:26:09.570 に答える