Question

In: Computer Science

in java code Modify your program as follows: Ask the user for the number of hours...

in java code

Modify your program as follows:

  1. Ask the user for the number of hours worked in a week and the number of dependents as input, and then print out the same information as in the initial payroll assignment. Perform input validation to make sure the numbers entered by the user are reasonable (non-negative, not unusually large, etc).
  2. Let the calculations repeat for several employees until the user wishes to quit the program.

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);
   }
}

Solutions

Expert Solution

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);

}
   }
}


Related Solutions

in java Write a while loop to ask the user to type number of hours(double) they...
in java Write a while loop to ask the user to type number of hours(double) they work per day. Calculate and print the salary for each valid input. If number of hours is greater than or equal to 0 and less than 5, then:  salary = numberofhours * 5, loop continues, the user can type another number If number of hours is greater or equal to 5, and less than 10, then: salary = numberofours * 8, loop continues, the user...
PYTHON Modify the program in section Ask the user for a first name and a last...
PYTHON Modify the program in section Ask the user for a first name and a last name of several people.  Use a loop to ask for user input of each person’s first and last names  Each time through the loop, use a dictionary to store the first and last names of that person  Add that dictionary to a list to create a master list of the names  Example dictionary: aDict = { "fname":"Douglas", "name":"Lee" } ...
I need a java code Write a simple program to prompt the user for a number...
I need a java code Write a simple program to prompt the user for a number between 1 and 12 (inclusive) and print out the corresponding month. For example:   The 12th month is December. Use a java "switch" statement to convert from the number (1-12) to the month. Also use a "do while" loop and conditional checking to validate that the number entered is between 1 and 12 inclusive and if not, prompt the user again until getting the correct...
Code in Java Change the instructionLabel to ask the user to enter a numeric value into...
Code in Java Change the instructionLabel to ask the user to enter a numeric value into the textField and click the button to convert the entered value from kilometers to miles (1.609344 km = 1 mile). When the actionButton on the form is clicked, ActionWindow should take the value entered in the textField, convert it from kilometers to miles, and display the result in the resultField. import java.awt.Container; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class...
Modify the following java code, utilizing a loop mechanism to enable the user to use the...
Modify the following java code, utilizing a loop mechanism to enable the user to use the calculator more than once. The program does the following:    It prompts the user to enter 2 numbers.    It prompts the user to choose an operation to perform on those numbers:    Operation 1: Addition.    Operation 2: Subtraction.    Operation 3: Multiplication.    Operation 4: Division.    It outputs the result of the operation.    It asks the user if they want...
write a program i java that ask the user to enter salary, user ID and username...
write a program i java that ask the user to enter salary, user ID and username and out put them
In python Write the code to ask a user to enter a number in the range...
In python Write the code to ask a user to enter a number in the range of 1-100. Have the code checked to make sure that it is in this range. If it is not, let the user re-enter the number. The user should be able to re-enter numbers until the number is within the correct range.
1) Java code: A user wants to save for a big purchase. Ask them for the...
1) Java code: A user wants to save for a big purchase. Ask them for the amount they need to save, the current balance in their savings accounts, and the interest rate on the account. Calculate the balance of the account after the first, second, and third year, using the initial balance and annual interest rate. Using simple interest, the value of an account after one year is the balance multiplied by the interest rate plus the balance. Provide output...
Write a program will ask the user for the annual income and the number of years...
Write a program will ask the user for the annual income and the number of years that the user has worked as their current job. The program will tell the user whether or not he or she qualifies for a loan based on the following condition: income is equal to or exceeds $35,000 or number of years on job is greater than 5 Do not use “nested if statements” in your solution. As an example of the output: What is...
modify the code below to create a GUI program that accepts a String from the user...
modify the code below to create a GUI program that accepts a String from the user in a TextField and reports whether or not there are repeated characters in it. Thus your program is a client of the class which you created in question 1 above. N.B. most of the modification needs to occur in the constructor and the actionPerformed() methods. So you should spend time working out exactly what these two methods are doing, so that you can make...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT