java compareTo method

Discussion in 'All other topics' started by rubixcube, Apr 29, 2009.

Thread Status:
Not open for further replies.
  1. rubixcube

    rubixcube Regular member

    Joined:
    Oct 31, 2004
    Messages:
    285
    Likes Received:
    0
    Trophy Points:
    26
    hey all,

    just have a question about the java programming compareTo method. basically, I have to write my own version of this method. I have a class called DR, which basically has a document name and a total count of how many words in that document.
    The method has to return -1 if x.totalcount < y.totalcount, 1 if x.totalcount > y.totalcount, and 0 if and only if x.totalcount == y.totalcount and x.equals(y). But if x.totalcount == y.totalcount but the objects arent equal, how can I return a value such that x.compareTo(y) < 0 and y.compareTo(x) > 0 ?
    here is what I have.

    public int compareTo(DR d){
    if (totalCount > d.totalCount)
    return -1;
    if (totalCount < d.totalCount)
    return 1;
    if (totalCount == d.totalCount)
    {if (!this.equals(d))
    return ????? }
    return 0;
    }

    Thanks!
     
    Last edited: Apr 29, 2009
Thread Status:
Not open for further replies.

Share This Page