I\'m trying to write a method that calculates pay, where if someone works over 8
ID: 3740561 • Letter: I
Question
I'm trying to write a method that calculates pay, where if someone works over 8 hours, you get paid time and a half (1.5) and if someone works over 12 hours, your get paid double time (2). 2 problems: 1) My main method is not working with I call x and y to display. 2) It does not calculate the way I want it to. For the first test case, it should return $84 as in the comments. Please help and explain.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package getpay;
/**
*
* @author paolosantos
*/
public class GetPay {
public static void main(String[] args) {
System.out.println ("The amount of pay earned at an hourly rate of $" + " for " + " hours worked is " + calcPay(10.50, 8)); //should return $84
System.out.println ("The amount of pay earned at an hourly rate of $" + x + " for " + y + " hours worked is " + calcPay(10.50, 10)); // Should return $157.5
System.out.println ("The amount of pay earned at an hourly rate of $" + x + " for " + y + " hours worked is " + calcPay(10.50, 12)); //Should return $252
}
public static double calcPay(double x, double y){
double z = 0;
if ( x <= 0 || y <= 0)
throw new IllegalArgumentException("The parameters are invalid.");
else {
// x = pay rate
// y = number of hours worked
// z = amount earned
if (y < 8){
z = y * x;
}
else if ( 8 < y && y < 12) {
z = y * x * 1.5 ;
}
else if ( y > 12 ){
z = y * x * 2;
}
return z;
}
}
}
Explanation / Answer
Following is the correct java code: