Java Can';我不明白为什么抽象方法是';t覆盖
我的编程任务是编写一个加薪/减薪抽象方法,该方法必须放在抽象的employee类中。我似乎无法在HourlyWorker中得到正确的方法,因此需要按“百分比”增加或减少工资。我的数学很好(月薪-或+(月薪*百分比),但在加薪/减薪后,我在测试课上的成绩是一样的。有什么帮助吗 员工类别:Java Can';我不明白为什么抽象方法是';t覆盖,java,Java,我的编程任务是编写一个加薪/减薪抽象方法,该方法必须放在抽象的employee类中。我似乎无法在HourlyWorker中得到正确的方法,因此需要按“百分比”增加或减少工资。我的数学很好(月薪-或+(月薪*百分比),但在加薪/减薪后,我在测试课上的成绩是一样的。有什么帮助吗 员工类别: abstract public class Employee { private String lastName; private String firstName; private String ID;
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;
}
}
小时工
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;
}
}
测试类(目前测试增加)
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());
}
}
主管级别:
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;
}
}
输出为:
4590.0
4590
2390
2390.0
似乎没有修改getMonthlyPay()
应该是:
4590.00
4819.50
2390
2629.00在
增加工资
中,您每月增加工资
:
public void increasePay(double percentage)
{
monthlyPay = monthlyPay* percentage;
}
public double getMonthlyPay()
{
return ((annualSalary + (annualSalary * .02)) / 12);
}
public void increasePay(double percentage)
{
annualSalary *= 1 + percentage / 100;
}
但是,当您getMonthlyPay
时,您使用另外两个变量计算工资-hourlyRate
和hours
:
public double getMonthlyPay()
{
return hourlyRate * hours;
}
因此,更改monthlyPay
不会影响getMonthlyPay
返回的内容。我怀疑在Supervisor
中也会发生类似的情况
increasePay
应改为增加hourlyRate
:
public void increasePay(double percentage)
{
hourlyRate *= 1 + percentage / 100;
}
另外,我认为员工在小时内根本不需要月工资
字段(或setMonthlyPay
),月工资可以通过小时
和小时工资
计算
对于主管
,执行相同的操作,并更改年度日历
,而不是月度日历
:
public void increasePay(double percentage)
{
monthlyPay = monthlyPay* percentage;
}
public double getMonthlyPay()
{
return ((annualSalary + (annualSalary * .02)) / 12);
}
public void increasePay(double percentage)
{
annualSalary *= 1 + percentage / 100;
}
谢谢你的输入。月薪有什么办法吗?我应该把月薪提高x个百分点。你的方法对小时工有效,每小时工资提高x个百分点,但在主管(我编辑了帖子,将主管级别包括在内)中,只有年薪(这是工资+奖金)。当我做一些类似于公开作废减少工资(双倍百分比){annualSalary=annualSalary-(annualSalary/percentage)}这个数字有点小,因为它在处理之前做了计算。另外,对于5%的增长,我得到了需要满足的测试代码行:staff[0]。increasePay(5);5是我需要增加每月付款的百分比。这就是我试图定制代码的原因towards@ChocolateGoosePoosey为什么月薪计算为((年薪+(年薪*.02))/12)
?是不是应该是年度工资/12
?工资中有2%的奖金。然后我除以12得到每月工资pay@ChocolateGoosePoosey可以使用月薪,但是您应该删除hourlyRate
字段,并使用月薪和h计算小时费率取而代之的是我们的
。真相应该只有一个来源。请阅读@TomerShetah no