I\'m trying to write a method that calculates pay, where if someone works over 8
ID: 3740552 • 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). It says bad operand types for line 29 ( else if ( 8 < y < 12) { ), but I don't know why my code isn't working so please include comments. Code is below. The return is the total amount of pay.
/*
* 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 $" + x + " for " + y + " hours worked is " + calcPay(10.50, 8));
System.out.println ("The amount of pay earned at an hourly rate of $" + x + " for " + y + " hours worked is " + calcPay(10.50, 10));
System.out.println ("The amount of pay earned at an hourly rate of $" + x + " for " + y + " hours worked is " + calcPay(10.50, 12));
}
public static double calcPay(double x, double y){
if ( x <= 0 || y <= 0)
throw new IllegalArgumentException("The parameters are invalid.");
else {
double z = 0;
x = 0; y = 0;
// x = pay rate
// y = number of hours worked
// z = amount earned
if (y < 8){
z = y * x;
}
else if ( 8 < y < 12) {
z = y * x * 1.5 ;
}
else if ( y > 12 ){
z = y * x * 2;
}
return z;
}
}
Explanation / Answer
Hi.......
There are two small mistake only.......and every thing is fine there....just replace this else if(8<y && y>12) in place of else if(8<y<12).......just remove x=0,y=0; in it will intilize to zero all the time whenever the method calls.....and method will return 0 only all time.....so,remove them before execute.....
Note : Here in your code you compared y at a time with two operands.....complier can't complie these two at time....so you got an error...bad operand types....
If any queries....revert back in comment box....