无法弄清楚为什么抽象方法没有被覆盖。

huangapple go评论90阅读模式
英文:

Can't figure out why abstract method isn't overriding

问题

抽象雇员类:

  1. abstract public class Employee
  2. {
  3. private String lastName;
  4. private String firstName;
  5. private String ID;
  6. public abstract void increasePay(double percentage);
  7. public abstract void decreasePay(double percentage);
  8. public abstract double getMonthlyPay();
  9. public Employee(String last, String first, String ID)
  10. {
  11. lastName = last;
  12. firstName = first;
  13. this.ID = ID;
  14. }
  15. public void setLast(String last)
  16. {
  17. lastName = last;
  18. }
  19. public void setFirst(String first)
  20. {
  21. firstName = first;
  22. }
  23. public void setIdNumber(String ID)
  24. {
  25. this.ID = ID;
  26. }
  27. public String getLastName()
  28. {
  29. return lastName;
  30. }
  31. public String getFirstName()
  32. {
  33. return firstName;
  34. }
  35. public String getName()
  36. {
  37. return firstName + lastName;
  38. }
  39. public String getIdNumber()
  40. {
  41. return ID;
  42. }
  43. }

小时工类:

  1. public class HourlyWorker extends Employee
  2. {
  3. private int hours;
  4. private double hourlyRate;
  5. private double monthlyPay;
  6. public HourlyWorker(String last, String first, String ID, double rate)
  7. {
  8. super(last, first, ID);
  9. hourlyRate = rate;
  10. }
  11. public void setHours(int hours)
  12. {
  13. this.hours = hours;
  14. }
  15. public int getHours()
  16. {
  17. return hours;
  18. }
  19. public void setHourlyRate(double rate)
  20. {
  21. if ( hours > 160 )
  22. this.hourlyRate = hourlyRate * 1.5;
  23. else
  24. this.hourlyRate = rate;
  25. }
  26. public double getHourlyRate()
  27. {
  28. return hourlyRate;
  29. }
  30. public void setMonthlyPay(double monthlyPay)
  31. {
  32. monthlyPay = hourlyRate * hours;
  33. }
  34. public double getMonthlyPay()
  35. {
  36. return hourlyRate * hours;
  37. }
  38. public void increasePay(double percentage)
  39. {
  40. monthlyPay = monthlyPay * (1 + percentage / 100);
  41. }
  42. public void decreasePay(double percentage)
  43. {
  44. monthlyPay = monthlyPay * (1 - percentage / 100);
  45. }
  46. public String toString()
  47. {
  48. String result = "Name: " + getFirstName() + " " + getLastName() + "\nID: "
  49. + getIdNumber() + " \nHourly Rate: " + hourlyRate;
  50. return result;
  51. }
  52. }

测试类:

  1. public class TestEmployee2
  2. {
  3. public static void main(String[] args)
  4. {
  5. Employee [] staff = new Employee[3];
  6. Supervisor sup = new Supervisor("Boss", "Jim", "JB7865", 54000);
  7. HourlyWorker hw1 = new HourlyWorker("Bee", "Busy", "BB1265", 11.95);
  8. hw1.setHours(200);
  9. staff[0] = sup;
  10. staff[1] = hw1;
  11. System.out.println(staff[0].getMonthlyPay());
  12. staff[0].increasePay(5);
  13. System.out.println(staff[0].getMonthlyPay());
  14. System.out.println(staff[1].getMonthlyPay());
  15. staff[1].increasePay(10);
  16. System.out.println(staff[1].getMonthlyPay());
  17. }
  18. }

主管类:

  1. public class Supervisor extends Employee
  2. {
  3. private double annualSalary;
  4. private double monthlyPay;
  5. public Supervisor(String last, String first, String ID, double salary)
  6. {
  7. super(last, first, ID);
  8. annualSalary = salary;
  9. }
  10. public void setAnnualSalary(double salary)
  11. {
  12. annualSalary = salary;
  13. }
  14. public double getAnnualSalary()
  15. {
  16. return annualSalary;
  17. }
  18. public double getMonthlyPay()
  19. {
  20. return ((annualSalary + (annualSalary * .02)) / 12);
  21. }
  22. public void increasePay(double percentage)
  23. {
  24. monthlyPay = monthlyPay * (1 + percentage / 100);
  25. }
  26. public void decreasePay(double percentage)
  27. {
  28. monthlyPay = monthlyPay * (1 - percentage / 100);
  29. }
  30. public String toString()
  31. {
  32. String result = "Name: " + getFirstName() + " " + getLastName() + "\nID: "
  33. + getIdNumber() + "\nAnnual Salary: " + annualSalary;
  34. return result;
  35. }
  36. }

输出结果:

  1. 4590.0
  2. 4819.5
  3. 2390.0
  4. 2629.0
英文:

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:

  1. abstract public class Employee
  2. {
  3. private String lastName;
  4. private String firstName;
  5. private String ID;
  6. public abstract void increasePay(double percentage);
  7. public abstract void decreasePay(double percentage);
  8. public abstract double getMonthlyPay();
  9. public Employee(String last, String first, String ID)
  10. {
  11. lastName = last;
  12. firstName = first;
  13. this.ID = ID;
  14. }
  15. public void setLast(String last)
  16. {
  17. lastName = last;
  18. }
  19. public void setFirst(String first)
  20. {
  21. firstName = first;
  22. }
  23. public void setIdNumber(String ID)
  24. {
  25. this.ID = ID;
  26. }
  27. public String getLastName()
  28. {
  29. return lastName;
  30. }
  31. public String getFirstName()
  32. {
  33. return firstName;
  34. }
  35. public String getName()
  36. {
  37. return firstName + lastName;
  38. }
  39. public String getIdNumber()
  40. {
  41. return ID;
  42. }

}


