jayrod
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?
class point implements Comparable{
int x
int y
}
in the compareTo method.. how would I specify less than and Greater than?
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
- ...
- 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;
}
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
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
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
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
jayrod, why didn't you split the points? I was the one talking about the "distance"...