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!