HourlyWorkerClass

  1. public class HourlyWorker extends Employee
  2. {
  3. private int hours;
  4. private double hourlyRate;
  5. private double monthlyPay;
  6. public HourlyWorker(String last, String first, String ID, double rate)
  7. {
  8. super(last, first, ID);
  9. hourlyRate = rate;
  10. }
  11. public void setHours(int hours)
  12. {
  13. this.hours = hours;
  14. }
  15. public int getHours()
  16. {
  17. return hours;
  18. }
  19. public void setHourlyRate(double rate)
  20. {
  21. if ( hours > 160 )
  22. this.hourlyRate = hourlyRate * 1.5;
  23. else
  24. this.hourlyRate = rate;
  25. }
  26. public double getHourlyRate()
  27. {
  28. return hourlyRate;
  29. }
  30. public void setMonthlyPay(double monthlyPay)
  31. {
  32. monthlyPay = hourlyRate * hours;
  33. }
  34. public double getMonthlyPay()
  35. {
  36. return hourlyRate * hours;
  37. }
  38. public void increasePay(double percentage)
  39. {
  40. monthlyPay = monthlyPay* percentage;
  41. }
  42. public void decreasePay(double percentage)
  43. {
  44. monthlyPay = monthlyPay* percentage;
  45. }
  46. public String toString()
  47. {
  48. String result = "Name: " + getFirstName() + " " + getLastName() + "\nID: "
  49. + getIdNumber() + " \nHourly Rate: " + hourlyRate;
  50. return result;
  51. }
  52. }

Testing class (currently testing increase

  1. public class TestEmployee2
  2. {
  3. public static void main(String[] args)
  4. {
  5. Employee [] staff = new Employee[3];
  6. Supervisor sup = new Supervisor("Boss", "Jim", "JB7865", 54000);
  7. HourlyWorker hw1 = new HourlyWorker("Bee", "Busy", "BB1265", 11.95);
  8. hw1.setHours(200);
  9. staff[0] = sup;
  10. staff[1] = hw1;
  11. System.out.println(staff[0].getMonthlyPay());
  12. staff[0].increasePay(5);
  13. System.out.println(staff[0].getMonthlyPay());
  14. System.out.println(staff[1].getMonthlyPay());
  15. staff[1].increasePay(10);
  16. System.out.println(staff[1].getMonthlyPay());
  17. }
  18. }

Supervisor class:

  1. public class Supervisor extends Employee
  2. {
  3. private double annualSalary;
  4. private double monthlyPay;
  5. public Supervisor(String last, String first, String ID, double salary)
  6. {
  7. super(last, first, ID);
  8. annualSalary = salary;
  9. }
  10. public void setAnnualSalary(double salary)
  11. {
  12. annualSalary = salary;
  13. }
  14. public double getAnnualSalary()
  15. {
  16. return annualSalary;
  17. }
  18. public double getMonthlyPay()
  19. {
  20. return ((annualSalary + (annualSalary * .02)) / 12);
  21. }
  22. public void increasePay(double percentage)
  23. {
  24. monthlyPay = monthlyPay* percentage;
  25. }
  26. public void decreasePay(double percentage)
  27. {
  28. monthlyPay = monthlyPay* percentage;
  29. }
  30. public String toString()
  31. {
  32. String result = "Name: " + getFirstName() + " " + getLastName() + "\nID: "
  33. + getIdNumber() + "\nAnnual Salary: " + annualSalary;
  34. return result;
  35. }
  36. }

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

答案1

得分: 0

increasePay 方法中,你正在增加 monthlyPay 变量:

  1. public void increasePay(double percentage)
  2. {
  3. monthlyPay = monthlyPay * percentage;
  4. }

但是在 getMonthlyPay 方法中,你是使用另外两个变量 hourlyRatehours 来计算工资:

  1. public double getMonthlyPay()
  2. {
  3. return hourlyRate * hours;
  4. }

所以改变 monthlyPay 不会影响 getMonthlyPay 方法返回的结果。我怀疑在 Supervisor 类中可能也存在类似的情况。

increasePay 方法应该改为增加 hourlyRate

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

另外,在 HourlyEmployee 类中我认为你不需要一个 monthlyPay 字段(或者 setMonthlyPay 方法)。月工资可以通过 hourshourlyRate 计算得出。

<hr>

对于 Supervisor 类,做相同的操作,但是改变 annualSalary 而不是 monthlyPay

  1. public double getMonthlyPay()
  2. {
  3. return ((annualSalary + (annualSalary * .02)) / 12);
  4. }
  5. public void increasePay(double percentage)
  6. {
  7. annualSalary *= 1 + percentage / 100;
  8. }
英文:

In increasePay, you are increasing monthlyPay:

  1. public void increasePay(double percentage)
  2. {
  3. monthlyPay = monthlyPay* percentage;
  4. }

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

  1. public double getMonthlyPay()
  2. {
  3. return hourlyRate * hours;
  4. }

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

increasePay should instead increase hourlyRate:

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

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.

<hr>

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

  1. public double getMonthlyPay()
  2. {
  3. return ((annualSalary + (annualSalary * .02)) / 12);
  4. }
  5. public void increasePay(double percentage)
  6. {
  7. annualSalary *= 1 + percentage / 100;
  8. }

huangapple
  • 本文由 发表于 2020年9月13日 10:47:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63866673.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定