Question

In: Computer Science

JAVA Specify, design, and implement a class called PayCalculator. The class should have at least the...

JAVA

Specify, design, and implement a class called PayCalculator. The class should have at least the following instance variables:

  • employee’s name

  • reportID: this should be unique. The first reportID must have a value of 1000 and for each new reportID you should increment by 10.

  • hourly wage

Include a suitable collection of constructors, mutator methods, accessor methods, and toString method. Also, add methods to perform the following tasks:

  1. Compute yearly salary - both the gross pay and net pay

  2. Increase the salary by a certain percentage

  3. Compute pay check net pay for a given pay period (there are 26 pay periods per year). Here is what you have to consider when computing the net pay:

    1. Federal Tax deductions – 9%

    2. State Tax deductions – 2%

    3. Overtime - number of hours worked over the 80hrs full time load. Here is how you can calculate the overtime pay rate

overTimePay = regularPayRate * 1.5

  1. Compute pay check net pay for all pay periods (all 26 pay periods)

Solutions

Expert Solution

////////////////////PayCalculator.java//////////////////

package test.pay;

import java.text.DecimalFormat;
import java.util.Random;

public class PayCalculator {
   private static int reportIdGenerator = 1000;
   private String employeeName;
   private int reportId;
   private double hourlyWage;
   private int[] overtimes = null;
          
   private static final int TOTAL_PAY_PERIOD = 26;
   private static final double FEDERAL_TAX = 0.09; //9%
   private static final double STATE_TAX = 0.02; //2%
   private static final double FULL_TIME_LOAD = 80; //80 hours
   private static final double OVERTIME_PAY_FACTOR = 1.5; //1.5 times of regular pay
  
  
   public PayCalculator(){
       this.reportId = reportIdGenerator;
       reportIdGenerator+=10;
       this.overtimes = generateOvertimes();
   }
  
   public PayCalculator(String name,double hourlyWage){
       this.reportId = reportIdGenerator;
       reportIdGenerator+=10;
       this.employeeName = name;
       this.hourlyWage = hourlyWage;
       this.overtimes = generateOvertimes();
   }

   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
               + "]";
   }
  
   /**
   * calculateYearlyGrossPay
   * @return
   */
   public double calculateYearlyGrossPay(){
       double totalGross = 0.0;  
       for(int period = 1; period <= TOTAL_PAY_PERIOD; period++){
           totalGross += calculateGrossForPeriod(period);
       }  
       return totalGross;
   }
  
   /**
   * calculateYerlyNetPay
   * @return
   */
   public double calculateYerlyNetPay(){
       double totalNet = 0.0;  
       for(int period = 1; period <= TOTAL_PAY_PERIOD; period++){
           totalNet += calculateNetPayForPeriod(period);
       }
       return totalNet;
   }
  
   /**
   * calculateNetPayForPeriod : calculate net pay for given period number
   * @return
   */
   public double calculateNetPayForPeriod(int periodNumber){
       double gross = calculateGrossForPeriod(periodNumber);
       double tax = calculateTax(gross);
       double netPay = gross-tax;
       return netPay;  
   }
  
   /**
   * print net pay for all 26 periods
   */
   public void printNetPayForAllPeriods(){
       DecimalFormat df = new DecimalFormat("#.00");
       System.out.println("NET PAY for all periods:\n");
       for(int period =1; period <= TOTAL_PAY_PERIOD; period++){
           System.out.println("PERIOD:"+period+" NET PAY:"+df.format(calculateNetPayForPeriod(period)));
       }
   }
  
   /**
   * increase hoourly wage by certain percentage%
   * @param percentage
   */
   public void increaseWageRate(double percentage){
       hourlyWage =hourlyWage+ hourlyWage*(percentage/100);
   }
  
   private double calculateGrossForPeriod(int periodNumber){
       double regulayPay = FULL_TIME_LOAD*hourlyWage;
       double overtimePay = overtimes[periodNumber-1]*(hourlyWage*OVERTIME_PAY_FACTOR);
       double gross= regulayPay+overtimePay;
       return gross;
   }
  
   private double calculateTax(double gross){
       double federalTax = gross*FEDERAL_TAX;
       double stateTax = gross*STATE_TAX;
       return federalTax+stateTax;
   }
  
   /**
   * generate random overtime hours between 0 to 15 for 26 pay periods
   * @return
   */
   private int[] generateOvertimes(){
       int[] overtimes = new int[TOTAL_PAY_PERIOD];
       for(int i = 0; i < overtimes.length; i++){
           //will generate overtime between 0 to 15
           overtimes[i] = (int)(Math.random()*16);
       }
      
       return overtimes;
   }
  
}

/////////////////////////TestPay.java//////////////////////////

package test.pay;

import java.text.DecimalFormat;

public class TestPay {
  
