public class TestMedian{ public static void main(String[] args){ System.out.println(median (7,3,9)); System.out.println(median (29,-14,11)); } public static int median(int x, int y, int z){ int middle=0; int middle2=0; middle= Math.max(x,y); middle2=Math.max(middle,z); if (middle>z){ } return middle; } }

## 16 Replies - 11802 Views - Last Post: 10 March 2012 - 08:18 PM

### #1

# Trouble returning the middle of the three numbers

Posted 02 March 2012 - 07:42 AM

##
**Replies To:** Trouble returning the middle of the three numbers

### #2

## Re: Trouble returning the middle of the three numbers

Posted 02 March 2012 - 07:46 AM

Could you be even MORE vague, just to make it interesting?

Look, the harder you make it for us to help you the less likely someone will - That's just human nature and common sense.

How about telling us what the trouble is, what the problem is, ASKING A QUESTION can do wonders.

Look, the harder you make it for us to help you the less likely someone will - That's just human nature and common sense.

How about telling us what the trouble is, what the problem is, ASKING A QUESTION can do wonders.

### #3

## Re: Trouble returning the middle of the three numbers

Posted 02 March 2012 - 07:52 AM

The three numbers are passed into the method, what im having trouble with is returning the middle of the three numbers, for example lets say 5,59, 16 are passed in, the number 16 would be returned because its in the middle

tlhIn`toq, on 02 March 2012 - 07:46 AM, said:

Could you be even MORE vague, just to make it interesting?

Look, the harder you make it for us to help you the less likely someone will - That's just human nature and common sense.

How about telling us what the trouble is, what the problem is, ASKING A QUESTION can do wonders.

Look, the harder you make it for us to help you the less likely someone will - That's just human nature and common sense.

How about telling us what the trouble is, what the problem is, ASKING A QUESTION can do wonders.

### #4

## Re: Trouble returning the middle of the three numbers

Posted 02 March 2012 - 07:56 AM

His problem is that System.out.println(median (29,-14,11)); is returning 29.

The code you posted middle2 will always be your largest value, so you would need to convey simlilar logic to this:

The code you posted middle2 will always be your largest value, so you would need to convey simlilar logic to this:

int low, middle; if( x == Math.max(x,y) ){ middle = x; low = y; }else{ middle = y; low = x; } int high, trueMiddle if( z == Math.max(z,middle ) ){ high= z; trueMiddle= middle; }else{ high= middle; trueMiddle= z; }

### #5

## Re: Trouble returning the middle of the three numbers

Posted 02 March 2012 - 08:00 AM

Math.max is messy for anything other than two numbers. Do yourself a favour - use an array, sort it and return array[1]

### #6

## Re: Trouble returning the middle of the three numbers

Posted 02 March 2012 - 08:09 AM

or you can make your prof cry by trying to read this

public static int median(int x, int y, int z){ return ( x > y ) ? ( x > z) ? z : x : ( y > z ) ? z : y ; }

### #7

## Re: Trouble returning the middle of the three numbers

Posted 02 March 2012 - 08:55 AM

g00se, on 02 March 2012 - 09:00 AM, said:

Math.max is messy for anything other than two numbers. Do yourself a favour - use an array, sort it and return array[1]

Assuming it is always an array of three elements: 0, 1, 2 - thus [1] is the middle

But a more dynamic approach would be to take the count of the elements (array.Length in this language

*I think*), and divide that in half to get the element closest to the middle.

If you have 27 elements, half of that (as an int to drop the fraction) would be 13 or 14 depending on your systems choice of rounding up or down. Either one could be considered 'the middle'

### #8

## Re: Trouble returning the middle of the three numbers

Posted 02 March 2012 - 09:41 AM

Quote

Assuming it is always an array of three elements: 0, 1, 2 - thus [1] is the middle

Well the method signature given for the assignment would have to be changed to accommodate anything other than that. But the general point is correct - a general solution is usually better

### #9

## Re: Trouble returning the middle of the three numbers

Posted 02 March 2012 - 09:55 AM

If I were you I would do away with Math.max and just write it yourself.

Start in Psuedocode.

We know the median is the number that is in between the other two values? SO think to yourself procedurally how you would find that.

We did this exact same thing in school. If you don't mind me asking what school do you go to? What book do you use?

Shot in the dark, Java Programming from the ground up, Methods Ch.6 Problem 12?

Start in Psuedocode.

We know the median is the number that is in between the other two values? SO think to yourself procedurally how you would find that.

If(x <= y && y <= z) // We know Y is in the middle?

We did this exact same thing in school. If you don't mind me asking what school do you go to? What book do you use?

Shot in the dark, Java Programming from the ground up, Methods Ch.6 Problem 12?

### #10

## Re: Trouble returning the middle of the three numbers

Posted 02 March 2012 - 04:31 PM

### #11

## Re: Trouble returning the middle of the three numbers

Posted 02 March 2012 - 04:37 PM

public static int median(int x, int y, int z){ int middle=0; int middle2=0; middle= Math.max(x,y); middle2=Math.max(middle,z); if (middle>z){ // you do nothing here... probably return z; ? } return middle; } }

### #12

## Re: Trouble returning the middle of the three numbers

Posted 02 March 2012 - 07:20 PM

if (x<=y && x>=z) x is in the middle

if (y<=x && y>=z) y is in the middle

if (z<=y && z>=x) z is in the middle

if (x>=y && x<=z) x is in the middle

if (y>=x && y<=z) y is in the middle

if (z>=y && z<=x) z is in the middle

if (y<=x && y>=z) y is in the middle

if (z<=y && z>=x) z is in the middle

if (x>=y && x<=z) x is in the middle

if (y>=x && y<=z) y is in the middle

if (z>=y && z<=x) z is in the middle

### #13

## Re: Trouble returning the middle of the three numbers

Posted 02 March 2012 - 07:45 PM

Too trivial. I guess we want a solution that will be also good for N panels

### #14

## Re: Trouble returning the middle of the three numbers

Posted 10 March 2012 - 06:39 PM

### #15

## Re: Trouble returning the middle of the three numbers

Posted 10 March 2012 - 06:48 PM