# Calculate someone's pay, including overtime

In this problem, you need to calculate someone's pay by multiplying their salary (double)by the hours (int) worked. If they worked for more than 8 hours, you pay them 1.5 times their salary for however many hours more they worked. Here is my solution.

public static double pay(double a, int b) {
if (b > 8) {
return (a * 8) + ((b - 8)*(a * 1.5));
}
else {
return (a * b);
}}


Wondering if there is any way to do it without if else statements since the course hasn't taught those yet.

• Shouldn't hours be a double as well? People don't always work an integer values of hours, especially when there's overtime involved. Oct 8 '15 at 15:37
• In this case, the problem specifically asked that hours be an int. Oct 8 '15 at 17:21
• A course is having you do programming without teaching you if else statements first? I cannot think of a more elementary logical construct. Oct 8 '15 at 20:55
• But this isn't some massive project. Just a simple lab that tests we know the Math class. I didn't really think about using the math class since overtime was conditional, so I did it this way. Oct 8 '15 at 21:11

In addition to the answer given by rolfl, you can also remove the if-else and do it like this:

public static double pay(double a, int b) {
int regularHours = Math.min(8, b);
int extraHours = b - regularHours;
return (a * regularHours) + (extraHours * a * 1.5);
}

• This is most likely how they wanted to do it. I was never thinking about max at the time. Oct 8 '15 at 13:09
• Probably. Just keep in mind that if you had to solve the problem without using any 3rd party methods (max is a method of Math which is included in the java.lang package and is written by someone else) but just the language, the method written by @rolfl is the way to go. Oct 8 '15 at 13:17
• Note that this gives the wrong answers..... ideone.com/fvAhZY - also, Math.max(...) is in fact, a conditional - in fact it is a few of them Oct 8 '15 at 14:16
• @rolfl, you're right (guess it happens when the code is not tested). Now the bug is fixed. Regarding Math.max (and also Math.min in this case) I'm well aware of the fact that they use conditionals internally, but in this context (it was the answer in a course where the students hadn't yet learned about the if-else construct) it was the most probable solution. Oct 8 '15 at 14:30
• @rolfl The Math class is a pretty big part of the course (atm), so the expectation was definitely going to be that we find a solution by using it. Oct 8 '15 at 17:19

You should use better variable names, for a start.... consider:

public static double pay(double hourlyWage, int hours) {
...
}


Then, your code is not that bad (with some indentation fixes too):

public static double pay(double hourlyWage, int hours) {
if (hours > 8) {
return (hourlyWage * 8) + ((hours - 8)*(hourlyWage * 1.5));
}
else {
return (hourlyWage * hours);
}
}


There is no good way to handle the overtime rate other than with a conditional somewhere. Ternary conditions may help a bit.....

public static double pay(double hourlyWage, int hours) {
int regular = hours > 8 ? 8 : hours;
int overtime = hours - regular;
return hourlyWage * (regular + (1.5 * overtime));
}

• Alright. I wonder how they were expecting us to do the problem. This is a fine solution anyway. Thanks Oct 8 '15 at 12:59
• I'm over thinking the problem but I would extract 8 as a magic numbers if it was in a real application. The maximum workable hours in the real world can change easily for a lot of reasons. Your solution is really elegant and I don't think it could be more readable than what it's currently is. Oct 8 '15 at 15:17
• @Marc-Andre, yeah, and I'd say 1.5 also (for the same reasons) Oct 8 '15 at 15:46