   public static void main(String[] args){
       DecimalFormat df = new DecimalFormat("#.00");
       System.out.println("----------------------------------------------------");
       PayCalculator p1 = new PayCalculator();
       p1.setEmployeeName("Henry Brown");
       p1.setHourlyWage(9.8);
      
       System.out.println(p1);
       System.out.println("Yerly gross pay: "+ df.format(p1.calculateYearlyGrossPay()));
       System.out.println("Yerly Net pay: "+ df.format(p1.calculateYerlyNetPay()));
       System.out.println("Net Pay for period 5 is:"+ df.format(p1.calculateNetPayForPeriod(5)));
       p1.printNetPayForAllPeriods();
      
       System.out.println("----------------------------------------------------");
       PayCalculator p2 = new PayCalculator("Thomas Hilly",7.60);
       System.out.println(p2);
      
       System.out.println("Yerly gross pay: "+ df.format(p2.calculateYearlyGrossPay()));
       System.out.println("Yerly Net pay: "+ df.format(p2.calculateYerlyNetPay()));
       System.out.println("Net Pay for period 12 is:"+ df.format(p2.calculateNetPayForPeriod(12)));
       p2.printNetPayForAllPeriods();
      
       p2.increaseWageRate(12); //increase hourly wage by 12%
       System.out.println("New wage for "+p2.getEmployeeName()+" is: "+ p2.getHourlyWage());
       p2.printNetPayForAllPeriods();
      
      
       System.out.println("----------------------------------------------------");
       PayCalculator p3 = new PayCalculator("Samuel Johnson",9);
       System.out.println(p3);
       System.out.println("Yerly gross pay: "+ df.format(p3.calculateYearlyGrossPay()));
       System.out.println("Yerly Net pay: "+ df.format(p3.calculateYerlyNetPay()));
       System.out.println("Net Pay for period 21 is:"+ df.format(p3.calculateNetPayForPeriod(21)));
      
   }

}

============================

OUTPUT

===========================

----------------------------------------------------
PayCalculator [employeeName=Henry Brown, reportId=1000, hourlyWage=9.8]
Yerly gross pay: 23897.30
Yerly Net pay: 21268.60
Net Pay for period 5 is:723.93
NET PAY for all periods:

PERIOD:1 NET PAY:894.00
PERIOD:2 NET PAY:802.42
PERIOD:3 NET PAY:828.59
PERIOD:4 NET PAY:867.84
PERIOD:5 NET PAY:723.93
PERIOD:6 NET PAY:894.00
PERIOD:7 NET PAY:854.76
PERIOD:8 NET PAY:789.34
PERIOD:9 NET PAY:894.00
PERIOD:10 NET PAY:880.92
PERIOD:11 NET PAY:841.67
PERIOD:12 NET PAY:789.34
PERIOD:13 NET PAY:880.92
PERIOD:14 NET PAY:854.76
PERIOD:15 NET PAY:828.59
PERIOD:16 NET PAY:723.93
PERIOD:17 NET PAY:750.09
PERIOD:18 NET PAY:697.76
PERIOD:19 NET PAY:737.01
PERIOD:20 NET PAY:750.09
PERIOD:21 NET PAY:815.51
PERIOD:22 NET PAY:894.00
PERIOD:23 NET PAY:815.51
PERIOD:24 NET PAY:776.26
PERIOD:25 NET PAY:828.59
PERIOD:26 NET PAY:854.76
----------------------------------------------------
PayCalculator [employeeName=Thomas Hilly, reportId=1010, hourlyWage=7.6]
Yerly gross pay: 18110.80
Yerly Net pay: 16118.61
Net Pay for period 12 is:622.29
NET PAY for all periods:

PERIOD:1 NET PAY:541.12
PERIOD:2 NET PAY:662.87
PERIOD:3 NET PAY:591.85
PERIOD:4 NET PAY:561.41
PERIOD:5 NET PAY:632.43
PERIOD:6 NET PAY:551.27
PERIOD:7 NET PAY:612.14
PERIOD:8 NET PAY:683.16
PERIOD:9 NET PAY:673.02
PERIOD:10 NET PAY:642.58
PERIOD:11 NET PAY:622.29
PERIOD:12 NET PAY:622.29
PERIOD:13 NET PAY:561.41
PERIOD:14 NET PAY:693.31
PERIOD:15 NET PAY:612.14
PERIOD:16 NET PAY:652.73
PERIOD:17 NET PAY:571.56
PERIOD:18 NET PAY:622.29
PERIOD:19 NET PAY:662.87
PERIOD:20 NET PAY:673.02
PERIOD:21 NET PAY:632.43
PERIOD:22 NET PAY:673.02
PERIOD:23 NET PAY:591.85
PERIOD:24 NET PAY:602.00
PERIOD:25 NET PAY:561.41
PERIOD:26 NET PAY:612.14
New wage for Thomas Hilly is: 8.512
NET PAY for all periods:

