Hourly workers are paid at their regular pay rate for their first 40 hours worked each week. Any hours beyond that are considered overtime, and they get paid "time-and-a-half" for those hours (1-1/2 times their regular pay rate).
For example, if a worker normally makes $12.00 per hour and has worked 45 hours in the past week, the pay is computed as follows: 40 times $12 for the "regular" hours is $480. The extra 5 hours is overtime and is paid at $18 per hour ($12 times 1.5 is $18). The "overtime pay" is 5 times $18, which is $90. Total pay would then be $480 + 90, which is $570.
Step1: Write a java program in Eclipse that will ask the user for a worker's pay rate and the number of hours worked this week. Print on the screen the number of overtime hours, how much regular pay the worker should receive, how much overtime pay, and the total pay for this week.
Step 2: Add comment statements from beginning to end.
Step3: Compile and test your program. Make sure it works properly and gives the correct answers for different inputs. TAKE SCREENSHOT OF 2 different values of OUTPUT
Your program should have
Here's a sample run of the program:
What is the pay rate? 9.50
How many hours worked this week? 42
You have worked 2 hours of overtime,
Regular pay is $380.00
Overtime pay is $28.50
Total pay is $408.50
Do you want to calculate for another set of hours? Press Y or N Y
What is the pay rate? 12.50
How many hours worked this week? 32
You have worked 0 hours of overtime
Regular pay is $400.00
Overtime pay is $0.00
Total pay is $400.00
Do you want to calculate for another set of hours? Press Y or N n
As per your requiremetnt, here is the Java code:
Rawcode:
//importing required packages
import java.util.*;
import java.math.RoundingMode;
import java.text.DecimalFormat;
//creating a class
public class HourlyPayRate {
//main function
public static void main(String[] args)
{
//declaring variables
double payRate;
int noOfHours;
char choice;
//creating a scanner class
Scanner input = new
Scanner(System.in);
//starting of so while loop this
will run till user enters y or Y
do
{
//asking user to
enter pay rate
System.out.printf("What is the pay rate? ");
//reading input
from user
payRate =
input.nextDouble();
//asking user to
enter number of hours he worked
System.out.printf("How many hours worked this week? ");
//reading input
from user
noOfHours =
input.nextInt();
//calling
function to calculate and print the regular pay, overtime pay and
total pay
calculateTotalPayrate(payRate, noOfHours);
//asking user to
enter a character of Y to continue or N to exit the program
System.out.printf("Do you want to calculate for another set of
hours? Press Y or N ");
//reading a
character from user
choice =
input.next().charAt(0);
}while(choice == 'y' || choice ==
'Y');
}
//function to calculate and print the regular pay,
overtime pay and total pay
public static void calculateTotalPayrate(double
payRate, double noOfHours) {
//declaring variables
double regularPay, overtimePay,
totalPay;
//checking if number of hours is
less than 40
if(noOfHours <= 40) {
//if it is less
than 40 then calculating regular pay that is payrate times of
number of hours
regularPay =
(double) noOfHours * payRate;
//so there is no
overtime pay then set it to 0
overtimePay =
0.00;
}
//if number of hours is greater
than 40
else {
//then
calculating regular pay for first 40 hours
regularPay =
(double) (40 * payRate);
//and
calculating the overtime pay for remaining hours
overtimePay =
(double) ((noOfHours - 40) * (payRate * 1.5));
}
//calculating total pay by adding
regular pay and overtime pay
totalPay = regularPay +
overtimePay;
//creating decimal format object to
print upto 2 decimal places
DecimalFormat df = new
DecimalFormat("#.##");
//rounding the last of digit of
decimal point
df.setRoundingMode(RoundingMode.CEILING);
//printing regular pay
System.out.println("Regular pay is
$" + df.format(regularPay));
//printing overtime pay
System.out.println("Overtime pay is
$" + df.format(overtimePay));
//printing total pay
System.out.println("Total pay is $"
+ df.format(totalPay));
}
}
Here is the screenshot of source code:
Here is the screenshot of output:
Hope this helps you. Please feel free to ask if you still have any queries.
Do upvote. Thank you !
Get Answers For Free
Most questions answered within 1 hours.