0

I am working on a class assignment and I don't quite understand how to use comparator in the way the assignment is asking.

The assignment reads:

"Complete the Priority Queue class

  1. Your Priority Queue must use an anonymous function to decide priority
  2. It must take the Function interface as a parameter to the constructor
  3. You should still have the default constructor – and if no function is provide use the compareTo function of the class"

This is the class that I am working on...

public class PriorityQueue <Item extends Comparable<Item>> {

    public PriorityQueue()
    {

    }
    public PriorityQueue(Comparator<Item> compare )
    {

    }  

    private int size = 0;
    private Node<Item> head = null;
    private Comparator<Item> compare ;

    private static class Node<Item>
    {
       private Item data;
       private Node<Item> next;


    public Node(Item data, Node<Item> next)
       {
          this.data = data;
          this.next = next;
       }       
       public Node(Item data)
       {
          this.data = data;
          this.next = null;
       }       
       public Node()
       {
          this.data = null;
          this.next = null;
       }
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public Item dequeue() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void enqueue(Item item) {
        Node<Item> curr = head;
        Node<Item> prev = curr;

        if (isEmpty())
        {
            head = new Node<Item>(item,null);
        }
        else
        {
            while (curr != null)
            {
                prev = curr;
                curr = curr.next;
            }

            prev.next = new Node<Item>(item, curr);
        }
        size++;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public void printQueue() {
        Node<Item> curr = head;

        while (curr != null)
        {
            System.out.println(curr.data);
            curr = curr.next;
        }

    }
}

This is the process class that the queue will contain...

public class Process implements Comparable<Process> {

    private ProcessPriorty priority;
        private String name;

    public Process(ProcessPriorty priority, String name) {
        super();
        this.priority = priority;
        this.name = name;
    }

    public void setPriority(ProcessPriorty priority) {
        this.priority = priority;
    }

    @Override
    public String toString() {
        return name + "... Priority = " + priority + ".";
    }

    public String getName() {
        return name;
    }

    public ProcessPriorty getPriority() {
        return priority;
    }

    @Override
    public int compareTo(Process other) {

        if(other == null)
        {
            return  1;
        }
        return this.priority.compareTo(other.priority) ;
    }
}

I understand the concept of a queue and have even coded the enqueue method to work as a simple queue that inserts the items as they come in. The problem I am coming across is comparing the nodes within that method to sort the list by priority at insertion. Which I believe relates back to those three directions of the assignment. So, what am I suppose to do with the constructors, the Comparator variable, and how do I make it default to compareTo?

  • *It must take the Function interface as a parameter to the constructor*: your class takes a Comparator. So it's not correct. Look at the Comparator javadoc. It has a method to create a Comparator using the natural ordering of elements to compare. And it also has one to create a Comparator by delegating to a Function transforming items to compare into comparables. – JB Nizet Mar 16 '19 at 17:54
  • The classes were structured by my professor. So essentially the class would look identical, except the methods would be blank. Then we have to try and piece together what he was planning. Are you saying his pre-written constructor parameter is not correct? –  Mar 16 '19 at 17:59
  • No, it's not correct, since it doesn't respect the instructions. And anonymous functions don't even exist in Java, BTW. Ask your teacher to clarify. But anyway, if Comparator is actually what the argument type should be, then you just need to use that comparator. What's the problem? Again, for the noarg constructor, look at the javadoc of Comparator: It has a method to create a Comparator using the natural ordering of elements to compare. – JB Nizet Mar 16 '19 at 18:02
  • @JBNizet I think the questions correct. It seems to be asking for lambdas i.e anonymous functions and `Comparator` is a functional interface. – Ryotsu Mar 16 '19 at 18:07
  • @Ryotsu the instructions say: "It must take the Function interface as a parameter to the constructor". Function interface != functional interface. Function != Comparator. And a lambda is called a lambda, not an anonymous function. The teacher probably made a mistake, and uses incorrect terminology. – JB Nizet Mar 16 '19 at 18:09
  • @JBNizet the 'function interface' is a typo in the question for 'functional interface' (there's nothing called a function interface )and anonymous function seems to loosely refer to a lambda in this case. – Ryotsu Mar 16 '19 at 18:15
  • 1
    @Ryotsu There actually is a `Function` in Java now, but I agree the instructions seem like they're in error and the `Comparator` is wanted instead. The OP should clarify with their instructor. `Function` : https://docs.oracle.com/javase/10/docs/api/java/util/function/Function.html – markspace Mar 16 '19 at 18:21
  • @markspace ah yes, It totally skipped by mind that `Function` is what might be referred to, but in w.r.t the questions context, I feel its a typo and yes better off clarifying. – Ryotsu Mar 16 '19 at 18:31

1 Answers1

0

Well since you have a reference to the head of the queue, its pretty simple from there. You have 2 cases -

  1. compare != null
  2. compare == null

    • In the first case, you are interested in Comparator::compareTo. From the definition of a priority queue, all you have to do is traverse the queue beginning from head, and as soon as the item in enqueue(Item item) is greater than the current element in the traversal, you insert item before element. You'll use compare.compareTo(item, element) to determine their order.

    • In the second case you'll simply use item.compareTo(element) to make the above stated comparison, the traversal and insertion will be the same.

Ryotsu
  • 786
  • 6
  • 16