Write a program named PayStub (file name: PayStub.java) to compute and print an
ID: 3846992 • Letter: W
Question
Write a program named PayStub (file name: PayStub.java) to
compute and print an employee’s weekly gross pay, withholdings (deductions) and net pay. Your
program will prompt the user to enter the employee’s name, company name, title, pay period, pay
rate, and hours worked. After computing the gross pay and all the withholdings (deductions) your
program will print the gross pay, net pay, and all the deductions. The output of your program
should look similar to the following:
Jane Q. Public
Customer Service Specialist
ACME Corporation, Inc.
Pay period: 6/6/2017-6/12/2017
Pay rate: 12.00
Hours per week: 48.5
Gross Pay: 633.00
Regular Pay: 480.00
Overtime Pay: 153.00
Net Pay: 483.29
Total Deductions: 149.71
Federal: 63.30
State: 37.98
Social Security: 39.25
Medicare: 9.18
Implementation Details: All statements should be defined in the main method of the class (except
for declarations of constants). The following are the requirements:
Declare a String variable named employeeName to store the employee name and the prompt
should be “Enter Employee Name: ”. Use a variable named employeeTitle to store
the employee’s title and the prompt to get the title should be “Enter Employee Title:
”. The String variable companyName stores the name of the company and the prompt should
be: “Enter Company Name: ”. The prompt for the pay period is “Enter Pay Period:
” and the pay period is stored in a String variable named payPeriod.
A variable payRate should be declared to store the employee’s hourly wage rate (dollars per
hour) and the prompt to get the user input for the pay rate should be: “Enter Pay Rate:
”. Similarly, the prompt “Enter Hours Worked: ” should be used to get the hours per
week. A double variable hoursWorked should be used to stores the hours worked. The
fraction part of hours worked can only be in the increments of 15 minutes which are .0, .25, .5
and .75. If the user enters a fraction that not in the increments of 15 minutes then round it up
to the next 15 minutes. So, if the user enters hours work to be 51.39 then it should be rounded
up to 51.5. In such a case print a message on the screen: Hours worked rounded up
to 51.5.
Any hours worked over the regular 40 hours is considered overtime and the overtime hours are
paid at a rate of hour and a half of the employee’s regular pay rate. Define a named constant
as follows and use this named constant instead of hardcoded value of 1.5 (for hour and a half)
in computing overtime pay in your code:
final double OVERTIME_RATE = 1.5;
Define named constants for the federal tax rate percent, the state tax rate percent, the social
security tax rate percent, and the medicare tax rate percent with the following values:
FEDERAL_TAX_PERCENT: 15.0.
STATE_TAX_PERCENT: 6.0.
SS_PERCENT: 6.2.
MEDICARE_PERCENT: 1.45.
Declare variables of type double named federalTax, stateTax, ssTax, and
medicareTax to hold the computed values of withholding (deductions) for the federal, state,
social security, and medicare taxes.
Use Java’s Scanner class to get the keyboard from the user. All double value (as shown in in
a sample output) should be printed two places after decimal. You can use Java’s formatted
output statements such as printf along with escape sequences such as to format the
output. You may have to do some trial and error to properly format your programs output to
match the output shown above.
Test your program with various input values of pay rate and hours worked. Make sure that the
input prompts and the out has the proper format with exact text, spacing and indentation.
Make sure that your program compiles and does not generate run-time error. Save your Java
program file as PayStub.java
Explanation / Answer
public class PayStub
{
private String name;
private double hoursWorked;
private double hourlyPayRate;
private static final double FEDERAL_TAX_WITHHOLDING_RATE = 0.20;
private static final double STATE_TAX_WITHHOLDING_RATE = 0.09;
public PayStub()
{
}
public PayStub(String n, double hw, double hpr)
{
this.name = n;
this.hoursWorked = hw;
this.hourlyPayRate = hpr;
}
public double grossPay(double hourlyRate, double hrsWorked)
{
double grossPay = (hourlyRate * hrsWorked);
return grossPay;
}
public double federalTax(double gross)
{
return FEDERAL_TAX_WITHHOLDING_RATE * gross;
}
public double stateTax(double gross)
{
return STATE_TAX_WITHHOLDING_RATE * gross;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getHoursWorked() {
return hoursWorked;
}
public void setHoursWorked(double hoursWorked) {
this.hoursWorked = hoursWorked;
}
public double getHourlyPayRate() {
return hourlyPayRate;
}
public void setHourlyPayRate(double hourlyPayRate) {
this.hourlyPayRate = hourlyPayRate;
}
}
// @Copyright: Swaran Bindra
import java.util.*;
public class PayrollDriver
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
PayStub data = new PayStub();
System.out.println("Enter your name: ");
data.setName(input.next());
System.out.println("Number of Hours Worked: ");
data.setHoursWorked(Double.parseDouble(input.next()));
System.out.println("Hourly Pay Rate: ");
data.setHourlyPayRate(Double.parseDouble(input.next()));
double grossAmount = data.grossPay(data.getHourlyPayRate(), data.getHoursWorked());
System.out.println();
System.out.println("------------------------------------");
System.out.println("Name: " + data.getName());
System.out.println("Hours Worked: " + data.getHoursWorked() + "hrs");
System.out.println("Pay Rate: $" + data.getHourlyPayRate());
System.out.println("Gross Pay: $" + grossAmount + ' ');
System.out.println("DEDUCTIONS");
System.out.println("Federal Tax Withholding (20%): $" + data.federalTax(grossAmount));
System.out.println("State Tax Withholding (9%): $" + data.stateTax(grossAmount));
System.out.println("Total Deductions: $" + (data.federalTax(grossAmount) + data.stateTax(grossAmount)));
System.out.println("Net Pay: $" + (grossAmount - (data.federalTax(grossAmount) + data.stateTax(grossAmount))));
input.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.