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

I don\'t know how to make this comparable. Help Please! Event ADT: This ADT is s

ID: 3625641 • Letter: I

Question

I don't know how to make this comparable. Help Please!

Event ADT: This ADT is slightly different from the event type specified in the text, each event will have the same format, which is a slight departure from the text.

This ADT will have the following data members:

char eventType: This is one of ‘A’ for arrival or ‘D’ for departure, any other value is invalid.
int eventTime: time in “minutes” when the expected event is to occur.
Customer theCustomer: This is an object reference to the customer object for which the event is scheduled.

This ADT must implement the comparable interface. Two Event objects will be ordered by ascending event time.

You will need to create a constructor for this ADT that receives values for all 3 data members. There is no default constructor.

You will also need getter methods for each of the data members.
_____________________________________________________________________
public class Event implements java.lang.Comparable<> {

char eventType;
int eventTime;
Customer theCustomer;

public Event(char eventType, int eventTime, Customer theCustomer)
{
this.eventType = eventType;
this.eventTime = eventTime;
this.theCustomer = theCustomer;
}

public char compareTo(eventType)
{
return eventType;
}

public int getEventTime()
{
return eventTime;
}

public Customer getTheCustomer()
{
return theCustomer;
}

}

Explanation / Answer

Here's how you make it comparable.

The class declaration needs to look like this:

public class Event implements java.lang.Comparable<Event> {

   // You can implement compareTo like this.
   // The essential thing is that compareTo returns a negative integer if this event is less than e
   // a positive integer if this event is greater than e,
   // and 0 if this event is equivalent to e
   // (where e is the argument to the method)
   // Since we're ordering events by ascending event time,
   // "less than" means "earlier than".
    public int compareTo(Event e)
    {
        return this.eventTime - e.eventTime;
    }

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