Problem overview
You write two methods in the same class. maxOfTwo(int a, int b) is a helper: it returns the larger of two numbers. maxOfThree(int a, int b, int c) returns the largest of three numbers, and it must get its answer by calling the helper instead of comparing numbers itself. Built-in max methods are off limits.
With a = 3, b = 7, c = 5 the answer is 7. With a = -3, b = -8, c = -1 it is -1, because -1 is closest to zero. With a = 4, b = 4, c = 2 it is 4: when two numbers tie for the largest, either one is the answer because the value is the same.
Approach
Finding the larger of two numbers takes one comparison. Finding the largest of three can reuse that: take the larger of a and b, then take the larger of that result and c. The winner of the first round plays the third number in the second round.
That is the whole idea of a helper method. The comparison logic is written once, in maxOfTwo, and maxOfThree only combines calls. If you later needed the largest of four numbers, you would add one more call, not another pile of if statements.
Step by step
- Start inside
maxOfTwo. Ifa > b, returna. Areturnends the method on the spot, so whenais larger nothing after it runs. - Return
bbelow theif. The code only reaches this line whenais not greater thanb. Thenbis larger, or the two are equal and either is right. Noelseis needed, because the earlierreturnalready handled the other case. - Write
maxOfThreeas one nested call. A method calls another method of the same class by its name.maxOfTwo(a, b)gives the larger ofaandb, and passing that result as the first argument of anothermaxOfTwocall, withcas the second, gives the largest of all three. The inner call runs first.
Tracing the first example
For a = 3, b = 7, c = 5, Java first evaluates maxOfTwo(3, 7), which is 7. The outer call then becomes maxOfTwo(7, 5), which is also 7. The method returns 7.
The solution in Java
class Solution {
public int maxOfTwo(int a, int b) {
if (a > b) {
return a;
}
return b;
}
public int maxOfThree(int a, int b, int c) {
return maxOfTwo(maxOfTwo(a, b), c);
}
}
Details worth knowing:
- Parameter names are local. Both methods have parameters called
aandb, and they don't interfere. InsidemaxOfTwo,aandbare copies of the values the caller passed in, whatever the caller named them. The callmaxOfTwo(maxOfTwo(a, b), c)hands the helper two values, the inner result andc. - Primitive arguments are copied. Java passes an
intby value, so the helper can't change the caller's variables. - The return type must match the
return. Both methods are declaredint, and every path throughmaxOfTwoends in areturnof anint. If you wrote only theifwith itsreturn a;, the compiler would reject the method, because a path would reach the closing brace without returning a value. - No
statichere. The methods belong to aSolutionobject, so you call them on an instance, such asnew Solution().maxOfThree(3, 7, 5). Inside the class, they call each other by name alone.
Edge cases and common mistakes
- Negative numbers. Starting from a variable set to 0 and updating it would give 0 for
-3, -8, -1. The helper compares only the given numbers, so negatives work with no special case. - Ties. With
a = 4, b = 4,a > bis false, so the helper returnsb, which is 4. Using>=would give the same value. - Comparing inside
maxOfThree. The lesson asks the helper to do all the comparing. Adding aniftomaxOfThreedefeats the purpose of splitting the work. - Forgetting to use the result. Writing
maxOfTwo(a, b);on its own line computes a value and throws it away. The result has to be returned, stored, or passed on, as the nested call does.
Testing
Calling maxOfThree gives these results:
maxOfThree(3, 7, 5)returns 7.maxOfThree(-3, -8, -1)returns -1.maxOfThree(4, 4, 2)returns 4.
Calling the helper on its own, maxOfTwo(3, 7) and maxOfTwo(7, 5) both return 7, which are the two calls in the trace above.
