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  3833 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
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
tlhIn`toq, on 02 March 2012  07:46 AM, said:
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
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
#6
Re: Trouble returning the middle of the three numbers
Posted 02 March 2012  08:09 AM
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:
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
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
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 (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
#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
