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

Using NetBeans, create a program called PriorityQueueRunner that uses the Person

ID: 3689132 • Letter: U

Question

Using NetBeans, create a program called PriorityQueueRunner that uses the Person class below. Notice that this class implements Comparable<Person>.

public class Person implements Comparable<Person>
{
   private String firstName;
   private String lastName;

   public Person(String firstName, String lastName)
   {
      this.firstName = firstName;
      this.lastName = lastName;
   }

   public String getFirstName() { return firstName; }

   public String getLastName() { return lastName; }

   public String toString()
   {
      return "Name: " + firstName + " " + lastName;
   }
   public int compareTo(Person p)
   {
      String pname1 = firstName + lastName;
      String pname2 = p.firstName + p.lastName;
      return pname1.compareTo(pname2);
   }
}

Use this class to create Person objects for each of the names below, and add them to a PriorityQueue<Person> object. Empty the queue by polling it and printing the Person objects that are returned. Are Person objects returned in sequence?

Sam Smith
Charlie Black
Betty Brown
Jessica Stewart
John Friday
Frank Foley

Explanation / Answer

PriorityQueueTest.java


import java.util.PriorityQueue;

public class PriorityQueueTest {

   /**
   * @param args
   */
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       // create priority queue
       PriorityQueue<Person> priorityQueue = new PriorityQueue<Person>();
       // create persons object
       Person p1 = new Person("Sam", "Smith");
       Person p2 = new Person("Charlie", "Black");
       Person p3 = new Person("Betty", "Brown");
       Person p4 = new Person("Jessica", "Stewart");
       Person p5 = new Person("John", "Friday");
       Person p6 = new Person("Frank", "Foley");
       // insert persons object in the queue
       priorityQueue.add(p1);
       priorityQueue.add(p2);
       priorityQueue.add(p3);
       priorityQueue.add(p4);
       priorityQueue.add(p5);
       priorityQueue.add(p6);
      
       // get the head from the queue
       for(int i=0; i<6; i++){
           Person p = priorityQueue.poll();
           System.out.println ( p.getFirstName() +" "+p.getLastName());
       }
      
   }

}

Person.java


public class Person implements Comparable<Person>
{
private String firstName;
private String lastName;

public Person(String firstName, String lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}

public String getFirstName() { return firstName; }

public String getLastName() { return lastName; }

public String toString()
{
return "Name: " + firstName + " " + lastName;
}
public int compareTo(Person p)
{
String pname1 = firstName + lastName;
String pname2 = p.firstName + p.lastName;
return pname1.compareTo(pname2);
}
}

Output:

Betty Brown
Charlie Black
Frank Foley
Jessica Stewart
John Friday
Sam Smith

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