This is a relatively simple program but for some reason, I can't compile it. I'm pretty sure the code is correct and working.
My compiler keeps saying that it cannot find a constructor matching a HourlyWorker constructor when I instantiate a HourlyWorker object. I've checked and rechecked the code (in WorkerTester and HourlyWorker classes) to make sure everything matches, but I can't find anything wrong.
Worker.java
public class Worker
{
public Worker(String n, int r)
{
name = n;
rate = r;
}
public double computePay(int hours)
{
return (rate * hours);
}
public int getRate()
{
return rate;
}
private String name;
private int rate;
}
SalariedWorker
public class SalariedWorker extends Worker
{
public SalariedWorker(String n, int r)
{
super(n, r);
}
public double computePay(int hours)
{
double totalPay = 0;
totalPay = this.getRate() * 40;
return totalPay;
}
}
HourlyWorker.java
public class HourlyWorker extends Worker
{
public HourlyWorker(String n, int r)
{
super(n, r);
}
public double computePay(int hours)
{
double totalPay = 0;
if (hours >= 0 && hours <= 40)
totalPay = this.getRate() * hours;
else
totalPay = this.getRate() * ((hours - 40) * 1.5 + 40);
return totalPay;
}
}
/**
This class tests class Worker and its subclasses.
*/
WorkerTester.java
public class WorkerTester
{
public static void main(String[] args)
{
Worker s = new SalariedWorker("Sally", 40);
Worker h = new HourlyWorker("Harry", 40);
System.out.println(s.computePay(30));
System.out.println("Expected: 1600");
System.out.println(h.computePay(30));
System.out.println("Expected: 1200");
System.out.println(s.computePay(50));
System.out.println("Expected: 1600");
System.out.println(h.computePay(50));
System.out.println("Expected: 2200");
}
}