In: Computer Science
in java code
Modify your program as follows:
Remember: Use variables or named constants to store all numbers so that they can be changed later on if necessary. Do not repeat calculations unnecessarily! Test your program with different values for the variables to make sure it works in all scenarios!
CODE
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
//variable to store per hour
rate
double hour_rate = 19.75;
//variable to store overtime
rate
double overtime = 1.5;
//variable to store social security
tax
double social_security_tax;
//variable to store federal income
tax
double federal_income_tax;
//variable to store state income
tax
double state_income_tax;
//variable to store union
dues
double union_dues = 9.95;
//variable to store health
insurance
double health_insurance;
//variable to store working
hours
double working_hours;
//variable to store overtime
hours
double overtime_hours;
//variable to store number of
dependents
int dependents;
//variable to store gross pay
double gross_pay;
//variable to store net pay
double net_pay;
Scanner sc = new
Scanner(System.in);
//prompt for working hours
System.out.print("Enter number of
hours worked in the week: ");
//reading working hours
working_hours =
Integer.parseInt(sc.next());
//prompt for number of
dependents
System.out.print("Enter number of
dependents: ");
//reading number of
dependents
dependents =
Integer.parseInt(sc.next());
//if working hours less than or
equals 40
if(working_hours <= 40)
{
//calculating
gross pay
gross_pay =
working_hours * hour_rate;
}
//if working hours greater than
40
else
{
//calculating
overtime hours
overtime_hours =
working_hours - 40;
//assigns
working hours as 40
working_hours =
40;
//calculating
gross pay
gross_pay =
(working_hours * hour_rate) + (overtime_hours * hour_rate *
overtime);
}
//calculating social security
tax
social_security_tax = 0.07 *
gross_pay;
//calculating federal income
tax
federal_income_tax = 0.13 *
gross_pay;
//calculating state income
tax
state_income_tax = 0.06 *
gross_pay;
//if number of dependents greater
than or equals 2
if(dependents >= 2)
{
//assigns health
insurance as 25
health_insurance
= 25;
}
//if number of dependents less than
2
else
{
//assigns health
insurance as 0
health_insurance
= 0;
}
//calculating net pay
net_pay = gross_pay -
social_security_tax - federal_income_tax - state_income_tax -
union_dues - health_insurance;
//printing gross pay
System.out.printf("Gross Pay:
%.2f\n", gross_pay);
//printing social security
tax
System.out.printf("Social Security
Tax: %.2f\n", social_security_tax);
//printing federal income tax
System.out.printf("Federal Income
Tax: %.2f\n", federal_income_tax);
//printing state income tax
System.out.printf("State Income
Tax: %.2f\n", state_income_tax);
//printing union dues
System.out.printf("Union Dues:
%.2f\n", union_dues);
//printing health insurance
System.out.printf("Health
Insurance: %.2f\n", health_insurance);
//printing net pay
System.out.printf("Net Pay:
%.2f\n", net_pay);
}
}
Summary-
For the user to enter the values and perform the program continuously without any breaks, an infinite while loop has been instigated to send a prompt to the user to continue or exit. If the user chose to continue the loop will be continued or it will be exited.
For the user input for the number of working hours per week, the hours shouldn't be negative and should be less than the total count of the hours in the week , which is 168 hours.
For the number of dependents also, the same is applicable.
Code-
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
// infinite loop until the user decides to quit
while(1)
{
System.out.println("Do u want to continue? enter 1 to continue enter 0 to exit")
int a=sc.nextInt();
if(a==0)
break;
else
{
//variable to store per hour
rate
double hour_rate = 19.75;
//variable to store overtime
rate
double overtime = 1.5;
//variable to store social security
tax
double social_security_tax;
//variable to store federal income
tax
double federal_income_tax;
//variable to store state income
tax
double state_income_tax;
//variable to store union
dues
double union_dues = 9.95;
//variable to store health
insurance
double health_insurance;
//variable to store working
hours
double working_hours;
//variable to store overtime
hours
double overtime_hours;
//variable to store number of
dependents
int dependents;
//variable to store gross pay
double gross_pay;
//variable to store net pay
double net_pay;
//prompt for working hours
System.out.print("Enter number of
hours worked in the week: ");
//reading working hours
working_hours =
Integer.parseInt(sc.next());
// validating the number of hours: the input should not be negative and not be greater than 168 hrs (24*7) per week logically
if(working_hours <0 || working_hours>168)
break;
else
continue;
//prompt for number of
dependents
System.out.print("Enter number of
dependents: ");
//reading number of
dependents
dependents =
Integer.parseInt(sc.next());
// in order to break the large numbers and the negative numbers.
// if(dependents<0 or dependents>50)
break;
//if working hours less than or
equals 40
if(working_hours <= 40)
{
//calculating
gross pay
gross_pay =
working_hours * hour_rate;
}
//if working hours greater than
40
else
{
//calculating
overtime hours
overtime_hours =
working_hours - 40;
//assigns
working hours as 40
working_hours =
40;
//calculating
gross pay
gross_pay =
(working_hours * hour_rate) + (overtime_hours * hour_rate *
overtime);
}
//calculating social security
tax
social_security_tax = 0.07 *
gross_pay;
//calculating federal income
tax
federal_income_tax = 0.13 *
gross_pay;
//calculating state income
tax
state_income_tax = 0.06 *
gross_pay;
//if number of dependents greater
than or equals 2
if(dependents >= 2)
{
//assigns health
insurance as 25
health_insurance
= 25;
}
//if number of dependents less than
2
else
{
//assigns health
insurance as 0
health_insurance
= 0;
}
//calculating net pay
net_pay = gross_pay -
social_security_tax - federal_income_tax - state_income_tax -
union_dues - health_insurance;
//printing gross pay
System.out.printf("Gross Pay:
%.2f\n", gross_pay);
//printing social security
tax
System.out.printf("Social Security
Tax: %.2f\n", social_security_tax);
//printing federal income tax
System.out.printf("Federal Income
Tax: %.2f\n", federal_income_tax);
//printing state income tax
System.out.printf("State Income
Tax: %.2f\n", state_income_tax);
//printing union dues
System.out.printf("Union Dues:
%.2f\n", union_dues);
//printing health insurance
System.out.printf("Health
Insurance: %.2f\n", health_insurance);
//printing net pay
System.out.printf("Net Pay:
%.2f\n", net_pay);
}
}
}