Write a function calcPay which takes two double values as arguments. The first a
ID: 3751253 • Letter: W
Question
Write a function calcPay which takes two double values as arguments. The first argument should represent the number of hours worked and the second should represent an hourly pay rate.
The function returns the calculated total pay. Hours worked over 40 hours (overtime hours) should be calculated at 1.5x the pay rate (time and a half).
Your function MUST be named calcPay
Your function should take two parameters in this order:
a floating point parameter for hours worked (type double)
a floating point parameter for pay rate (type double)
Your function should return the total pay.
For example, if someone worked 41 hours and their pay rate is $10 their total pay is $415.
Regular Pay: (40 hours * $10) = $400
Overtime Pay: (1 hours * ($10 * 1.5)) = $15
Total Pay: = $415
Handling invalid input:
What happens if the user accidentally calls calcPay with a pay rate of -$10 and 41 hours worked? We could calculate their pay using the formula, but the result would be -$415, which doesn't make much sense! So, if one or both the parameters (pay rate and hours worked) are negative, the function should return -1.
Explanation / Answer
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Vamsi
*/
public class pay {
//method to calculated total pay
static double calcPay(double hours,double payRate)
{
if(hours<0 || payRate<0)//if any one of both are negative
return -1;//then returning -1
return hours*payRate;
}
//testing method
public static void main(String argv[])
{
double hours = 41,pay=10;
double overtime = 1;
double t1 = calcPay(hours,pay);
double t2 = calcPay(overtime,pay*1.5);//for over time pay is its 1.5 times
System.out.println("Regular pay:("+hours+" hours * $"+pay+") =$"+t1);
System.out.println("Overtime pay:("+hours+" hours * ($"+pay+" * 1.5) =$"+t2);
System.out.println("Total pay: =$"+(t1+t2));
}
}
output:
run:
Regular pay:(41.0 hours * $10.0) =$410.0
Overtime pay:(41.0 hours * ($10.0 * 1.5) =$15.0
Total pay: =$425.0
BUILD SUCCESSFUL (total time: 0 seconds)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.