In: Computer Science
This program is written in Java and should be modularized in methods in one class.
This program will calculate the Gross Earnings, FICA tax (Medicare and Social Security taxes), Federal Tax Withheld, and Net Amount of the payroll check for each employee of a company. The output must contain numbers with 2 decimal places. The user input must be validated – if incorrect data is entered, send an error message to the user.
INPUT
The application must be able to collect the following “required” information for each employee:
OUTPUT
These are the outputs:
PROCESS
For each employee (HINT: Do while QUIT is not entered) calculate the following for their individual payroll for the week. Use an array to hold the gross pay amounts and print the grand payroll total once the user types QUIT.
Gross earnings: Hourly wage times hours worked (with time and a half after 40 hours).
FICA Tax: sum of 6.2% of the total wages (Social Security tax) and 1.45% of total wages (Medicare tax)
Federal Income Tax Withheld: Subtract $55.77 from the gross earnings for each withholding exemption giving the Adjusted Gross Income. Based on the Adjusted Gross Income, use the following parameters to determine the federal tax based on marital status:
Adjusted Gross Income |
Income Tax Withheld (S) |
Income Tax Withheld (M) |
$0 to $50.99 |
$0 |
$0 |
$51 to $500.99 |
10% over $51 |
5% over $51 |
$501 to $2,500.99 |
$45.00 + 15% of amount over $500.00 |
$22.50 + 10% of amount over $500.00 |
$2,501 to $5,000 |
$345.00 + 20% of amount over $2,500 |
$225.50 + 15% of amount over $2,500 |
Over $5,000 |
$845.00 + 25% of amount over $5,000 |
$600.50 + 20% of amount over $5,000 |
Net Earnings: Gross Earnings – FICA taxes – Federal Income Tax Withheld
Here is an example of the output format:
Paystub Calculator
-------------------------------------------------------
Employee Name:(type 'Quit' to end program)
Sam Smith
Hourly Wage: 10
Hours Worked: 40
Withholding Exemptions: 0
Marital Status (S = Single, M = Married): M
Paycheck for Tomie Gartland
Gross Income: $ 400.00
less Medicare Tax: $ 5.80
less Social Security Tax: $ 24.80
less FICA Tax: $ 30.60
less Federal Income Tax Withheld: $ 17.45
Net Income: $ 351.95
-----------------------------------------------------
Employee Name:(type 'Quit' to end program)
Sally Smith
Hourly Wage: 25
Hours Worked: 50
Withholding Exemptions: 2
Marital Status (S = Single, M = Married): M
Paycheck for Sally Smith
Gross Income: $ 1375.00
less Medicare Tax: $ 19.94
less Social Security Tax: $ 85.25
less FICA Tax: $ 105.19
less Federal Income Tax Withheld: $ 98.85
Net Income: $ 1170.97
-----------------------------------------------------
Employee Name:(type 'Quit' to end program)
Quit
Grand Paycheck Totals--------------------------------
-----------------------------------------------------
Total Gross Income: $ 1775.00
Total Medicare Tax: $ 25.74
Total Social Security Tax: $ 110.05
Total FICA Tax: $ 135.79
Total Income Tax Withheld: $ 116.30
Total Net Income: $ 1522.92
-----------------------------------------------------
-----------------------------------------------------
Explanation:
I have implemented all the methods in a single class as mentioned in the question. I have used various methods to calculate the gross earnings,tax,income, marital status,etc.I have used total variables to store the totals of all incomes,taxes,etc.I have also shown the output of the code,please find it attached with the answer.I have used DecimalFormat to format the output upto two decimal places.Please upvote if you liked my answer and comment of you need any modification or explanation
//code starts
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.util.Scanner;
public class GrossEarnings {
public double getGrossIncome(float hourlyWage,
float hoursWorked) {
double grossIncome = 0;
if (hoursWorked > 40) {
grossIncome +=
(hourlyWage * 1.5 * (hoursWorked - 40));
hoursWorked =
40;
}
grossIncome += hourlyWage *
hoursWorked;
return grossIncome;
}
public double getSocialSecurityTax(double
grossIncome) {
double socialSecurityTax = 0;
socialSecurityTax = (6.2 *
grossIncome) / 100;
return socialSecurityTax;
}
public double getMedicareTax(double grossIncome)
{
double medicalTax = 0;
medicalTax = (1.45 * grossIncome) /
100;
return medicalTax;
}
public double getFederalTax(double grossIncome, int
withHolding, char maritalStatus) {
double federalTax = 0;
double adjustGrossEarning =
grossIncome - withHolding * 55.77;
switch (maritalStatus) {
case 'M':
if
(adjustGrossEarning >= 0 && adjustGrossEarning <=
50.99)
return 0;
else if
(adjustGrossEarning >= 51 && adjustGrossEarning <=
500.99) {
federalTax += (5 * (adjustGrossEarning - 51)) /
100;
return federalTax;
} else if
(adjustGrossEarning >= 501 && adjustGrossEarning <=
2500.99) {
federalTax += 22.50 + (10 * (adjustGrossEarning
- 500)) / 100;
return federalTax;
} else if
(adjustGrossEarning >= 2501 && adjustGrossEarning <=
5000) {
federalTax += 225.50 + (15 * (adjustGrossEarning
- 2500)) / 100;
return federalTax;
} else if
(adjustGrossEarning > 5000) {
federalTax += 600.50 + (20 * (adjustGrossEarning
- 5000)) / 100;
return federalTax;
}
break;
case 'S':
if
(adjustGrossEarning >= 0 && adjustGrossEarning <=
50.99)
return 0;
else if
(adjustGrossEarning >= 51 && adjustGrossEarning <=
500.99) {
federalTax += (10 * (adjustGrossEarning - 51)) /
100;
return federalTax;
} else if
(adjustGrossEarning >= 501 && adjustGrossEarning <=
2500.99) {
federalTax += 45.00 + (15 * (adjustGrossEarning
- 500)) / 100;
return federalTax;
} else if
(adjustGrossEarning >= 2501 && adjustGrossEarning <=
5000) {
federalTax += 345.00 + (20 * (adjustGrossEarning
- 2500)) / 100;
return federalTax;
} else if
(adjustGrossEarning > 5000) {
federalTax += 845.00 + (25 * (adjustGrossEarning
- 5000)) / 100;
return federalTax;
}
default:
break;
}
return federalTax;
}
public static void main(String[] args) throws
IOException {
GrossEarnings grossEarnings = new
GrossEarnings();
DecimalFormat df = new
DecimalFormat("##.00");
double totalGrossIncome = 0;
double totalMedicalTax = 0;
double totalSocialSecurityTax =
0;
double totalIncomeTax = 0;
double totalNetIncome = 0;
Scanner scanner = new
Scanner(System.in);
BufferedReader bufferedReader = new
BufferedReader(new InputStreamReader(System.in));
System.out.println("Paystub
Calculator");
System.out.println("----------------------------------------");
while (true) {
System.out.println("Employee Name:(type 'Quit' to end
program)");
String name =
bufferedReader.readLine();// scanner.next();
if
(name.trim().compareTo("Quit") == 0)
break;
System.out.print("Hourly Wage:");
float hourlyWage
= Float.parseFloat(bufferedReader.readLine());
System.out.print("Hours Worked:");
float
hoursWorked = Float.parseFloat(bufferedReader.readLine());
System.out.print("Withholding Exemptions:");
int
withholdingExemptions =
Integer.parseInt(bufferedReader.readLine());
System.out.print("Marital Status (S = Single, M =
Married):");
char
maritalStatus = bufferedReader.readLine().charAt(0);
double
grossIncome = grossEarnings.getGrossIncome(hourlyWage,
hoursWorked);
System.out.println("Gross Income:\t\t\t $" +
df.format(grossIncome));
double
medicalTax = grossEarnings.getMedicareTax(grossIncome);
System.out.println("less Medicare Tax:\t\t $" +
df.format(medicalTax));
double
securityTax =
grossEarnings.getSocialSecurityTax(grossIncome);
System.out.println("less Social Security Tax:\t $" +
df.format(securityTax));
System.out.println("less FICA Tax:\t\t\t $" + df.format((medicalTax
+ securityTax)));
double
federalTax = grossEarnings.getFederalTax(grossIncome,
withholdingExemptions, maritalStatus);
System.out.println("less Federal Income Tax Withheld: $" +
df.format(federalTax));
System.out
.println("Net Income:\t\t\t
$" + df.format((grossIncome - medicalTax - securityTax -
federalTax)));
totalGrossIncome
+= grossIncome;
totalIncomeTax
+= federalTax;
totalMedicalTax
+= medicalTax;
totalSocialSecurityTax += securityTax;
totalNetIncome
+= grossIncome - medicalTax - securityTax - federalTax;
}
scanner.close();
System.out.println("Grand Paycheck
Totals--------------------------------");
System.out.println("Total Gross
Income: \t\t $" + df.format(totalGrossIncome));
System.out.println("Total Medicare
Tax: \t\t $" + df.format(totalMedicalTax));
System.out.println("Total Social
Security Tax: \t $" + df.format(totalSocialSecurityTax));
System.out.println("Total FICA Tax:
\t\t $" + df.format((totalMedicalTax +
totalSocialSecurityTax)));
System.out.println("Total Income
Tax Withheld: \t $" + df.format(totalIncomeTax));
System.out.println("Total Net
Income: \t\t $" + df.format(totalNetIncome));
}
}
Output: