Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Class is not abstract and does not override abstract method compareTo(X) in Comparable

I’m trying to use the comparable interface to compare the appointment times (String) in the format of dd-mm-yyyy hh:mm
Also it seems that it can’t find the getAppointmentTime method, which is clearly there

In the tester class, I’m guessing I should just use Collection.sort and that’s it.

But these are the errors I’m getting…

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

HW3\Appointment.java:13: error: Appointment is not abstract and does not override abstract method compareTo(Appointment) in Comparable
public class Appointment implements Comparable<Appointment>
       ^
HW3\Appointment.java:189: error: name clash: compareTo(Object) in Appointment and compareTo(Appointment) in Comparable have the same erasure, yet neither overrides the other
    public int compareTo(Object obj)
               ^
HW3\Appointment.java:191: error: cannot find symbol
        return getAppointmentTime().compareTo(obj.getAppointmentTime());
                                                 ^
  symbol:   method getAppointmentTime()
  location: variable obj of type Object

Code:

package HW3;

import java.lang.Comparable;

public class Appointment implements Comparable<Appointment>
{

private String serviceType;
private int patientID;
private String patientName;
private String appointmentTime;
private String doctorName;
private String symptoms;

public Appointment(String serviceType, int patientID, String patientName, String appointmentTime, String doctorName, String symptoms)
{
this.serviceType = serviceType;
this.patientID = patientID;
this.patientName = patientName;
this.appointmentTime = appointmentTime;
this.doctorName = doctorName;
this.symptoms = symptoms;
}

public String getServiceType()
{
  return serviceType;
}


public int getPatientID()
{
  return patientID;
}


public String getPatientName()
{
  return patientName;
}


public String getAppointmentTime()
{
  return appointmentTime;
}


public String getDoctorName()
{
  return doctorName;
}

public String getSymptoms()
{
  return symptoms;
}

public int compareTo(Object obj)
{
    return getAppointmentTime().compareTo(obj.getAppointmentTime());
}

}

>Solution :

Try the below code:

package HW3;

import java.lang.Comparable;

public class Appointment implements Comparable<Appointment> {

private String serviceType;
private int patientID;
private String patientName;
private String appointmentTime;
private String doctorName;
private String symptoms;

public Appointment(String serviceType, int patientID, String patientName, String appointmentTime, String doctorName, String symptoms) {
    this.serviceType = serviceType;
    this.patientID = patientID;
    this.patientName = patientName;
    this.appointmentTime = appointmentTime;
    this.doctorName = doctorName;
    this.symptoms = symptoms;
}

public String getServiceType() {
    return serviceType;
}


public int getPatientID() {
    return patientID;
}


public String getPatientName() {
    return patientName;
}


public String getAppointmentTime() {
    return appointmentTime;
}


public String getDoctorName() {
    return doctorName;
}

public String getSymptoms() {
    return symptoms;
}

@Override
public int compareTo(Appointment obj) {
    return getAppointmentTime().compareTo(obj.getAppointmentTime());
 }
}

Changes:
Changed the method parameter of compareTo method from Object to Appointment

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading