Solved

comparable interface

Posted on 2004-10-29
225 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
Question by:jayrod
    10 Comments
     
    LVL 37

    Expert Comment

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

    Expert Comment

    by:zzynx
    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
    What exactly do you want to achieve?
    0
     
    LVL 13

    Expert Comment

    by:petmagdy
    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
    offcourse u understand the silly mispelling mistakes in the code i wrote
    0
     
    LVL 3

    Author Comment

    by:jayrod
    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
    hmm.. the distance from 0,0 would be interesting.. don't remember the distance formula though :P
    0
     
    LVL 37

    Expert Comment

    by:zzynx
    >> 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:
    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
    jayrod, why didn't you split the points? I was the one talking about the "distance"...
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT Security CISA, CISSP & CISM Certification

    Master the advanced techniques required to protect network resources from external threats with the IT Cyber Security bundle. Built around industry best-practice guidelines, the IT Cyber Security bundle consists of three in-depth courses.

    Suggested Solutions

    1. Package the applet into a JAR file. The applet must be in a JAR file before a certificate can be attached to it. Use the jar JDK utility. If the applet was previously referenced with the help of a codebase attribute in  tag, replace the codebase …
    Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
    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 …
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    856 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now