Question

In: Computer Science

My class PayCalculator isn't implementing the interface Constants, it did this before but when I saved...

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;
}

Solutions

Expert Solution

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;

      }

}


Related Solutions

(Implementing Interfaces) Implement the following interface and classes: a. Interface IsCompetition, which has no constants and...
(Implementing Interfaces) Implement the following interface and classes: a. Interface IsCompetition, which has no constants and two methods (see the classes below for details about what these methods should do): i. getSummary(), ii. getCompetitors() b. Class Game: A game object is associated with a home team, away team, home score, away score, and a date of competition. It should only be created using the following constructor: Game(String HTeam, String ATeam, int HScore, int AScore, LocalDate date). A game is also...
I was wondering why my merger class isn't compiling - Java -------------------------------------------------------------------------------------------------------------------- public class Person{ private...
I was wondering why my merger class isn't compiling - Java -------------------------------------------------------------------------------------------------------------------- public class Person{ private String firstName; private String lastName; private int age; private String email; private String phone; private String address;       public Person(String firstName, String lastName, int age, String email, String phone, String address){ setFirstName(firstName); setLastName(lastName); setAge(age); setEmail(email); setPhone(phone); setAddress(address); } public Person(String firstName, String lastName, String email){ setFirstName(firstName); setLastName(lastName); setEmail(email); }    public String getFirstName(){ return firstName; }    public String getLastName(){ return lastName; }...
My Javascript code isn't working (when i press calculate button) - what's wrong with it ?...
My Javascript code isn't working (when i press calculate button) - what's wrong with it ? Car Rental Enter Number of Rental Days: Select Car Type: onclick= "priceofcar = 50;"/> Compact onclick= "priceofcar = 60;"/> Economy onclick= "priceofcar = 70;"/> Intermediate Select Loss Damage Waiver onclick= "damagewaiver='yes'"/> Yes onclick= "damagewaiver='no'"/> No damagewaiver = boxDays.value * 25;} else if (damagewaiver == No) { damagewaiver = 0; }/> Select Roadside Issues Coverage: onclick= "issuescoverage='yes'"/> Yes onclick= "issuescoverage='no'"/> No issuescoverage = boxDays.value *...
I am implementing a generic List class and not getting the expected output. My current output...
I am implementing a generic List class and not getting the expected output. My current output is: [0, 1, null] Expected Output in a separate test class: List list = new SparseList<>(); list.add("0"); list.add("1"); list.add(4, "4"); will result in the following list of size 5: [0, 1, null, null, 4]. list.add(3, "Three"); will result in the following list of size 6: [0, 1, null, Three, null, 4]. list.set(3, "Three"); is going to produce a list of size 5 (unchanged): [0,...
I am implementing a generic List class and not getting the expected output. My current output...
I am implementing a generic List class and not getting the expected output. My current output is: [0, 1, null] Expected Output in a separate test class: List list = new SparseList<>(); list.add("0"); list.add("1"); list.add(4, "4"); will result in the following list of size 5: [0, 1, null, null, 4]. list.add(3, "Three"); will result in the following list of size 6: [0, 1, null, Three, null, 4]. list.set(3, "Three"); is going to produce a list of size 5 (unchanged): [0,...
Write the code to create a class Square implementing the interface Figure with constructor to initialize...
Write the code to create a class Square implementing the interface Figure with constructor to initialize with a size and toString method. Write another class SquareUser that creates and array of Squares and initializing with sides 1, 2,3, 4 and 5. Also write the code to call the toString method of squares in a loop to print the string for all squares in the array.
In Python, I've created a Node class for implementing a singly linked list. My Code: class...
In Python, I've created a Node class for implementing a singly linked list. My Code: class Node: def __init__(self,initdata): self.data = initdata self.next = None def getData(self): return self.data def getNext(self): return self.next def setData(self,newdata): self.data = newdata def setNext(self,newnext): self.next = newnext class SinglyLinkedList: def __init__(self): self.head = None def add(self,key): addkey = Node(key) addkey.setNext(self.head) self.head = addkey Now the question is: Create an append method that is O(1) by modifying the constructor of the SinglyLinkedList class by adding...
This is Cost Accounting, I asked this question before, and when according to my professor some...
This is Cost Accounting, I asked this question before, and when according to my professor some of the answers were not correct, can you help me with this question please, thanks Decision Making – Equipment Replacement Mathews manages an assembly facility of Orthom Scientific. A supplier approaches Mathews about replacing a large piece of manufacturing equipment that Orthom uses in its process with a more efficient model. While the supplier made some compelling arguments in favor of replacing the 3-year-old...
My name is Aman and i did my Bachelor of Arts and B.ed first then i...
My name is Aman and i did my Bachelor of Arts and B.ed first then i completed Masters of English Literature and D.ed. I have two years teaching experience in college as an assistant lecturer. The instructions are below: For this Intellectual Autobiography (should not be copied/ Strictly Please 800-1200 WORDS), I would like you to reflect on your life as a learner, thinker, and scholar. It is an opportunity for you to reflect upon and articulate the circumstances and...
#1 a. When to use an Interface vs when to use an abstract class. For each...
#1 a. When to use an Interface vs when to use an abstract class. For each “when” provide extended example(s) (with class/interface codes). b. Suppose you have an interface Moveable. Think of some interface that can extend it. Implement this two interfaces. (java oop)-> laboratory work
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT