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

Priority Queue implementation. In this exercise you are asked to develop a reusa

ID: 3787727 • Letter: P

Question

Priority Queue implementation. In this exercise you are asked to develop a reusable implementation of the Abstract Data Type (ADT) Priority Queue (PQ). Your implementation must reuse existing implementations of ADT of the Java Collections Framework mentioned at http://docs.oracle.com/javase/tutoriaiycollections/implementations/index.html. We assume that PQ accepts any element having a property named priority Value that can be accessed as a public attribute value. This property represents the priority of an element. The element having the lowest priority will be removed whenever the PQ instance receives a remove() call. When more than one elements have the same priority, the PQ ADT allows to remove any of them after a removeO with no supplementary constraints. We assume also that the frequency of removal is about 1/10 of the frequency of adding elements, so your implementation may consider this to improve efficiency minimizing the overall cost of theses operations.

Explanation / Answer

Imlementation of PQ in java.

-----------

package chegg;

public class Person {
   private int id;
   private String name;
  
   public Person(int i, String n){
       this.id=i;
       this.name=n;
   }

   public int getId() {
       return id;
   }

   public String getName() {
       return name;
   }
}

----------------------

package chegg;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;

public class PQ {

   public static void main(String[] args) {
      
       //natural ordering example of priority queue
       Queue<Integer> integerPriorityQueue = new PriorityQueue<>(7);
       Random rand = new Random();
       for(int i=0;i<7;i++){
           integerPriorityQueue.add(new Integer(rand.nextInt(100)));
       }
       for(int i=0;i<7;i++){
           Integer in = integerPriorityQueue.poll();
           System.out.println("Processing Integer:"+in);
       }
      
       //PriorityQueue example with Comparator
       Queue<Person> customerPriorityQueue = new PriorityQueue<>(7, idComparator);
       addDataToQueue(customerPriorityQueue);
      
       pollDataFromQueue(customerPriorityQueue);
      
   }
  
   //Comparator anonymous class implementation
   public static Comparator<Person> idComparator = new Comparator<Person>(){
      
       @Override
       public int compare(Person c1, Person c2) {
return (int) (c1.getId() - c2.getId());
}
   };

   //utility method to add random data to Queue
   private static void addDataToQueue(Queue<Person> customerPriorityQueue) {
       Random rand = new Random();
       for(int i=0; i<7; i++){
           int id = rand.nextInt(100);
           customerPriorityQueue.add(new Person(id, "vks "+id));
       }
   }
  
   //utility method to poll data from queue
   private static void pollDataFromQueue(Queue<Person> customerPriorityQueue) {
       while(true){
           Person cust = customerPriorityQueue.poll();
           if(cust == null) break;
           System.out.println("Processing Person with ID="+cust.getId());
       }
   }

}

-------------------------------------------

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