PERIOD:1 NET PAY:606.05
PERIOD:2 NET PAY:742.42
PERIOD:3 NET PAY:662.87
PERIOD:4 NET PAY:628.78
PERIOD:5 NET PAY:708.33
PERIOD:6 NET PAY:617.42
PERIOD:7 NET PAY:685.60
PERIOD:8 NET PAY:765.14
PERIOD:9 NET PAY:753.78
PERIOD:10 NET PAY:719.69
PERIOD:11 NET PAY:696.96
PERIOD:12 NET PAY:696.96
PERIOD:13 NET PAY:628.78
PERIOD:14 NET PAY:776.51
PERIOD:15 NET PAY:685.60
PERIOD:16 NET PAY:731.05
PERIOD:17 NET PAY:640.14
PERIOD:18 NET PAY:696.96
PERIOD:19 NET PAY:742.42
PERIOD:20 NET PAY:753.78
PERIOD:21 NET PAY:708.33
PERIOD:22 NET PAY:753.78
PERIOD:23 NET PAY:662.87
PERIOD:24 NET PAY:674.24
PERIOD:25 NET PAY:628.78
PERIOD:26 NET PAY:685.60
----------------------------------------------------
PayCalculator [employeeName=Samuel Johnson, reportId=1020, hourlyWage=9.0]
Yerly gross pay: 21312.00
Yerly Net pay: 18967.68
Net Pay for period 21 is:724.90


Related Solutions

JAVA - Design and implement a class called Flight that represents an airline flight. It should...
JAVA - Design and implement a class called Flight that represents an airline flight. It should contain instance data that represent the airline name, the flight number, and the flight’s origin and destination cities. Define the Flight constructor to accept and initialize all instance data. Include getter and setter methods for all instance data. Include a toString method that returns a one-line description of the flight. Create a driver class called FlightTest, whose main method instantiates and updates several Flight...
Java programming language should be used Implement a class called Voter. This class includes the following:...
Java programming language should be used Implement a class called Voter. This class includes the following: a name field, of type String. An id field, of type integer. A method String setName(String) that stores its input into the name attribute, and returns the name that was just assigned. A method int setID(int) that stores its input into the id attribute, and returns the id number that was just assigned. A method String getName() that return the name attribute. A method...
Write a Java class called Person. The class should have the following fields: A field for...
Write a Java class called Person. The class should have the following fields: A field for the person’s name. A field for the person’s SSN. A field for the person’s taxable income. A (Boolean) field for the person’s marital status. The Person class should have a getter and setter for each field. The Person class should have a constructor that takes no parameters and sets the fields to the following values: The name field should be set to “unknown”. The...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named coin and an object of the class Random called r. Coin will have a value of 0 or 1 (corresponding to heads or tails respectively). The constructor should take a single parameter, an int that indicates whether the coin is currently heads (0) or tails (1). There is no need to error check the input. The constructor should initialize the coin instance variable to...
Design and implement a class called circle_location to keep track of the position of a single...
Design and implement a class called circle_location to keep track of the position of a single point that travels around a circle. An object of this class records the position of the point as an angle, measured in a clockwise direction from the top of the circle. Include these public member functions: • A default constructor to place the point at the top of the circle. • Another constructor to place the point at a specified position. • A function...
*****Using Java 1.         There is a class called Wages. It should have one method: calculateWages. This...
*****Using Java 1.         There is a class called Wages. It should have one method: calculateWages. This method accepts from a user (1) an integer hourly rate and (2) an integer total number of hours worked in a week. It calculates and displays the total weekly wage of an employee. The company pays straight time for the first 40 hours worked by an employee and times and a half for all the hours worked in excess of 40. This class should...
ROCK, PAPER, SCISSORS Using C++, you will implement a class called Tool. It should have an...
ROCK, PAPER, SCISSORS Using C++, you will implement a class called Tool. It should have an int field called strength and a char field called type. You may make them either private or protected. The Tool class should also contain the function void setStrength(int), which sets the strength for the Tool. Create 3 more classes called Rock, Paper, and Scissors, which inherit from Tool. Each of these classes will need a default constructor that sets the strength to 1 and...
Implement Java source code for a class called Temperature that does the following: a. Prompts the...
Implement Java source code for a class called Temperature that does the following: a. Prompts the user to input a temperature b. Prints “Too Hot!” when the input temperature is above 75 c. Prints “Too Cold!” when the input temperature is below 40 d. Prints “Just Right!” when the input temperature is 40 to 75 e. Be precise, include comments, prologue, etc. if needed.
(In java language) Write an abstract class called House. The class should have type (mobile, multi-level,...
(In java language) Write an abstract class called House. The class should have type (mobile, multi-level, cottage, etc.) and size. Provide the following methods: A no-arg/default constructor. A constructor that accepts parameters. A constructor that accepts the type of the house and sets the size to 100. All other required methods. An abstract method for calculating heating cost. Come up with another abstract method of your own. Then write 2 subclasses, one for mobile house and one for cottage. Add...
1) Design an implement a program that created and exception class called StringTooLongException, designed to be...
1) Design an implement a program that created and exception class called StringTooLongException, designed to be thrown when a string is discovered that has too many characters in it. Create a driver that reads in strings from the user until the user enters DONE. If a string that has more then 5 characters is entered, throw the exception. Allow the thrown exception to terminate the program 2) Create a class called TestScore that has a constructor that accepts an array...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT