Write a Java program that calculates and prints the monthly pay check for an employee. The net pay is calculated after taking the following deductions:

Federal Income Tax: 15%
State Tax: 3.5%
Social Security Tax: 5.75%
Medicare/Medicaid Tax: 2.75%
Pension Plan: 5%
Health Insurance: $75.00

Your program should prompt the user to input the employee name and the gross amount. The out put will be stored in a file. Format your output to two decimal places. A sample output follows.

Allison Nields
Gross Amount: $3575.00
Federal Tax: $536.25
State Tax: $125.13
Social Security Tax: $205.56
Medicare/Medicaid Tax: $98.31
Pension Plan: $178.75
Health Insurance: $75.00
Net Pay: $2356.00

Respuesta :

Answer:

import java.io.FileWriter;

import java.util.Scanner;

public class PayCheck {

public static void main(String[] args) {

 // Create an object of the Scanner class to allow for user's input

 // via the  keyboard.

 Scanner input = new Scanner(System.in);

 // Prompt the user to enter employee's name

 System.out.println("Please enter employee's name");

 // Store the user's input in a String variable called name.

 String name = input.nextLine();

 // Prompt the user to enter the gross amount

 System.out.println("Please enter the gross amount (in dollars)");

 // Store the user's input in a double variable called amount.

 double amount = input.nextDouble();

 // Calculate the Federal Income Tax

 double FIT = 15 / 100.0 * amount;

 // Calculate the State Tax

 double ST = 3.5 / 100.0 * amount;

 // Calculate the Social Security Tax

 double SST = 5.75 / 100.0 * amount;

 // Calculate the Medicare/Medicaid Tax

 double MT = 2.75 / 100.0 * amount;

 // Calculate Pension Plan

 double PP = 5 / 100.0 * amount;

 // Calculate the Health Insurance;

 double HI = 75.00;

 // Calculate total pay to be deducted from actual pay

 double TOTAL = FIT + ST + SST + MT + PP + HI;

 // Calculate the net pay

 double NETPAY = amount - TOTAL;

 // Format the result using String.format() to print out the details of

 // the employee plus the amount paid in two decimal places

 // Store the result of the formatting in a String variable, paydetails.

 String paydetails = String.format("%s%.2f\n", "Gross Amount: $", amount);

 paydetails += String.format("%s%.2f\n", "Federal Tax: $", FIT);

 paydetails += String.format("%s%.2f\n", "State Tax: $", ST);

 paydetails += String.format("%s%.2f\n", "Social Security Tax: $", SST);

 paydetails += String.format("%s%.2f\n", "Medicare/Medicaid Tax: $", MT);

 paydetails += String.format("%s%.2f\n", "Pension Plan: $", PP);

 paydetails += String.format("%s%.2f\n", "Health Insurance: $", HI);

 paydetails += String.format("%s%.2f\n", "Net Pay: $", NETPAY);

 // Print out the result to the console

 System.out.println(paydetails);

 // You could also write the output to a file named document4.txt

 //  using the  FileWriter class.

 try {

  FileWriter fwriter = new FileWriter("document4.txt");

  fwriter.write(paydetails);

  System.out.println("Written to file");

  fwriter.close();

 } catch (Exception e) {

  e.printStackTrace();

 }

}

}

Explanation:

The source code file for this program has also been attached to this response. Please download the source code file for better readability. The code contains comments that explain every segment of the code. Kindly read through the comments line after line.

Hope this helps!

Ver imagen stigawithfun