timurichk
timurichk

Reputation: 25

My class does not override abstract method compareTo

My class details attributes of restaurants downtown, said attributes being x/y locations and rank. The problem is, whenever I run the program It throws an error, saying that non-abstract class "Downtown" does not override abstract method "compareTo". I cannot make this class abstract because I need to initialise the object outside this block of code. Where does my program go wrong? Is there a problem with my compareTo implementation? Any suggestions will be much appreciated.

public class Downtown implements Comparable<Downtown> {//Throws error on this line
    private int x;
    private int y;
    private int rank;

    public Downtown(int x, int y, int rank) {
        this.x = x;
        this.y = y;
        this.rank = rank;
    }
    //Appropriate setters and getters for x , y and rank
    public int getX() {
         return x;
    }
    public void setX(int x) {
    this.x = x;
    }
    public int getY() {
    return y;
    }
    public void setY(int y) {
    this.y = y;
    }
    public int getRank() {
    return rank;
    }
    public void setRank(int rank) {
    this.rank = rank;
    }   

    public int compareTo(Downtown p1, Downtown p2)//Actual comparison
    {
        // This is so that the sort goes x first, y second and rank last

        // First by x- stop if this gives a result.
        int xResult = Integer.compare(p1.getX(),p1.getX());
        if (xResult != 0)
        {
            return xResult;
        }

        // Next by y
        int yResult = Integer.compare(p1.getY(),p2.getY());
        if (yResult != 0)
        {
            return yResult;
        }

        // Finally by rank
        return Integer.compare(p1.getRank(),p2.getRank());
    }

    @Override
    public String toString() {
        return "["+x+' '+y+' '+rank+' '+"]";
    }

Upvotes: 0

Views: 2541

Answers (2)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727097

Java's Comparable<T> interface defines compareTo method as follows:

int compareTo(T o);

This means that the method must take one parameter; the other parameter is the object itself, i.e. this. You need to implement this one-argument method in place of your two-argument method to fix this problem.

Compiler will help you figure out issues like this by using @Override annotation on your method:

@Override // Issues an error
public int compareTo(Downtown p1, Downtown p2)

@Override // Compiles fine
public int compareTo(Downtown other)

Upvotes: 2

Dawood ibn Kareem
Dawood ibn Kareem

Reputation: 79875

The compareTo method should compare the current object (this) to just one other. It shouldn't have two parameters for comparison. You could write your method like this.

public int compareTo(Downtown p2)//Actual comparison
{
    // This is so that the sort goes x first, y second and rank last

    // First by x- stop if this gives a result.
    int xResult = Integer.compare(getX(),p2.getX());
    if (xResult != 0)
    {
        return xResult;
    }

    // Next by y
    int yResult = Integer.compare(getY(),p2.getY());
    if (yResult != 0)
    {
        return yResult;
    }

    // Finally by rank
    return Integer.compare(getRank(),p2.getRank());
}

Notice how I've replace all the calls on p1 to calls on the current object.

Upvotes: 1

Related Questions