[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

comparable interface

Posted on 2004-10-29
10
Medium Priority
?
230 Views
Last Modified: 2010-03-31
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?
0
Comment
Question by:jayrod
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 12445008
You could use the "distance" from the point (0,0)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12445044
It's all a matter of taste: you could use
- x+y
- x*y
- x^2 + y^2
- ...
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12445059
What exactly do you want to achieve?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 13

Expert Comment

by:petmagdy
ID: 12445068
like this

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

//tie
return 0;
 
 // I am less than compared
return -1;
}
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 12445102
offcourse u understand the silly mispelling mistakes in the code i wrote
0
 
LVL 3

Author Comment

by:jayrod
ID: 12445127
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
0
 
LVL 3

Author Comment

by:jayrod
ID: 12445148
hmm.. the distance from 0,0 would be interesting.. don't remember the distance formula though :P
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12445246
>> 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




0
 
LVL 4

Accepted Solution

by:
lcwiding earned 1000 total points
ID: 12445263
The distance formula coudl be used as follows:

public int compare(Point compared)
{
   long dist = x * x + y * y;
   long dist2 = compared.x * compared.x + compared.y * compared.y;

   if (dist == dist2)
      return 0;

   if (dist < dist2)
      return -1;
   else
      return 1;
}

The issue here is that, a you pointed out, 3,2 is not 2,3 but would come out equal in this situation. This does get to be tricky, as comparison in a situation like this is probably not going to equate to being the same.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12453256
jayrod, why didn't you split the points? I was the one talking about the "distance"...
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question