0

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+' '+"]";
    }
timurichk
  • 25
  • 5
  • 1
    `compareTo` only takes one parameter: `public int compareTo(Downtown p2)`. It's supposed to compare `this` with `p2`. – Andreas Dec 07 '17 at 04:15
  • Signature of compareTo() is wrong. Refer to https://dzone.com/articles/java-comparable-interface-in-five-minutes for details. – Santosh Dec 07 '17 at 04:15
  • Your `compareTo` function actually takes 3 `Downtown` objects: `p1`, `p2,` and `this`. This is not the correct signature for `compareTo`. – AJNeufeld Dec 07 '17 at 04:16
  • 1
    Tip: Use the `@Override` annotation on methods that should be overrides. You'll get a compiler error if the method signature is wrong. – D M Dec 07 '17 at 04:17
  • [Java : Comparable vs Comparator](https://stackoverflow.com/q/4108604), [What is the difference between compare() and compareTo()?](https://stackoverflow.com/q/420223) – Pshemo Dec 07 '17 at 04:19
  • as Pshemo pointed out if you want to compare value of two object then use Comparator interface. – deepaksingh pawar Dec 07 '17 at 04:34

2 Answers2

2

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)
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

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.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110