2

I'm working on an assignment for my computer science course requiring me to implement a Comparable interface.

We haven't discussed the interface at any sort of length except just being told it compares two objects and returns less than, greater than and equal to, but literally that's about it, which is frustrating.

I intend to do more research on it, but for now I am finding I'm confused as to why my implementation of the compareTo() method isn't working.

Eclipse is giving me an error that compareTo() must return an int, but if you notice, I am returning an integer value. So what might be the issue?

public int compareTo(Task taskToCompare) {
    if(this.complete && taskToCompare.isComplete()) {
        if(this.priority == taskToCompare.getPriority()) {
            return 0;
        }
        else if(this.priority < taskToCompare.getPriority()){
            return -1;
        }
        else if(this.priority > taskToCompare.getPriority()) {
            return 1;
        }
    } else if(this.complete == true && taskToCompare.isComplete() == false) {
        return -1;
    } else if(this.complete == false && taskToCompare.isComplete() == true) {
        return 1;
    }
}
2
  • what about when this.complete == false && taskToCompare.isComplete() == false
    – Nonika
    Mar 12, 2020 at 21:47
  • 1
    There is no return in the default case (if the if statements fall through).
    – NomadMaker
    Mar 12, 2020 at 22:16

3 Answers 3

9

If the return type is int, you will have to return an int or throw an exception. Just exiting the method without a return will lead to a compiler error.

If you have a if-else-if condition, there may be a case where none of the blocks is called. You therefore should create an else statement with a return.

Also, the result of isComplete() and taskToCompare.getPriority() may change if you call the method multiple times. The compiler doesn't know if your logic prevents that.

For example, this is the case if complete is false and isComplete() also returns false. As before, the compiler doesn't know if your logic prevents that.

I think you want something like:

public int compareTo(Task taskToCompare) {
    if(this.complete && taskToCompare.isComplete()) {
        if(this.priority == taskToCompare.getPriority()) {
            return 0;
        }
        else if(this.priority < taskToCompare.getPriority()){
            return -1;
        }
        else{
            return 1;
        }
    } else if(this.complete == true && taskToCompare.isComplete() == false) {
        return -1;
    } else if(this.complete == false && taskToCompare.isComplete() == true) {
        return 1;
    }else{
        return 0;
    }
}
4
  • Oh ok I see. Thank you. Shouldn't this.complete && taskToCompare.isComplete() take care of whether both tasks are complete or not?
    – GainzNerd
    Mar 12, 2020 at 21:55
  • No, if one of the tasks are incomplete, it not take care of it. If no task is completed, neither the if nor the else-if block will be invoked. Also, the isComplete() method may return different results if it is e.g. modifiedby another Thread between it is executed in the if and the else if.
    – dan1st
    Mar 12, 2020 at 21:59
  • I tried to implement: (this.complete == true && taskToCompare.isComplete() == true) || (this.complete == false && taskToCompare.isComplete() == false) Will (this.complete == false && taskToCompare.isComplete() == false) not implement to true?
    – GainzNerd
    Mar 12, 2020 at 23:01
  • also, again, thank you for helping and your time. Much appreciated.
    – GainzNerd
    Mar 12, 2020 at 23:30
4

What if this.complete == false and taskToCompare.isComplete() == false?

The compiler is complaining because you haven't covered every case.

1
  • Got it. Thank you so much for your input and making it clear.
    – GainzNerd
    Mar 12, 2020 at 21:55
1

more compact version:

public int compareTo(Task taskToCompare) {
        int completeCompare = (this.complete == taskToCompare.complete) ? 0 : (this.complete  ? 1 : -1);
        if(completeCompare==0) {
            return  this.priority-taskToCompare.getPriority();
        }
        return completeCompare;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.