英文:
How do I implement a method for calculating overtime pay in an HourlyWorker class
问题
以下是您提供的代码的翻译部分:
我有一个任务,要求计算下面代码中的内容。所有内容都正常工作 - 我只需要计算超过160小时的每月工时,按照正常小时费率的1.5倍支付。我的数学似乎是正确的,计算得很好:
((hours - 160) * overtime) + (160 * hourlyRate)
但是我不知道我是否应该将这个if语句放在正确的方法中,甚至是否应该是一个if语句。在此之前,我的增加/减少支付方法是有效的,并且它们需要保留。我删除了一些内容,以便阅读更容易。
HourlyWorker类:
public class HourlyWorker extends Employee
{
private int hours;
private double hourlyRate;
private double monthlyPay;
private double overtime = (1.5 * hourlyRate);
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)
{
   this.hourlyRate = rate;
}
public double getHourlyRate()
{
   return hourlyRate;
}
public double getMonthlyPay()
{
   if (hours > 160)
   {
      monthlyPay = ((hours - 160) * overtime) + (160 * hourlyRate);
   }
   else 
   {
      monthlyPay = hourlyRate * hours;
   }
   return monthlyPay;
}
public void increasePay(double percentage)
{
   hourlyRate *= 1 + percentage / 100;
}
public void decreasePay(double percentage)
{
   hourlyRate *= 1 - percentage / 100;
}
}
测试内容:
public class TestEmployee2
{
   public static void main(String[] args)
   {
   Employee [] staff = new Employee[3];
      HourlyWorker hw1 = new HourlyWorker("Bee", "Busy", "BB1265", 10);
       
      hw1.setHours(200);	
      staff[0] = hw1;
   System.out.println(staff[0].getMonthlyPay());
   staff[0].increasePay(10);
   System.out.println(staff[0].getMonthlyPay());
}
}
输出结果:
1600  (初始每月费率,包括40小时的加班和160小时的正常工时)
1760  (月薪增加10%)
应该是:
2006
2206.6
英文:
I have an assignment which asks for everything I have in the code below. That all works fine - I just need to calculate any monthly hours over 160 hours to be paid at 1.5 times the normal hourly rate. My math seems sound and calculates fine:
> ((hours - 160) * overtime) + (160 * hourlyRate)
But I dont know if I'm putting this if statement in the right method or if it even should be an if statement. My increase/decreasePay methods are working prior to this and they need to stay. I removed some things so it's easier to read.
HourlyWorker Class:
public class HourlyWorker extends Employee
{
private int hours;
private double hourlyRate;
private double monthlyPay;
private double overtime = (1.5 * hourlyRate);
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)
{
   this.hourlyRate = rate;
}
public double getHourlyRate()
{
   return hourlyRate;
}
public double getMonthlyPay()
{
   if (hours > 160)
   {
      monthlyPay = ((hours - 160) * overtime) + (160 * hourlyRate);
   }
   else 
   {
      monthlyPay = hourlyRate * hours;
   }
   return monthlyPay;
}
public void increasePay(double percentage)
{
   hourlyRate *= 1 + percentage / 100;
}
public void decreasePay(double percentage)
{
   hourlyRate *= 1 - percentage / 100;
}
}
What I'm testing with:
public class TestEmployee2
{
   public static void main(String[] args)
   {
   Employee [] staff = new Employee[3];
      HourlyWorker hw1 = new HourlyWorker("Bee", "Busy", "BB1265", 10);
       
      hw1.setHours(200);	
      staff[0] = hw1;
   System.out.println(staff[0].getMonthlyPay());
   staff[0].increasePay(10);
   System.out.println(staff[0].getMonthlyPay());
}
}
Output is: 
1600  (initial monthly rate, with 40 overtime hours and 160 regular hours)
1760  (10% increase to the monthlyPay)
Should be: 
2006 
2206.6
答案1
得分: 2
你的代码存在以下问题:
- 
字段初始化程序在构造函数体之前运行,因此
overtime = (1.5 * hourlyRate)使用hourlyRate字段的默认值 0,计算出一个overtime值为 0,且永远不会重新计算,因为初始化程序只在初始化期间运行一次。 - 
setHourlyRate()更新了hourlyRate字段,但未重新计算overtime字段的值。increasePay()和decreasePay()也是同样的情况。 - 
monthlyPay字段没有意义,因为你实际上从未使用过它,考虑到你只将它用作getMonthlyPay()方法中的局部变量。 
消除 monthlyPay 和 overtime 字段,将它们都作为 getMonthlyPay() 方法中的局部变量。
FYI: 在 Java 中,不建议使用 double 类型表示货币金额。推荐用于货币的数据类型是 BigDecimal。
英文:
You code has the following issues:
- 
Field initializers run before the body of the constructor, so
overtime = (1.5 * hourlyRate)uses the default value of 0 for thehourlyRatefield, calculating anovertimevalue of 0, and is never recalculated, since initializers only run once, during initialization. - 
setHourlyRate()updates thehourlyRatefield, but doesn't re-calculate the value for theovertimefield. Same forincreasePay()anddecreasePay(). - 
There is no point to the
monthlyPayfield, since you never really use it, given that you only use it as-if it was a local variable in thegetMonthlyPay()method. 
Get rid of fields monthlyPay and overtime, and make them both local variables in the getMonthlyPay() method.
FYI: Using double for currency amounts is discouraged. The recommended type for currency in Java is BigDecimal.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论