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

use java for the codes Create a program that uses a priority queue to collect us

ID: 3820620 • Letter: U

Question

use java for the codes

Create a program that uses a priority queue to collect user name and user age from a system user and then displays those values in ascending order (regardless of the order in which they are added).

import java.util.PriorityQueue;

public class MIKESQCOMPARATORMAIN

{

    public static void main(String[] args)

    {

        

        MyComparator comparator = new MyComparator();

        

        PriorityQueue<Employee> pQueue = new PriorityQueue<Employee>(7, comparator);

        pQueue.offer(new Employee("AAA", 15000));

        pQueue.offer(new Employee("BBB", 12000));

        pQueue.offer(new Employee("CCC", 7500));

        pQueue.offer(new Employee("DDD", 17500));

        pQueue.offer(new Employee("EEE", 21500));

        pQueue.offer(new Employee("FFF", 29000));

        pQueue.offer(new Employee("GGG", 14300));

        System.out.println(pQueue.poll());     

        System.out.println(pQueue.poll());      

        System.out.println(pQueue.poll());      

        System.out.println(pQueue.poll());      

        System.out.println(pQueue.poll());      

        System.out.println(pQueue.poll());      

        System.out.println(pQueue.poll());      

   

    }

}

Explanation / Answer

MIKESQCOMPARATORMAIN.java


import java.util.PriorityQueue;

public class MIKESQCOMPARATORMAIN
{
public static void main(String[] args)
{
  
MyComparator comparator = new MyComparator();

  
PriorityQueue<Employee> pQueue = new PriorityQueue<Employee>(7, comparator);

pQueue.offer(new Employee("AAA", 15000));
pQueue.offer(new Employee("BBB", 12000));
pQueue.offer(new Employee("CCC", 7500));
pQueue.offer(new Employee("DDD", 17500));
pQueue.offer(new Employee("EEE", 21500));
pQueue.offer(new Employee("FFF", 29000));
pQueue.offer(new Employee("GGG", 14300));

System.out.println(pQueue.poll());   
System.out.println(pQueue.poll());
System.out.println(pQueue.poll());
System.out.println(pQueue.poll());
System.out.println(pQueue.poll());
System.out.println(pQueue.poll());
System.out.println(pQueue.poll());


}
}

Employee.java


public class Employee {
   private String name;
   private int age;
   public Employee(String name, int age){
       this.name = name;
       this.age = age;
   }
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }
   public int getAge() {
       return age;
   }
   public void setAge(int age) {
       this.age = age;
   }
   public String toString() {
       return "Name: "+getName()+" Age: "+getAge();
      
   }
}

MyComparator.java


import java.util.Comparator;

public class MyComparator implements Comparator<Employee> {

   public int compare(Employee o1, Employee o2) {
       return o1.getName().compareTo(o2.getName());
   }

}

Output:

Name: AAA Age: 15000
Name: BBB Age: 12000
Name: CCC Age: 7500
Name: DDD Age: 17500
Name: EEE Age: 21500
Name: FFF Age: 29000
Name: GGG Age: 14300