In: Computer Science
My class PayCalculator isn't implementing the interface Constants, it did this before but when I saved it went nuts. I was wondering how to fix it?
import java.text.DecimalFormat;
public class PayCalculator implements Constants {
private String employeeName;
private int reportID;
private double hourlyWage;
private static int ID = 0;
private static int reportIDGenerator = 1000;
public int[] overtimes = HOURS_WORKED;
public PayCalculator(String name) {
public PayCalculator() {
this.reportID = reportIDGenerator;
reportIDGenerator+=10;
this.overtimes = HOURS_WORKED;
}
}
public PayCalculator(String name, double hourlyWage) {
this.reportID = reportIDGenerator;
reportIDGenerator+=10;
this.employeeName = name;
this.hourlyWage = hourlyWage;
this.overtimes = HOURS_WORKED;
}
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
public int getReportID() {
return reportID;
}
public double getHourlyWage() {
return hourlyWage;
}
public void setHourlyWage(double hourlyWage) {
this.hourlyWage = hourlyWage;
}
@Override
public String toString() {
return "PayCalculator [employeeName=" + employeeName + ",
reportId=" + reportID +
", hourlyWage=" + hourlyWage+
"]";
}
public double calculateYearlyGrossPay(){
double totalGross = 0.0;
for(int period = 1; period <= PAY_PERIODS_IN_YEAR;
period++){
totalGross += calculateGrossForPeriod(period);
}
return totalGross;
}
public double calculateYearlyNetPay(){
double totalNet = 0.0;
for(int period = 1; period <= PAY_PERIODS_IN_YEAR;
period++){
totalNet += calculateNetPayForPeriod(period);
}
return totalNet;
}
public double calculateNetPayForPeriod(int periodNumber){
double gross = calculateGrossForPeriod(periodNumber);
double tax = calculateTax(gross);
double netPay = gross-tax;
return netPay;
}
public double PAY_PERIODS_IN_YEAR(int periodNumber){
double gross = calculateGrossForPeriod(periodNumber);
double tax = calculateTax(gross);
double netPay = gross-tax;
return netPay;
}
public void printNetPayForAllPeriods(){
DecimalFormat df = new DecimalFormat("#.00");
System.out.println("NET PAY for all periods:\n");
for(int period =1; period <= PAY_PERIODS_IN_YEAR;
period++){
System.out.println("PERIOD:"+period+" NET
PAY:"+df.format(calculateNetPayForPeriod(period)));
}
}
public void increaseWageRate(double percentage){
hourlyWage =hourlyWage+ hourlyWage*(percentage/100);
}
private double calculateGrossForPeriod(int periodNumber){
double regulayPay = FULL_TIME*hourlyWage;
double overtimePay =
overtimes[periodNumber-1]*(hourlyWage*OVERTIME_RATE);
double gross= regulayPay+overtimePay;
return gross;
}
private double calculateTax(double gross){
double federalTax = gross*FEDERAL_TAX_RATE;
double stateTax = gross*STATE_TAX_RATE;
return federalTax+stateTax;
}
}
public interface Constants {
public final int[] HOURS_WORKED = {89, 80, 19, 73, 44,
99, 77, 0, 80, 70, 80, 87, 84, 82,
80, 30, 89, 90,
100, 120, 0, 69, 99, 91, 83, 80};
public final int PAY_PERIODS_IN_YEAR = 26;
public final double FEDERAL_TAX_RATE = 0.2;
public final double STATE_TAX_RATE = 0.09;
public final double OVERTIME_RATE = 1.5;
public final double FULL_TIME = 80;
public final int PAY_PERIOD_1 = 0;
public final int PAY_PERIOD_2 = 1;
public final int PAY_PERIOD_3 = 2;
public final int PAY_PERIOD_4 = 3;
public final int PAY_PERIOD_5 = 4;
public final int PAY_PERIOD_6 = 5;
public final int PAY_PERIOD_7 = 6;
public final int PAY_PERIOD_8 = 7;
public final int PAY_PERIOD_9 = 8;
public final int PAY_PERIOD_10 = 9;
public final int PAY_PERIOD_11 = 10;
public final int PAY_PERIOD_12 = 11;
public final int PAY_PERIOD_13 = 12;
public final int PAY_PERIOD_14 = 13;
public final int PAY_PERIOD_15 = 14;
public final int PAY_PERIOD_16 = 15;
public final int PAY_PERIOD_17 = 16;
public final int PAY_PERIOD_18 = 17;
public final int PAY_PERIOD_19 = 18;
public final int PAY_PERIOD_20 = 19;
public final int PAY_PERIOD_21 = 20;
public final int PAY_PERIOD_22 = 21;
public final int PAY_PERIOD_23 = 22;
public final int PAY_PERIOD_24 = 23;
public final int PAY_PERIOD_25 = 24;
public final int PAY_PERIOD_26 = 25;
}
The problem was not just because of the implementation of Constants interface, PayCalculator constructor taking an employee name was wrongly written, the default constructor was written inside that constructor which caused compile errors. Fixed it. Now back to the Constants interface, I don’t think your PayCalculator should implement Constants interface. Inheritance is not needed here, if PayCalculator implements Constants, then we can say PayCalculator is an instance/child of Constants. Does not make any sense. Since Constants interface only contain public constants, what you should do is use them without implementing Constants interface. For example, if you want to access PAY_PERIOD_1 constant outside the interface, you can just simply call Constants.PAY_PERIOD_1
Here is the fixed code for this problem. Only attached PayCalculator.java since Constants interface is unmodified. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
Note: If you still want PayCalculator to implement Constansts interface, let me know.
// PayCalculator.java
import java.text.DecimalFormat;
public class PayCalculator {
private String employeeName;
private int reportID;
private double hourlyWage;
private static int ID = 0; // dont know the use of this variable.
private static int reportIDGenerator = 1000;
public int[] overtimes = Constants.HOURS_WORKED;
// there was an error inside this constructor, actually, you were writing
// another constructor within this. Fixed it. Now it should work
public PayCalculator(String name) {
this.reportID = reportIDGenerator;
reportIDGenerator += 10;
// using HOURS_WORKED constant from Constants interface, assigning to
// overtimes`
this.overtimes = Constants.HOURS_WORKED;
// setting name
employeeName = name;
}
public PayCalculator() {
this.reportID = reportIDGenerator;
reportIDGenerator += 10;
this.overtimes = Constants.HOURS_WORKED;
}
public PayCalculator(String name, double hourlyWage) {
this.reportID = reportIDGenerator;
reportIDGenerator += 10;
this.employeeName = name;
this.hourlyWage = hourlyWage;
this.overtimes = Constants.HOURS_WORKED;
}
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
public int getReportID() {
return reportID;
}
public double getHourlyWage() {
return hourlyWage;
}
public void setHourlyWage(double hourlyWage) {
this.hourlyWage = hourlyWage;
}
@Override
public String toString() {
return "PayCalculator [employeeName=" + employeeName + ", reportId="
+ reportID + ", hourlyWage=" + hourlyWage + "]";
}
public double calculateYearlyGrossPay() {
double totalGross = 0.0;
for (int period = 1; period <= Constants.PAY_PERIODS_IN_YEAR; period++) {
totalGross += calculateGrossForPeriod(period);
}
return totalGross;
}
public double calculateYearlyNetPay() {
double totalNet = 0.0;
for (int period = 1; period <= Constants.PAY_PERIODS_IN_YEAR; period++) {
totalNet += calculateNetPayForPeriod(period);
}
return totalNet;
}
public double calculateNetPayForPeriod(int periodNumber) {
double gross = calculateGrossForPeriod(periodNumber);
double tax = calculateTax(gross);
double netPay = gross - tax;
return netPay;
}
public double PAY_PERIODS_IN_YEAR(int periodNumber) {
double gross = calculateGrossForPeriod(periodNumber);
double tax = calculateTax(gross);
double netPay = gross - tax;
return netPay;
}
public void printNetPayForAllPeriods() {
DecimalFormat df = new DecimalFormat("#.00");
System.out.println("NET PAY for all periods:\n");
for (int period = 1; period <= Constants.PAY_PERIODS_IN_YEAR; period++) {
System.out.println("PERIOD:" + period + " NET PAY:"
+ df.format(calculateNetPayForPeriod(period)));
}
}
public void increaseWageRate(double percentage) {
hourlyWage = hourlyWage + hourlyWage * (percentage / 100);
}
private double calculateGrossForPeriod(int periodNumber) {
double regulayPay = Constants.FULL_TIME * hourlyWage;
double overtimePay = overtimes[periodNumber - 1]
* (hourlyWage * Constants.OVERTIME_RATE);
double gross = regulayPay + overtimePay;
return gross;
}
private double calculateTax(double gross) {
double federalTax = gross * Constants.FEDERAL_TAX_RATE;
double stateTax = gross * Constants.STATE_TAX_RATE;
return federalTax + stateTax;
}
}