Question

Priority Queue Demo import java.util.*; public class PriorityQueueDemo {    public static void main(String[] args) {...

Priority Queue Demo


import java.util.*;

public class PriorityQueueDemo {
   public static void main(String[] args) {
       //TODO 1: Create a priority queue of Strings and assign it to variable queue1
      

       //TODO 2: Add Oklahoma, Indiana, Georgia, Texas to queue1
      
      

       System.out.println("Priority queue using Comparable:");
       //TODO 3: remove from queue1 all the strings one by one
       // with the "smaller" strings (higher priority) ahead of "bigger" strings.
       // print out each removed string and a space after the string.
      
      
      
       System.out.println();

       //TODO 4: Create a priority queue of Strings that will treat "bigger" as with higher priority.
       // Assign the queue to variable queue2.
      

       //TODO 5: Add Oklahoma, Indiana, Georgia, Texas to queue12

       System.out.println("\nPriority queue using Comparator:");
       //TODO 6: remove from queue2 all the strings one by one
       // with the "bigger" strings ahead of "smaller" strings.
       // print out each removed string and a space after the string.


       System.out.println();


       PriorityQueue<WorkOrder> queue3 = new PriorityQueue<>();
       queue3.offer(new WorkOrder(3, "Clean"));
       queue3.offer(new WorkOrder(2, "Build new structures"));
       queue3.offer(new WorkOrder(1, "Repair"));
       queue3.offer(new WorkOrder(4, "Preserve Energy"));


       System.out.println("\nWorkOrder Priority queue using Comparable:");
       while (queue3.size() > 0) {
           System.out.print(queue3.remove() + " ");
       }
       System.out.println();
   }
}

/**
This class encapsulates a work order with a priority.
*/
public class WorkOrder implements Comparable<WorkOrder>
{
   private int priority;
   private String description;

   /**
Constructs a work order with a given priority and description.
@param aPriority the priority of this work order
@param aDescription the description of this work order
   */
   public WorkOrder(int aPriority, String aDescription)
   {
       priority = aPriority;
       description = aDescription;
   }

   public String toString()
   {
       return "WorkOrder (" + priority + ", " + description + ")";
   }

   @Override
   public int compareTo(WorkOrder other) {
       // TODO: complete the method body.
       // Return a negative if this object's priority attribute is less than other's priority attribute
       // Return a positive if this object's priority attribute is greater than other's priority attribute
       // Return zero if this object's priority attribute is equal to other's priority attribute

       return 0;
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;

public class Main{
public static void main(String[] args) {
  
  

  
//TODO 1: Create a priority queue of Strings and assign it to variable queue1
PriorityQueue<String> queue1=new PriorityQueue<String>();
  
//TODO 2: Add Oklahoma, Indiana, Georgia, Texas to queue1
queue1.add("Oklahoma");
queue1.add("Indiana");
queue1.add("Georgia");
queue1.add("Texas");

  

System.out.println("Priority queue using Comparable:");
//TODO 3: remove from queue1 all the strings one by one
// with the "smaller" strings (higher priority) ahead of "bigger" strings.
// print out each removed string and a space after the string.
  
while (!queue1.isEmpty()) {
System.out.print(queue1.remove()+" ");
}
  
System.out.println();

Comparator<String> customComparator = new Comparator<String>() {
@Override
public int compare(String str1, String str2){
return str2.compareTo(str1);
}
};

//TODO 4: Create a priority queue of Strings that will treat "bigger" as with higher priority.
// Assign the queue to variable queue2.
PriorityQueue<String> queue2=new PriorityQueue<String>(customComparator);
  
//TODO 5: Add Oklahoma, Indiana, Georgia, Texas to queue12
queue2.add("Oklahoma");
queue2.add("Indiana");
queue2.add("Georgia");
queue2.add("Texas");


System.out.println("\nPriority queue using Comparator:");
//TODO 6: remove from queue2 all the strings one by one
// with the "bigger" strings ahead of "smaller" strings.
// print out each removed string and a space after the string.
while (!queue2.isEmpty()) {
System.out.print(queue2.remove()+" ");
}
  


System.out.println();


PriorityQueue<WorkOrder> queue3 = new PriorityQueue<>();
queue3.offer(new WorkOrder(3, "Clean"));
queue3.offer(new WorkOrder(2, "Build new structures"));
queue3.offer(new WorkOrder(1, "Repair"));
queue3.offer(new WorkOrder(4, "Preserve Energy"));


System.out.println("\nWorkOrder Priority queue using Comparable:");
while (queue3.size() > 0) {
System.out.print(queue3.remove() + " ");
}
System.out.println();
}
}

/**
This class encapsulates a work order with a priority.
*/
class WorkOrder implements Comparable<WorkOrder>
{
private int priority;
private String description;

/**
Constructs a work order with a given priority and description.
@param aPriority the priority of this work order
@param aDescription the description of this work order
*/
public WorkOrder(int aPriority, String aDescription)
{
priority = aPriority;
description = aDescription;
}

public String toString()
{
return "WorkOrder (" + priority + ", " + description + ")";
}

@Override
public int compareTo(WorkOrder other) {
// TODO: complete the method body.
// Return a negative if this object's priority attribute is less than other's priority attribute
// Return a positive if this object's priority attribute is greater than other's priority attribute
// Return zero if this object's priority attribute is equal to other's priority attribute
if(other.priority==priority){
return 0;
}
else if(priority<other.priority){
return -1;
}
else{
return 1;
}

}
}

Screenshot of code

//TODO 1: Create a priority queue of Strings and assign it to variable queue1 PriorityQueue<String> queue1=new PriorityQueueConstructs a work order with a given priority and description. @param aPriority the priority of this work order @param aDescr

Sample Output

Priority queue using Comparable: Georgia Indiana Oklahoma Texas Priority queue using Comparator: Texas Oklahoma Indiana Georg

Add a comment
Know the answer?
Add Answer to:
Priority Queue Demo import java.util.*; public class PriorityQueueDemo {    public static void main(String[] args) {...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        //...

    import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("Welcome to the Triangle Maker! Enter the size of the triangle.");        Scanner keyboard = new Scanner(System.in);    int size = keyboard.nextInt();    for (int i = 1; i <= size; i++)    {    for (int j = 0; j < i; j++)    {    System.out.print("*");    }    System.out.println();    }    for (int...

  • import java.util.Scanner; public class StudentClient {       public static void main(String[] args)    {   ...

    import java.util.Scanner; public class StudentClient {       public static void main(String[] args)    {        Student s1 = new Student();         Student s2 = new Student("Smith", "123-45-6789", 3.2);         Student s3 = new Student("Jones", "987-65-4321", 3.7);         System.out.println("The name of student #1 is ");         System.out.println("The social security number of student #1 is " + s1.toString());         System.out.println("Student #2 is " + s2);         System.out.println("the name of student #3 is " + s3.getName());         System.out.println("The social security number...

  • import java.util.ArrayList; import java.util.List; import java.util.Stack; public class Main { public static void main(String[] args) {...

    import java.util.ArrayList; import java.util.List; import java.util.Stack; public class Main { public static void main(String[] args) { int programCounter = 0; List<String> program = new ArrayList<String>(); Stack<String> stack = new Stack<String>(); // TODO string probably not best program.add("GOTO start<<1>>"); program.add("LABEL Read"); program.add("LINE -1"); program.add("FUNCTION Read -1 -1"); program.add("READ"); program.add("RETURN "); program.add("LABEL Write"); program.add("LINE -1"); program.add("FUNCTION Write -1 -1"); program.add("FORMAL dummyFormal 0"); program.add("LOAD 0 dummyFormal"); program.add("WRITE"); program.add("RETURN "); program.add("LABEL start<<1>>"); program.add("LINE 1"); program.add("FUNCTION main 1 4"); program.add("LIT 0 i"); program.add("LIT 0 j");...

  • Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args)...

    Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); final int maxSize = 128; String[] titles = new String[maxSize]; int[] lengths = new int[maxSize]; int numDVDs = 0; String op; op = menu(stdIn); System.out.println(); while (!op.equalsIgnoreCase("q")) { if (op.equalsIgnoreCase("a")) { if (numDVDs < maxSize) numDVDs = addDVD(titles, lengths, numDVDs, stdIn); } else if (op.equalsIgnoreCase("t")) searchByTitle(titles, lengths, numDVDs, stdIn);    else if (op.equalsIgnoreCase("l")) searchByLength(titles, lengths, numDVDs, stdIn); System.out.println('\n');...

  • //LinkedList import java.util.Scanner; public class PoD {    public static void main( String [] args )...

    //LinkedList import java.util.Scanner; public class PoD {    public static void main( String [] args ) { Scanner in = new Scanner( System.in ); LinkedList teamList = new LinkedList(); final int TEAM_SIZE = Integer.valueOf(in.nextLine()); for (int i=0; i<TEAM_SIZE; i++) { String newTeamMember = in.nextLine(); teamList.append(newTeamMember); } while (in.hasNext()) { String removeMember = in.nextLine(); teamList.remove(removeMember); }    System.out.println("FINAL TEAM:"); System.out.println(teamList); in.close(); System.out.print("END OF OUTPUT"); } } =========================================================================================== //PoD import java.util.NoSuchElementException; /** * A listnked list is a sequence of nodes with...

  • Revision Question Consider the following Java class: { public static void main (String [ ] args)...

    Revision Question Consider the following Java class: { public static void main (String [ ] args) { ArrayQueue<Integer> queue; queue = new ArrayQueue<Integer> () ; Integer x, y ; x = 3; y = 6; queue.offer (x) ; queue.offer (12) ; queue.offer (y) ; y = queue.peek () ; queue.poll () ; queue. offer (x - 2) ; queue.offer (x) ; queue.offer (y + 4) ; System.out.println ("Queue Elements: ") ; while (! queue.empty() ) System.out.print (queue.poll () + "...

  • Cant figure out how to fix error Code- import java.io.File; import java.io.IOException; import java.util.*; public class Program8 {    public static void main(String[] args)throws IOException{       ...

    Cant figure out how to fix error Code- import java.io.File; import java.io.IOException; import java.util.*; public class Program8 {    public static void main(String[] args)throws IOException{        File prg8 = new File("program8.txt");        Scanner reader = new Scanner(prg8);        String cName = "";        int cID = 0;        double bill = 0.0;        String email = "";        double nExempt = 0.0;        String tExempt = "";        int x = 0;        int j = 1;        while(reader.hasNextInt()) {            x = reader.nextInt();}        Customers c1 [] = new Customers [x];        for (int...

  • Java Here is the template public class ShiftNumbers { public static void main(String[] args) { // TODO:...

    Java Here is the template public class ShiftNumbers { public static void main(String[] args) { // TODO: Declare matrix shell size // TODO: Create first row // TODO: Generate remaining rows // TODO: Display matrix } /** * firstRow * * This will generate the first row of the matrix, given the size n. The * elements of this row will be the values from 1 to n * * @param size int Desired size of the array * @return...

  • import java.util.Random; import java.util.ArrayList; /** * */ public class hw5_task8 { public static void main(String[] args)...

    import java.util.Random; import java.util.ArrayList; /** * */ public class hw5_task8 { public static void main(String[] args) { int[] grades = randomIntArr(10); printIntArray("grades", grades); ArrayList<Integer> indexesF_AL = selectIndexes_1(grades); System.out.println(" indexesF_AL: " + indexesF_AL); int[] indexesF_Arr = selectIndexes_2(grades); printIntArray("indexesF_Arr",indexesF_Arr); } public static int[] randomIntArr(int N){ int[] res = new int[N]; Random r = new Random(0); for(int i = 0; i < res.length; i++){ res[i] = r.nextInt(101); // r.nextInt(101) returns an in in range [0, 100] } return res; } public static void...

  • import java.util.Scanner; public class Client{ public static void main(String args[]){    Coin quarter = new Coin(25);...

    import java.util.Scanner; public class Client{ public static void main(String args[]){    Coin quarter = new Coin(25); Coin dime = new Coin(10); Coin nickel = new Coin(5);    Scanner keyboard = new Scanner(System.in);    int i = 0; int total = 0;    while(true){    i++; System.out.println("Round " + i + ": "); quarter.toss(); System.out.println("Quarter is " + quarter.getSideUp()); if(quarter.getSideUp() == "HEADS") total = total + quarter.getValue();    dime.toss(); System.out.println("Dime is " + dime.getSideUp()); if(dime.getSideUp() == "HEADS") total = total +...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT