In: Computer Science
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:
Compute yearly salary - both the gross pay and net pay
Increase the salary by a certain percentage
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:
Federal Tax deductions – 9%
State Tax deductions – 2%
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
Compute pay check net pay for all pay periods (all 26 pay periods)
////////////////////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