Link to home
Start Free TrialLog in
Avatar of jayrod
jayrodFlag for United States of America

asked on

comparable interface

I'm implementing a simple point class

class point implements Comparable{

int x
int y

}


in the compareTo method.. how would I specify less than and Greater than?
Avatar of zzynx
zzynx
Flag of Belgium image

You could use the "distance" from the point (0,0)
It's all a matter of taste: you could use
- x+y
- x*y
- x^2 + y^2
- ...
What exactly do you want to achieve?
like this

public int compare( Point comapred )
{
 // I am begger than compared
return 1;

//tie
return 0;
 
 // I am less than compared
return -1;
}
offcourse u understand the silly mispelling mistakes in the code i wrote
Avatar of jayrod

ASKER

I guess consistincey..

because point 2,2 is equal to 2,2

but 3,2 is != 2,3  but in this case.. which is bigger? because 3+2 = 5 and 2+3 = 5 so that doesn't work
Avatar of jayrod

ASKER

hmm.. the distance from 0,0 would be interesting.. don't remember the distance formula though :P
>> don't remember the distance formula though
square root from ( x^2 + y^2 )
You'll always have points that will be "equal" (all points that lie on a circle with (0,0) as center will give equality)

>>because 3+2 = 5 and 2+3 = 5 so that doesn't work
Unless you add && pt1.x==pt2.x && pt1.y==pt2.y




ASKER CERTIFIED SOLUTION
Avatar of lcwiding
lcwiding

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
jayrod, why didn't you split the points? I was the one talking about the "distance"...