Trouble returning the middle of the three numbers

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 2945 Views - Last Post: 10 March 2012 - 08:18 PM Rate Topic: -----

#1 ola3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 17-February 12

Trouble returning the middle of the three numbers

Posted 02 March 2012 - 07:42 AM

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;
  }
}

Is This A Good Question/Topic? 0
  • +

Replies To: Trouble returning the middle of the three numbers

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5517
  • View blog
  • Posts: 11,826
  • Joined: 02-June 10

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.
Was This Post Helpful? 0
  • +
  • -

#3 ola3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 17-February 12

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


View PosttlhIn`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.

Was This Post Helpful? 0
  • +
  • -

#4 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

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:

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;
}

Was This Post Helpful? 1
  • +
  • -

#5 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2720
  • View blog
  • Posts: 11,438
  • Joined: 20-September 08

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]
Was This Post Helpful? 1
  • +
  • -

#6 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

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 ;
}


Was This Post Helpful? 1
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5517
  • View blog
  • Posts: 11,826
  • Joined: 02-June 10

Re: Trouble returning the middle of the three numbers

Posted 02 March 2012 - 08:55 AM

View Postg00se, 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'
Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2720
  • View blog
  • Posts: 11,438
  • Joined: 20-September 08

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
Was This Post Helpful? 0
  • +
  • -

#9 Dorito_125  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 88
  • Joined: 13-December 08

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.

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?
Was This Post Helpful? 0
  • +
  • -

#10 ola3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 17-February 12

Re: Trouble returning the middle of the three numbers

Posted 02 March 2012 - 04:31 PM

could you explain this code?


View PostSwiftStriker00, on 02 March 2012 - 08:09 AM, said:

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 ;
}


Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

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;
  }
}


Was This Post Helpful? 0
  • +
  • -

#12 burakaltr  Icon User is offline

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 274
  • Joined: 07-November 10

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
Was This Post Helpful? -1
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

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
Was This Post Helpful? 0
  • +
  • -

#14 ola3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 17-February 12

Re: Trouble returning the middle of the three numbers

Posted 10 March 2012 - 06:39 PM

Could you explain this code in further detail please?


View PostSwiftStriker00, on 02 March 2012 - 08:09 AM, said:

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 ;
}


Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Trouble returning the middle of the three numbers

Posted 10 March 2012 - 06:48 PM

Same homework ?
All solved there

http://www.dreaminco...8&#entry1572438
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2