首页 > 解决方案 > Can't figure out why abstract method isn't overriding

问题描述

My programming assignment tasked me with writing an increase/decreasePay abstract method that must be put in my abstract employee class. I can't seem to get the the method correct in HourlyWorker so that it will take increase or decrease the pay by a "percentage". My math is sound (monthly pay - or + (monthly pay * the percentage), but my output in my test class is coming out the same after increasing/decreasing pay. Any help?

Employee class:

abstract public class Employee
{

private String lastName;
private String firstName;
private String ID;


public abstract void increasePay(double percentage);
public abstract void decreasePay(double percentage);
public abstract double getMonthlyPay();

public Employee(String last, String first, String ID)
   {
   lastName = last;
   firstName = first;
   this.ID = ID;
   } 

public void setLast(String last)
   {
      lastName = last;
   }
   
public void setFirst(String first)
   {
      firstName = first;
   }
   
public void setIdNumber(String ID)
   {
      this.ID = ID;
   }

      
public String getLastName()
{
   return lastName;
}

public String getFirstName()
{
   return firstName;
}

public String getName()
{
   return firstName + lastName;
}

public String getIdNumber()
{
   return ID;
}

}


HourlyWorkerClass

public class HourlyWorker extends Employee
{
private int hours;
private double hourlyRate;
private double monthlyPay;

public HourlyWorker(String last, String first, String ID, double rate)
{
   super(last, first, ID);
   hourlyRate = rate;
}

public void setHours(int hours)
{
   this.hours = hours;
}

public int getHours()
{
   return hours;
}

public void setHourlyRate(double rate)
{
   if ( hours > 160 )
       this.hourlyRate = hourlyRate * 1.5;
    else 
       this.hourlyRate = rate;
}

public double getHourlyRate()
{
   return hourlyRate;
}


public void setMonthlyPay(double monthlyPay)
{
   monthlyPay = hourlyRate * hours;
}

public double getMonthlyPay()
{
   return hourlyRate * hours;
}

public void increasePay(double percentage)
{
   monthlyPay = monthlyPay* percentage;
}

public void decreasePay(double percentage)
{
   monthlyPay = monthlyPay* percentage;
}

public String toString() 
   {
        String result = "Name: " + getFirstName() + " " + getLastName() + "\nID: "
                         + getIdNumber() + " \nHourly Rate: " + hourlyRate;
                        return result;
   }

}

Testing class (currently testing increase

public class TestEmployee2
{
   public static void main(String[] args)
   {
   Employee [] staff = new Employee[3];
      Supervisor sup = new Supervisor("Boss", "Jim", "JB7865", 54000);
      HourlyWorker hw1 = new HourlyWorker("Bee", "Busy", "BB1265", 11.95);

      hw1.setHours(200);
            
      staff[0] = sup;
      staff[1] = hw1;        
      
   System.out.println(staff[0].getMonthlyPay());
   staff[0].increasePay(5);
   System.out.println(staff[0].getMonthlyPay());

   System.out.println(staff[1].getMonthlyPay());
   staff[1].increasePay(10);
   System.out.println(staff[1].getMonthlyPay());
   
}
}

Supervisor class:

public class Supervisor extends Employee
{
private double annualSalary;
private double monthlyPay;

public Supervisor(String last, String first, String ID, double salary)
{
   super(last, first, ID);
   annualSalary = salary;
}

public void setAnnualSalary(double salary)
{
   annualSalary = salary;
}

public double getAnnualSalary()
{
   return annualSalary;
}

public double getMonthlyPay()
{
   return ((annualSalary + (annualSalary * .02)) / 12);
}

public void increasePay(double percentage)
{
   monthlyPay = monthlyPay* percentage;
}

public void decreasePay(double percentage)
{
   monthlyPay = monthlyPay* percentage;
}

public String toString() 
   {
        String result = "Name: " + getFirstName() + " " + getLastName() + "\nID: "
                         + getIdNumber() + "\nAnnual Salary: " + annualSalary;
         return result;
   }
}

Output is:

4590.0 4590.0 2390.0 2390.0

Doesn't appear to be modifying getMonthlyPay()

Should be:

4590.00 4819.50 2390.00 2629.00

标签: java

解决方案


In increasePay, you are increasing monthlyPay:

public void increasePay(double percentage)
{
   monthlyPay = monthlyPay* percentage;
}

But when you getMonthlyPay, you calculate the pay using two other variables - hourlyRate and hours:

public double getMonthlyPay()
{
   return hourlyRate * hours;
}

So changing monthlyPay doesn't affect what getMonthlyPay returns. I suspect a similar thing happens in Supervisor.

increasePay should instead increase hourlyRate:

public void increasePay(double percentage)
{
   hourlyRate *= 1 + percentage / 100;
}

Also, I don't think you need a monthlyPay field (or setMonthlyPay, for that matter) in HourlyEmployee at all. The monthly rate can always be calculated by hours and hourlyRate.


For Supervisor, do the same thing, and change annualSalary rather than monthlyPay:

public double getMonthlyPay()
{
   return ((annualSalary + (annualSalary * .02)) / 12);
}

public void increasePay(double percentage)
{
   annualSalary *= 1 + percentage / 100;
}

推荐阅读