Programming PayCalculator - Write in java and include comments Create an abstrac
ID: 3919148 • Letter: P
Question
Programming PayCalculator - Write in java and include comments
Create an abstract class PayCalculator that has an attribute payRate given in dollars per hour. The class should also have a method computePay(hours) that returns the pay for a given amount of time.
Derive a class RegularPay from PayCalculator, as described above. It should have a constructor that has a parameter for the pay rate. It should not override any of the methods. Then derive a class HazardPay from PayCalculator that overrides the computePay method. The new method should return the amount returned by the base class method multiplied by 1.5.
Grading Rubric
Task
Points
Working solution for the problem
5
Best practices in programming
1
Total
6
Task
Points
Working solution for the problem
5
Best practices in programming
1
Total
6
Explanation / Answer
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
// PayCalculator.java
public abstract class PayCalculator {
//attribute
private double payRate;
/**
* constructor to initialize the payrate
*/
public PayCalculator(double payRate) {
this.payRate=payRate;
}
/**
* method to calculate the payment for given number of hours
*/
public double computePay(int hours){
return payRate*hours;
}
}
// RegularPay.java
public class RegularPay extends PayCalculator {
public RegularPay(double payRate) {
/**
* passing payrate to super class constructor
*/
super(payRate);
}
}
// HazardPay.java
public class HazardPay extends RegularPay {
public HazardPay(double payRate) {
/**
* passing payrate to super class constructor
*/
super(payRate);
}
@Override
public double computePay(int hours) {
/**
* multiplying amount returned by super class computePay() method by 1.5
*/
return super.computePay(hours) * 1.5;
}
}
// Test.java
public class Test {
public static void main(String[] args) {
/**
* creating RegularPay and HazardPay objects and testing computePay()
* method
*/
RegularPay regularPay = new RegularPay(200);
HazardPay hazardPay = new HazardPay(200);
System.out.println("Regular payment for 5 hours: "
+ regularPay.computePay(5));
System.out.println("Hazard payment for 5 hours: "
+ hazardPay.computePay(5));
}
}
/*OUTPUT*/
Regular payment for 5 hours: 1000.0
Hazard payment for 5 hours: 1500.0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.