Question

In: Computer Science

I've seen this question answered for C but I don't know how to translate to Java....

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:

  1. Basic pay rate: from user's choice
  2. Overtime (in excess of 40 hours) = time and a half (i.e., if the hours worked is 45, the hours for pay will be 47.5, which is 40 + 5 * 1.5).
  3. Tax rate:
    • 15% of the first $300;
    • 20% of the next $150;
    • 25% of the rest.
_____________________________
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
        }

}

Solutions

Expert Solution

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.


Related Solutions

I already answered the first three questions and put the answer down I just don't know...
I already answered the first three questions and put the answer down I just don't know how to answer the last question which asks to calculate K in the rate law? A clock reaction is run at 20 ºC with several different mixtures of iodide, sodium bromate and acid, to form iodine. Thiosulfate is used to react with the iodine formed initially. Starch indicator is added to form a blue color when all the thiosulfate has been used up and...
Hey, I'm stuck on this assignment for AP Comp Sci Java. I don't know how to...
Hey, I'm stuck on this assignment for AP Comp Sci Java. I don't know how to start. An array of String objects, words, has been properly declared and initialized. Each element of words contains a String consisting of lowercase letters (a–z). Write a code segment that uses an enhanced for loop to print all elements of words that end with "ing". As an example, if words contains {"ten", "fading", "post", "card", "thunder", "hinge", "trailing", "batting"}, then the following output should...
HI, I hope you are doing well. I really don't understand this question and don't know...
HI, I hope you are doing well. I really don't understand this question and don't know how to solve it at all because I am completely new to this c++ programming. can you please explain each line of code with long and clear comments? please think of me as someone who doesn't know to code at all. and I want this code to be written in c++ thank you very much and I will make sure to leave thumbs up....
please I don't really know how to start answering this question I really need to understand...
please I don't really know how to start answering this question I really need to understand it please show the work with a clear handwriting A collision in one dimension A mass m1 = 2 kg moving at v1i = 3 ms−1 collides with another mass m2 = 4 kg moving at v2i = −2 ms−1. After the collision the mass m1 moves at v1f = −3.66 ms−1. (a) Calculate the final velocity of the mass m2. (b) After the...
I know the what the answers are but I don't know how to get them. Can...
I know the what the answers are but I don't know how to get them. Can you please explain the process? Thank you. Part VII. Discontinued Operations and Earnings per Share (11 points) Todd Corporation had pre-tax income for 2017 of $2,500,000. On December 31, 2017, Boyd disposed of a component of its business that represented a strategic shift in operation. That component had a Loss on Discontinued Operations of $450,000 (pre-tax). Boyd received $1,000,000 net cash proceeds from the...
Can I get all of the question answered in a way that I know under which...
Can I get all of the question answered in a way that I know under which sub question the answer goes please. Can I get all of the questions answered in a way that I know how it fits in the question. Some parts are explanations. Thank you. Q. When a pink aqueous solution of potassium permanganate, faintly acidified with dilute sulfuric acid was treated with 10% aq. hydrogen peroxide, the reaction took place with the evolution of gas bubbles,...
I've answered the first half, but im stuck on the second half with multilanes and i...
I've answered the first half, but im stuck on the second half with multilanes and i need help with it . Highway Capacity and Level-of-Service Analysis Freeways A new segment of freeway is being built to connect two existing parallel freeway facilities, in an urban area. The following traffic and roadway characteristics are expected: Traffic Characteristics • AADT = 92000 veh/day • K = 13% • D = 50% • PHF = 0.92 • 8% trucks and buses • 3%...
I don't know how to build my last code for TestPairOfDice...below the question is in bold....
I don't know how to build my last code for TestPairOfDice...below the question is in bold. I'm including my code for the previous problems which are all needed for number 6 1. Implement a method named surface that accepts 3 integer parameters named width, length, and height as user input. It will return the total surface area (6 sides) of the rectangular box it represents. The formula for Surface Area is 2(length * width) + 2(length * height) + 2(height...
IMPORTANT: I know the answer is "C". However, I don't know why. Could you please explain...
IMPORTANT: I know the answer is "C". However, I don't know why. Could you please explain why? Thank you A linear total cost curve that passes through the origin implies that a.         average cost is constant and marginal cost is variable. b.         average cost is variable and marginal cost is constant.             c.         average and marginal costs are constant and equal.             d.         you need more information to answer question.
THIS IS A SPANISH QUESTION (I have seen Spanish questions answered here before so I'd figure...
THIS IS A SPANISH QUESTION (I have seen Spanish questions answered here before so I'd figure i would try it) Write two paragraphs of at least five sentences describing your favorite restaurant and your least favorite restaurant. Describe the food (breakfast, lunch, dinner, and drinks), prices and service. These restaurants can be of whatever you'd like, just as long as it fits all the requirements when its being described.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT