Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can you explain why my compareTo method is not showing to be Overrided? I get er

ID: 3849633 • Letter: C

Question

Can you explain why my compareTo method is not showing to be Overrided? I get errors from my compliler that my class needs to be abstract because I am not correctly overriding the compareTo method.

What simple thing am I missing here to get compareTo to be overrided?

Here is my code:

public class Vehicle implements Comparable {

    private String make;
  
    public Vehicle(String inputMake) {
        make = inputMake;
    }
    @Override
    public String toString() {
        return "working on this method";
    }

        @Override
    public int compareTo(Vehicle vehicle) {
        int difference = make.compareToIgnoreCase(vehicle.getMake(vehicle));
        return difference;
    }

    public String getMake(Vehicle vehicle) {
        return make;
    }
}

Explanation / Answer

You need to implement Comparable<Vehicle> rather than Comparable for your class to compile and work.

If you are implementing Comparable, the expected method signature is compareTo(Object o)which is missing in your class and hence the error.

class Vehicle implements Comparable<Vehicle> {
private String make;
  
public Vehicle(String inputMake) {
make = inputMake;
}
@Override
public String toString() {
return "working on this method";
}
@Override
public int compareTo(Vehicle vehicle) {
int difference = make.compareToIgnoreCase(vehicle.getMake(vehicle));
return difference;
}
public String getMake(Vehicle vehicle) {
return make;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote