Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Using Comparable, extends Comparable

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    56
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Using Comparable, extends Comparable

    Hello okay so my assignment was to create a priority queue for Airline Passengers. Here is what I have done so far:

    //Driver
    package priorityqueuestandby;
     
    import java.util.NoSuchElementException;
    import javax.swing.JOptionPane;
     
    public class PriorityQueueStandBy {
     
        public static void main(String[] args) {
            {
     
                String firstName;
                String lastName;
                String middleInitial;
                String longevity;
                int number = 1;
                StandByPassenger standbypassengerReference;
                boolean done = false;
     
                PryorityQueueDLList priorityQueue0 = new PryorityQueueDLList();
     
                String[] choices = {"enqueue", "dequeue", "full?", "empty?", "quit"};
     
                while (!done) {
     
                    int choice = JOptionPane.showOptionDialog(
                            null,
                            "Click a choice",
                            "Queue Operations Menu",
                            JOptionPane.YES_NO_CANCEL_OPTION,
                            JOptionPane.QUESTION_MESSAGE,
                            null,
                            choices,
                            choices[0]);
                    try {
     
                        switch (choice) {
     
                            case 0:
     
                                firstName = JOptionPane.showInputDialog(
                                        "Enter your first name");
                                lastName = JOptionPane.showInputDialog(
                                        "Enter your last name");
                                middleInitial = JOptionPane.showInputDialog(
                                        "Enter your middle Initial");
                                longevity = JOptionPane.showInputDialog(
                                        "Enter the year, month and day you started"
                                                +"working for the airline ex 200515 = May 1, 2005");
     
                                int longevityInt = Integer.parseInt(longevity);
     
                                standbypassengerReference = new StandByPassenger(firstName, lastName, middleInitial, 
                                        longevityInt, number);
     
                                priorityQueue0.enqueue(standbypassengerReference);
                                number++;
                                break;
     
                            case 1:
     
                                if (priorityQueue0.isEmpty()) {
                                    JOptionPane.showMessageDialog(
                                            null,
                                            "Cannot dequeue; queue is empty!");
                                } else {
                                    standbypassengerReference = (StandByPassenger) priorityQueue0.dequeue();
                                    JOptionPane.showMessageDialog(
                                            null,
                                            "The next stand-by passenger in the queue was "
                                            + standbypassengerReference.getFirstName()
                                            + " "
                                            + standbypassengerReference.getMiddleInitial()
                                            + " "
                                            + standbypassengerReference.getLastName()
                                            + " "
                                            + "T"
                                            + standbypassengerReference.getNumber() 
                                            + " "
                                            + "The passenger's longevity is: "
                                            + standbypassengerReference.getLongevity() + ".");
                                }
                                break;
     
                            case 2:
     
                                    JOptionPane.showMessageDialog(
                                        null,
                                        "This queue will never be full!");
                                break;
     
                            case 3:
     
                                if (priorityQueue0.isEmpty()) {
                                    JOptionPane.showMessageDialog(
                                            null,
                                            "The queue is empty!");
                                } else {
                                    JOptionPane.showMessageDialog(
                                            null,
                                            "The queue is not empty!");
                                }
                                break;
     
                            case 4:
     
                                done = true;
                                break;
                            default:
                                JOptionPane.showMessageDialog(
                                        null,
                                        "Invalid selection; try again!");
                        } //end switch
                    } // end try
                    catch (NoSuchElementException e) {
     
                        JOptionPane.showMessageDialog(
                                null,
                                "The queue is Empty",
                                "",
                                JOptionPane.ERROR_MESSAGE);
     
                    } // end catch
                } // end while
            }
        }
    }

    //DLList class
    package priorityqueuestandby;
     
    public class PryorityQueueDLList<E>{
     
        PryorityQueueNode<E> head;    //Head node
        PryorityQueueNode<E> tail;    //Tail node
     
        int size;
     
        public PryorityQueueDLList(){
     
            head = new PryorityQueueNode<>(null, null, null);
     
            tail = new PryorityQueueNode<>(null, null, null);
     
            size = 0;
     
            //Set references
            head.setNext(tail);
            tail.setPrev(head);
     
        }
     
    //Return the next Node based on the refnode
        public PryorityQueueNode<E> getNext(PryorityQueueNode<E> referenceNode){
     
            return referenceNode.getNext();
        }
     
    //Return the previous Node based on the refnode
       public PryorityQueueNode<E> getPrev(PryorityQueueNode<E> referenceNode){
     
            return referenceNode.getPrev();
        }
     
    //Add an element before the refnode
        private void addBefore(PryorityQueueNode<E> referenceNode, E element){
     
            PryorityQueueNode<E> prevNode = this.getPrev(referenceNode);
     
            //Set reference
            PryorityQueueNode<E> newNode = new PryorityQueueNode<>(element, prevNode, referenceNode);
     
            referenceNode.setPrev(newNode);
            prevNode.setNext(newNode);
     
            size++;
        }
     
    //Add an element after the refnode
        private void addAfter(PryorityQueueNode<E> referenceNode, E element){
     
            PryorityQueueNode<E> nextNode = this.getNext(referenceNode);
     
            //Set reference
            PryorityQueueNode<E> newNode = new PryorityQueueNode<E>(element, referenceNode, nextNode);
     
            referenceNode.setNext(newNode);
            nextNode.setPrev(newNode);
     
            size++;
        }
     
    //Add an element at the front of the DLL
        public void addFirst(E element){
     
            this.addAfter(head, element);
        }
     
    //Add an element at the rear of the DLL
        public void enqueue(E element){
     
            this.addBefore(tail, element);
        }
     
    //Method to remove an element
        private E remove(PryorityQueueNode<E> removeNode){
     
            PryorityQueueNode<E> prevNode = this.getPrev(removeNode); //Node1
            PryorityQueueNode<E> nextNode = this.getNext(removeNode); //Node2
     
            //Dereference the node which is removed
            removeNode.setPrev(null);
            removeNode.setNext(null);
     
            //Set references
            prevNode.setNext(nextNode);
            nextNode.setPrev(prevNode);
     
            size--;
     
            //return element of removeNode
            return removeNode.getElement();
        }
     
    //Method to remove the last element of the DLL
        public E removeLast(){
     
            PryorityQueueNode<E> tempNode = this.getPrev(tail);
     
            return this.remove(tempNode);
        }
     
    //Method to remove the first element of the DLL
        public E dequeue(){
     
            PryorityQueueNode<E> tempNode = this.getNext(head);
     
            return this.remove(tempNode);
        }
     
        public boolean isEmpty(){
     
            return (this.size == 0);
        }
     
        public boolean isFull(){
     
            return false;
        }
     
        public int size(){
     
            return this.size;
        }
    }

    //DLList node class
    package priorityqueuestandby;
     
    public class PryorityQueueNode<E> {
     
        PryorityQueueNode<E> prevNode;
     
        PryorityQueueNode<E> nextNode;
     
        E element;
     
        public PryorityQueueNode(E element, PryorityQueueNode<E> prevNode, PryorityQueueNode<E> nextNode) {
     
            this.prevNode = prevNode;
     
            this.nextNode = nextNode;
     
            this.element = element;
        }
     
        public E getElement() {
     
            return this.element;
        }
     
        public void setElement(E newElement) {
     
            this.element = newElement;
        }
     
        public PryorityQueueNode<E> getPrev() {
     
            return prevNode;
        }
     
        public void setPrev(PryorityQueueNode<E> newPrev) {
     
            this.prevNode = newPrev;
        }
     
        public PryorityQueueNode<E> getNext(){
     
            return nextNode;
        }
     
        public void setNext(PryorityQueueNode<E> newNext){
     
            this.nextNode = newNext;
        }
    }

    //Passenger builder class
    package priorityqueuestandby;
     
    public class StandByPassenger {
     
       private String firstName; 
       private String lastName;
       private String middleInitial;
       private int longevity;
       int number;
     
       public StandByPassenger(String firstName, String lastName, String middleInitial, int longevity, int number){
     
           this.firstName = firstName;
           this.lastName = lastName;
           this.middleInitial = middleInitial;
           this.longevity = longevity;
           this.number = number;
       }
     
       public void setFirstName(String firstName){
     
           this.firstName = firstName;
       }
     
       public String getFirstName(){
     
           return firstName;
       }
     
       public void setLastName(String lastName){
     
           this.lastName = lastName;
        }
     
       public String getLastName(){
     
           return lastName;
       }
     
       public void setMiddleInitial(String middleInitial){
     
           this.middleInitial = middleInitial;
        }
     
       public String getMiddleInitial(){
     
           return middleInitial;
       }
     
       public void setLongevity(int longevity){
     
           this.longevity = longevity;
       }
     
       public int getLongevity(){
     
           return longevity;
       }
     
       public int getNumber(){
     
           return number;
       }
    }

    So the part that I cant figure out is:

    b. When a standby passenger is to be enqueued into the priority queue, it must be done so that at the moment of each dequeue operation, the item at the head of the queue is the standby passenger with the longest longevity, and also so that passengers with the same longevity are dequeued in a first-come-first-served fashion.

    he says that we need to "Make your program so that it extends Comparable and implements the compareTo() method properly..."

    So I was looking at the Comparable class and I could't find a compareTo() method... I am not confident I know how extends works either. Can someone please take a look at how my program is set up and give me an idea of where to start. I am assuming I need a new class if I am going to be extending another class. Right now I am taking in longevity as a String and converting it to an int because my last ditch effort is going to be to set up a loop that will organize longevity into a/an circular array based on the size of the incoming integer.


  2. #2
    Junior Member
    Join Date
    Oct 2014
    Posts
    12
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Using Comparable, extends Comparable

    Have a look at this link: CLICKY

    It explains comparable and compareTo() pretty well.

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    56
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Using Comparable, extends Comparable

    Sorry I am still confused. So here is what I think this might look like:

    In my main in Case 0 (enqueue) just after I pass the values for a/an airline passenger I thought I would write:

    StandByPassenger.compareTo(longevityInt);

    This will require me to make compareTo() a static method which is fine. Okay so say I do that well if I change my StandByPassenger class declaration to:

    public class StandByPassenger implements Comparator

    Now I get an error that says "error: StandByPassenger is not abstract and does not override abstract method compareTo(Object) in Comparable"

    I am not sure what this means exactly. Also why does my teacher want me to extend the Comparable class? Is the compareTo() method something I need to create?

    --- Update ---

    I think he means Comparable and write my class so that it extends Comparable however I am not sure why I need to extend Comparable.
    Last edited by vysero; October 10th, 2014 at 08:54 PM.

Similar Threads

  1. Understanding the Comparable Interface
    By sonicjr in forum Java Theory & Questions
    Replies: 9
    Last Post: July 3rd, 2012, 10:40 PM
  2. Comparable is implemented, but still " java.lang.Comparable" error appears
    By knowNothing23 in forum What's Wrong With My Code?
    Replies: 14
    Last Post: June 20th, 2012, 04:32 PM
  3. Using Comparable in Binary Search
    By Shaybay92 in forum Algorithms & Recursion
    Replies: 3
    Last Post: November 12th, 2011, 03:23 AM
  4. Help with Comparable interface.
    By novice564 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 19th, 2011, 08:39 AM
  5. Comparable Interface
    By yelrubk in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 28th, 2010, 09:08 AM