In: Computer Science
I've seen this question answered for C but I don't know how to translate to Java.
Write a program that requests the hours worked in a week and the basic pay rate, and then prints the gross pay (before tax), the taxes, and the net pay (after tax).
Assume the following:
_____________________________ import java.util.Scanner; public class Pay { public static void main(String[] args) { //Constants final int NORMAL_WORK_HOURS = 40; final double OVERWORKED_TIMES = 1.5; final double TAX_RATE_1 = 0.15; final double TAX_RATE_2 = 0.20; final double TAX_RATE_3 = 0.25; final double TAX_BREAK_1 = 300; final double TAX_BREAK_2 = 450; //Declare variables Scanner sc = new Scanner(System.in); int userChoice = 0; double payRate = 0; double hoursWorked = 0;//The actual hours worked double hoursPay = 0; //The times for pay, i.e., one and a half for each hour overworked double grossPay = 0; double netPay = 0; double tax = 0; //Display the choices of basic tax rate //Accept user's choice of pay rate and set up the basic pay rate //Ask user for the hours worked //Calculate the hours for pay: //If the hours worked is less than or equals to 40, same as is //if it's greater than 40, hoursPay = 40 + (hoursWorked - 40) * 1.5 //Calculate the gross pay //Calculate the tax //Calculate the net pay //Display the result } }
Code
import java.util.Scanner;
public class Pay {
public static void main(String[] args) {
//Constants
final int NORMAL_WORK_HOURS = 40;
final double OVERWORKED_TIMES = 1.5;
final double TAX_RATE_1 = 0.15;
final double TAX_RATE_2 = 0.20;
final double TAX_RATE_3 = 0.25;
final double TAX_BREAK_1 = 300;
final double TAX_BREAK_2 = 450;
//Declare variables
Scanner sc = new Scanner(System.in);
int userChoice = 0;
double payRate = 0;
double hoursWorked = 0;//The actual hours worked
double hoursPay = 0; //The times for pay, i.e., one and a half for
each hour overworked
double grossPay = 0;
double netPay = 0;
double tax = 0;
//Accept user's choice of pay rate and set up the basic pay
rate
System.out.print("Enter your pay rate: ");
payRate=sc.nextDouble();
//Ask user for the hours worked
System.out.print("Enter weekly hours worked: ");
hoursWorked=sc.nextDouble();
//Calculate the hours for pay:
if(hoursWorked>40) //if it's greater than 40, hoursPay = 40 +
(hoursWorked - 40) * 1.5
hoursPay=NORMAL_WORK_HOURS+(hoursWorked-NORMAL_WORK_HOURS)*OVERWORKED_TIMES;
else //If the hours worked is less than or equals to 40, same as
is
hoursPay=hoursWorked;
grossPay=hoursPay*payRate;
//Calculate the tax
if(grossPay>TAX_BREAK_2)
{
tax=75+(grossPay-TAX_BREAK_2)*TAX_RATE_3;
}
else if(grossPay>TAX_BREAK_1)
{
tax=45+(grossPay-TAX_BREAK_1)*TAX_RATE_2;
}
else {
tax=grossPay*TAX_RATE_1;
}
//Calculate the net pay
netPay=grossPay-tax;
//Display the result
System.out.printf("Gross pay :$%,.2f%n",grossPay);
System.out.printf("Tax :$%,.2f%n",tax);
System.out.printf("Net pay :$%,.2f%n",netPay);
}
}
outputs
If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.