Question

JAVA PROGRAMMING You are given an interface PriorityQueue (the source code is at the end of...

JAVA PROGRAMMING

You are given an interface PriorityQueue (the source code is at the end of this document) that specifies the protocols for a priority queue with generic values and priorities, where the priorities are discretely ordered rather than comparable. Implement the class ArrayListPQ implements PriorityQueue to fulfill the requirements of the interface (see the Javadoc in the interface’s source code). Use an ArrayList in order to fulfill the needs of the interface. For each constructor and method in your implementation, implement it in constant time if possible or else in no worse than linear time, and include a detailed comment analyzing the runtime in ?(… ) notation (order of magnitude or “big-Oh” notation). You are not expected to implement a main method. The class you implement is just an abstract data type, which you test as necessary using techniques discussed in lecture and lab. Do not change the PriorityQueue interface, because the instructors use the interface as given. Your implemented class is expected to comply with the given interface without any protocol conflicts.

Interface:

public interface PriorityQueue<V, P> {

/**

* Clears or initializes all encapsulated data structures

* which store any discrete priorities or enqueued values.

*

* Each constructor must call this method exactly once.

*/

void clear();

/**

* Appends a new lowest priority to the end of the sequence

* of discrete priorities (ordered from highest to lowest).

*

* This method must be called at least once after clearing

* the priority queue but before enqueueing any values.

*

* @param priority The priority to append

*/

void prioritize(P priority);

/**

* Adds a new value with the given priority

* after all other values with the same or higher priority

* and before all values with a lower priority.

*

* @param value The value to add

* @param priority The priority for that value

*/

void enqueue(V value, P priority);

/**

* Removes the oldest value of the

* highest priority available.

*

* @return The value to remove

*/

V dequeue();

/**

* Accesses the oldest value of the

* highest priority available.

*

* @return The value to access

*/

V peek();

/**

* Determines whether the priority queue has no values.

*

* @return Whether the priority queue is empty

*/

boolean isEmpty();

/**

* Represents the values in the priority queue

* in descending order of priority with a notation

* that includes both values and their priorities.

*

* @return The string representation

*/

String toString();

}

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

Program

filename:ArrayListPQ.java

import java.util.ArrayList;
import java.util.PriorityQueue;

public class ArrayListPQ {

private ArrayList<Object> lst; // arraylist to hold the elements

/**
* default constructor to create a new PriorityQueue
*/
public ArrayListPQ() {
lst = new ArrayList<Object>();
}

/**
* method to enqueue a new element to the PriorityQueue
*
* @param e
*/
public void enqueue(Object e) {
lst.add(lst.size(), e);
}

/**
* method to dequeue an existing element from the PriorityQueue
*
* @return the removed element
*/
public Object dequeue() {
int maxIndex = maxIndex();
Object maxObj = lst.get(maxIndex);
lst.remove(maxIndex);
return maxObj;
}

/**
* method to view the element at the top of the PriorityQueue
*
* @return the element at the top
*/
public Object peek() {
return lst.get(maxIndex());
}

/**
* method to return the maximum index
*
* @return the maximum index
*/
private int maxIndex() {
int max = 0;
for (int i = 0; i < lst.size(); i++) {
Comparable<Object> d = (Comparable<Object>) lst.get(i);
Object maxObj = lst.get(max);
if (d.compareTo(maxObj) > 0) {
max = i;
}
}

return max;

}

/**
* main driver method
*
* @param args
*/
public static void main(String[] args) {

PriorityQueue<String> Way = new PriorityQueue<String>();
Way.add("Alpha");
Way.add("Bravo");
Way.add("Charlie");

int count = 1;

while (!Way.isEmpty()) {
System.out.println("PQ " + count + "-->" + Way.remove());
count++;
System.out.println();
}

}
}

import java.util.ArrayList; import java.util.PriorityQueue; public class ArrayListPQ { private ArrayList<object> Ist; // arra

max i; 56 57 58 } } return max; } main driver method @param args public static void main(String[] args) { = new PriorityQueue

Output

PQ 1-->Alpha PQ 2-->Bravo PQ 3-->Charlie

==========================================================================================

Hope this helps!

Thank you!

I hope you're safe during the pandemic.

Add a comment
Know the answer?
Add Answer to:
JAVA PROGRAMMING You are given an interface PriorityQueue (the source code is at the end of...
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
  • JAVA PROGRAMMING You are given an interface PriorityQueue (the source code is at the end of...

    JAVA PROGRAMMING You are given an interface PriorityQueue (the source code is at the end of this document) that specifies the protocols for a priority queue with generic values and priorities, where the priorities are discretely ordered rather than comparable. Implement the class ListofQueuesPQ implements PriorityQueue to fulfill the requirements of the interface (see the Javadoc in the interface’s source code). Use an ListofQueues in order to fulfill the needs of the interface. You will need to make sub-classes within...

  • Can someone implement this using singly linked list. Please and thank you public interface Priori...

    Can someone implement this using singly linked list. Please and thank you public interface PriorityQueueInterface<T extends Comparable<T>> { /** * Adds a new entry to this priority queue * @param newEntry An object to be added. */ void add(T newEntry); /** Removes and returns the entry having the highest priority. * @return Either the object having the highest priority or, if the priority * queue is empty before the operation, null. */ T remove(); /** Retrieves the entry having the...

  • JAVA. Create an application that uses a "PriorityQueue" to perform the following Uses the constructor that rece...

    JAVA. Create an application that uses a "PriorityQueue" to perform the following Uses the constructor that receives a "Comparator" object as an argument Stores 5 "Time1" objects using the "Time1" class shown in Fig. 8.1 on page 331. The class must be modified to implement the "Comparator" interface Displays the "Universal Time" in priority order Note: To determine the ordering when implementing the "Comparator" interface, convert the time into seconds (i.e., hours * 3600 + minutes * 60 + seconds),...

  • Q2 [ 20 pts]. In computer science, a priority queue is an abstract data type which is like a regu...

    Data Structure Java code Q2 [ 20 pts]. In computer science, a priority queue is an abstract data type which is like a regular queue data structure, but where additionally each element has a "priority" associated with it. In a priority queue, an element with high priority is served before an element with low priority. If two elements have the same priority, they are served according to the order in which they were enqueued A typical priority queue supports following...

  • Please help me to solve this source code. Use the following file: InterfaceRunner.java import java.util.ArrayList; public...

    Please help me to solve this source code. Use the following file: InterfaceRunner.java import java.util.ArrayList; public class InterfaceRunner { public static void main(String[] args) { ArrayList<GeometricSolid> shapes = new ArrayList<>(); shapes.add(new Sphere(10)); shapes.add(new Sphere(1)); shapes.add(new Cylinder(1, 5)); shapes.add(new Cylinder(10, 20)); shapes.add(new RightCircularCone(1, 5)); shapes.add(new RightCircularCone(10, 20)); /* * Notice that the array list holds different kinds of objects * but each one is a GeometricSolid because it implements * the interface. * */ for (GeometricSolid shape : shapes) { System.out.printf("%.2f%n",shape.volume());...

  • JAVA Have not gotten the public Iterator<Item> iterator() . Can someone explain it please? import java.util.Iterator;...

    JAVA Have not gotten the public Iterator<Item> iterator() . Can someone explain it please? import java.util.Iterator; /* * GroupsQueue class supporting addition and removal of items * with respect to a given number of priorities and with * respect to the FIFO (first-in first-out) order for items * with the same priority. * * An example, where GroupsQueue would be useful is the airline * boarding process: every passenger gets assigned a priority, * usually a number, e.g., group 1,...

  • 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"...

  • Given the Interface Code write a java class that implements this interface and show the working...

    Given the Interface Code write a java class that implements this interface and show the working functionality in the main method: public interface CustomList<T> { /** * This method should add a new item into the <code>CustomList</code> and should * return <code>true</code> if it was successfully able to insert an item. * @param item the item to be added to the <code>CustomList</code> * @return <code>true</code> if item was successfully added, <code>false</code> if the item was not successfully added (note: it...

  • Java program. Code the following interfaces, implement them, and use the interface as a parameter, respectively:...

    Java program. Code the following interfaces, implement them, and use the interface as a parameter, respectively: a. An interface called Printable with a void print() method. b. An interface called EmployeeType with FACULTY and CLASSIFIED integer data. The value of FACULTY is 1 and CLASSIFIED is 2. a. A class called Employee to implement the two interfaces. Its constructor will initialize three instance data as name, employeeType, and salary. Implementation of method print() will display name, employeeType, and salary; salary...

  • Needed in C please Write the implementation file, priority_queue.c, for the interface in the given header file, priority_queue.h. Turn in your priority_queue.c file and a suitable main program, main.c...

    Needed in C please Write the implementation file, priority_queue.c, for the interface in the given header file, priority_queue.h. Turn in your priority_queue.c file and a suitable main program, main.c, that tests the opaque object. priority_queue.h is attached as a file to this assignment but is also listed here for your convenience. Your implementation file should implement the priority queue using a heap data structure. Submissions that implement the priority queue without using a heap will not receive any credit. #ifndef...

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