5
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Shouldn't hours be a double as well? People don't always work an integer values of hours, especially when there's overtime involved. \$\endgroup\$ – Michael McGriff Oct 8 '15 at 15:37
  • \$\begingroup\$ In this case, the problem specifically asked that hours be an int. \$\endgroup\$ – Tony J Oct 8 '15 at 17:21
  • \$\begingroup\$ A course is having you do programming without teaching you if else statements first? I cannot think of a more elementary logical construct. \$\endgroup\$ – corsiKa Oct 8 '15 at 20:55
  • \$\begingroup\$ 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. \$\endgroup\$ – Tony J Oct 8 '15 at 21:11
5
\$\begingroup\$

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);
}
\$\endgroup\$
  • \$\begingroup\$ This is most likely how they wanted to do it. I was never thinking about max at the time. \$\endgroup\$ – Tony J Oct 8 '15 at 13:09
  • \$\begingroup\$ 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. \$\endgroup\$ – Gentian Kasa Oct 8 '15 at 13:17
  • 1
    \$\begingroup\$ 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 \$\endgroup\$ – rolfl Oct 8 '15 at 14:16
  • \$\begingroup\$ @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. \$\endgroup\$ – Gentian Kasa Oct 8 '15 at 14:30
  • \$\begingroup\$ @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. \$\endgroup\$ – Tony J Oct 8 '15 at 17:19
12
\$\begingroup\$

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));
}
\$\endgroup\$
  • \$\begingroup\$ Alright. I wonder how they were expecting us to do the problem. This is a fine solution anyway. Thanks \$\endgroup\$ – Tony J Oct 8 '15 at 12:59
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – Marc-Andre Oct 8 '15 at 15:17
  • 1
    \$\begingroup\$ @Marc-Andre, yeah, and I'd say 1.5 also (for the same reasons) \$\endgroup\$ – Gentian Kasa Oct 8 '15 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.