I am trying to get my output to match the output shown in the image. I think my variable values are getting crossed and giving me the incorrect output. also if you have any tips on how I can better organize my code I'd greatly appreciate it! Thank you! import java.util.Scanner; public class Program4 { //*************************************************************** // // Method: main // // Description: The main method of the program // // Parameters: String array // // Returns: N/A // //************************************************************** //start of main() method public static void main(String[] args) { Scanner input = new Scanner(System.in); //get employee info System.out.print("Enter your ID number: "); int employeeID = input.nextInt(); System.out.print("Enter you Payrate: "); double hourlyPay = input.nextDouble(); System.out.print("Enter your hours worked: "); double hoursWorked = input.nextDouble(); //Calculate Regular and overtime pay double regularHoursPay; double overTimeHoursPay = 0; if (hoursWorked <= 40){ regularHoursPay = hoursWorked * hourlyPay; } else { overTimeHoursPay = hoursWorked - 40; regularHoursPay = overTimeHoursPay * (hourlyPay * 1.5); } //Calculate gross pay double grossPay; double overTimeHours = 0; if(hoursWorked <= 40) { grossPay = hoursWorked * hourlyPay; } else { overTimeHours = hoursWorked - 40; grossPay = hourlyPay * 40 + overTimeHours * hourlyPay * 1.5; } //Calculate deductions double incomeTax = 0; double parkingCharge = 35; double totalDeduction = (incomeTax + parkingCharge); if(grossPay > 750) { incomeTax = grossPay * .1275 ; } //Calculate net pay double netPay = grossPay - totalDeduction; //Display employee results System.out.println("Program 4 "); System.out.printf("ID Number: %d%n", employeeID); System.out.printf("Pay Rate: %.2f%n", hourlyPay); System.out.printf("Regular Hours: %.2f%n", hoursWorked); System.out.printf("Overtime Hours: %.2f%n", overTimeHours); System.out.printf("Total Hours: %.2f%n", overTimeHours + hoursWorked); System.out.printf("Regular Pay: %.2f%n", regularHoursPay); System.out.printf("Overtime Pay: %.2f%n", overTimeHoursPay); System.out.printf("Gross Pay: %.2f%n", grossPay); System.out.printf("Tax: %.2f%n", incomeTax); System.out.printf("Deductions: %.2f%n", totalDeduction); System.out.printf("Net Pay: %.2f%n", netPay); }
I am trying to get my output to match the output shown in the image. I think my variable values are getting crossed and giving me the incorrect output. also if you have any tips on how I can better organize my code I'd greatly appreciate it! Thank you!
import java.util.Scanner;
public class Program4 {
//***************************************************************
//
// Method: main
//
// Description: The main method of the program
//
// Parameters: String array
//
// Returns: N/A
//
//**************************************************************
//start of main() method
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//get employee info
System.out.print("Enter your ID number: ");
int employeeID = input.nextInt();
System.out.print("Enter you Payrate: ");
double hourlyPay = input.nextDouble();
System.out.print("Enter your hours worked: ");
double hoursWorked = input.nextDouble();
//Calculate Regular and overtime pay
double regularHoursPay;
double overTimeHoursPay = 0;
if (hoursWorked <= 40){
regularHoursPay = hoursWorked * hourlyPay;
}
else {
overTimeHoursPay = hoursWorked - 40;
regularHoursPay = overTimeHoursPay * (hourlyPay * 1.5);
}
//Calculate gross pay
double grossPay;
double overTimeHours = 0;
if(hoursWorked <= 40) {
grossPay = hoursWorked * hourlyPay;
}
else {
overTimeHours = hoursWorked - 40;
grossPay = hourlyPay * 40 + overTimeHours * hourlyPay * 1.5;
}
//Calculate deductions
double incomeTax = 0;
double parkingCharge = 35;
double totalDeduction = (incomeTax + parkingCharge);
if(grossPay > 750) {
incomeTax = grossPay * .1275 ;
}
//Calculate net pay
double netPay = grossPay - totalDeduction;
//Display employee results
System.out.println("Program 4 ");
System.out.printf("ID Number: %d%n", employeeID);
System.out.printf("Pay Rate: %.2f%n", hourlyPay);
System.out.printf("Regular Hours: %.2f%n", hoursWorked);
System.out.printf("Overtime Hours: %.2f%n", overTimeHours);
System.out.printf("Total Hours: %.2f%n", overTimeHours + hoursWorked);
System.out.printf("Regular Pay: %.2f%n", regularHoursPay);
System.out.printf("Overtime Pay: %.2f%n", overTimeHoursPay);
System.out.printf("Gross Pay: %.2f%n", grossPay);
System.out.printf("Tax: %.2f%n", incomeTax);
System.out.printf("Deductions: %.2f%n", totalDeduction);
System.out.printf("Net Pay: %.2f%n", netPay);
}


Step by step
Solved in 5 steps with 3 images









