here is the challenge:

Given an array of ints of odd length, look at the first, last, and middle values in the array and return the largest. The array length will be a least 1.

maxTriple([1, 2, 3]) → 3

maxTriple([1, 5, 3]) → 5

maxTriple([5, 2, 3]) → 5

and my code, so far:

I've no idea where I'm going wrong. Maybe my if statements are a bit messy?

Where did you get your formula for middle from? That looks wrong.

It would also be wrong to use only one method; that code is begging to be subdivided in to multiple methods. They should probably be static, but coding bat doesn't allow static methods, so miss out the keyword static.

Bod MacNeil wrote: None of the arrays are < 3.

Assuming you're referring to length, that's

__not__what the wording of the "challenge" you posted says.

Winston

"Leadership is nature's way of removing morons from the productive flow" - Dogbert

Articles by Winston can be found here

Yeah the challenge says they could be of length 1, but after running it I noticed 3 was the smallest. Kind of cheating but if I can get this to work first I'll add a conditional statement to deal with arrays < 3 in length. cheers;

So the middle element of 1-length array is the only element (index 0).

The middle element of 3-length array is element with index 1.

The middle element of 5-length array is element with index 2.

The middle element of 7-length array is element with index 3.

Do you see a pattern?

A quiz question. What is the result of this expression in Java?

3/2

You need to split your task into two parts.

1. Determine values of first, middle and last element in the array correctly.

2. If and only if you have completed step 1. work on selecting which of those values is the biggest.

It is only important because you want to be unambiguous about which the middle element is. You can find a middle element in an even‑numbered array, but there are two potential middle elements. If you use the / 2 method, you simply have to take the left middle element.Paweł Baczyński wrote:The challenge says that the length of the array will always be odd. It is important. . . .

A couple of things:

1. The method name doesn't fit what the method does. Methods that start with "is" are usually boolean whereas this method returns an int. A good name should reflect its intent clearly and unambiguously. See this article: http://arlobelshee.com/good-naming-is-a-process-not-a-single-step/ for some great ideas about choosing good names.

2. Alternative implementation:

This code works exactly as yours except the case of numbers being equal is more explicitly defined in the code whereas your version it's implied. Not that it matters much in this case but in both versions, when there are two or more numbers that are the same, the latter number will be favored. So if first and middle are equal and both are greater than last, the middle number will be returned. If last is the greatest and is equal to first and/or middle, last will be returned.

You could also write that as:

*Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.*— Junilu

[How to Ask Questions] [How to Answer Questions]

Campbell Ritchie wrote:

return Math.max(Math.max(i, j), k);

Cheater.

*Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.*— Junilu

[How to Ask Questions] [How to Answer Questions]

Campbell Ritchie wrote:

return Math.max(Math.max(i, j), k);

I don't like the name isBigger, which should have boolean return type. Try largestOfThree.

I always wondered if there was a built in function to check more than 2 ints. Never thought of just using the Math.max inside a Math.max!

The first thing people pck up on in my code is the names of everything. I'm going to take some time to read the article Junilu posted! cheers.

Junilu Lacar wrote:You could also write that as:...

or indeed:However, yet another alternative is to

*sort*the three values - which requires the same number of checks and at most 2 "swaps" - but then makes the three "value results" (greatest, smallest, middle) trivial.

Winston

"Leadership is nature's way of removing morons from the productive flow" - Dogbert

Articles by Winston can be found here

`int`s (I do not know whether you can pass an int[], but you can use Arrays#stream() with an int[] instead). Both those techniques return an IntStream reference and in this instance you simply need to call its max() method. That doesn't return an

`int`as one might think, but an OptionalInt reference, to prevent problems if max() is called on a 0‑element stream.. There are various ways to get the value out of that optional; I shall try orElse which returns its argument (here I am using the most negative permissible value for an

`int`) if the optional is empty and there is no risk of an Exception.

Coding Bat didn't like the use of a Stream at all; it must be stuck in Java7 mode.

But why are they stuck on Java7?

If two numbers are equal, you can use either as the larger. I think that is implicit in all max functions.Ganesh Patekar wrote:. . . numbers equals to not considered. . . .]

Campbell Ritchie wrote:If two numbers are equal, you can use either as the larger...

I still say it's a lot of extra work for specifically

__three__values when a sort requires the same number of compares, and at most two swaps; and provides you with the answer for all three "value" methods in one fell swoop.

But I guess I'm just an old fart...

Winston

"Leadership is nature's way of removing morons from the productive flow" - Dogbert

Articles by Winston can be found here

Campbell Ritchie wrote:You can generalise that to any size of array...

Very true. I hadn't thought of that. I've actually been working on a generic 'Range'

`Iterable`as an exercise - kinda fun.

Winston

Articles by Winston can be found here