Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For this assignment, you will write two implementations of a Priority Queue. For this ADT, removal operations always return the object in the queue of

For this assignment, you will write two implementations of a Priority Queue. For this ADT, removal operations always return the object in the queue of highest priority that has been in the queue the longest. That is, no object of a given priority is ever removed as long as the queue contains one or more object of a higher priority. Within a given priority FIFO order must be preserved. Your implementations will be: 1. Ordered Array 2. Unordered Array Both implementations must have identical behavior, and must implement the PriorityQueue interface (provided). The implementations must have two constructors, a default constructor with no arguments that uses the DEFAULT_MAX_CAPACITY constant from the PriorityQueue interface, and a constructor that takes a single integer parameter that represents the maximum capacity of the priority queue. The PriorityQueue interface follows: /* The PriorityQueue ADT may store objects in any order. However, removal of objects from the PQ must follow specific criteria. The object of highest priority that has been in the PQ longest must be the object returned by the remove() method. FIFO return order must be preserved for objects of identical priority. Ranking of objects by priority is determined by the Comparable interface. All objects inserted into the PQ must implement this interface. */ package data_structures; import java.util.Iterator; public interface PriorityQueue> extends Iterable { public static final int DEFAULT_MAX_CAPACITY = 1000; // Inserts a new object into the priority queue. Returns true if // the insertion is successful. If the PQ is full, the insertion // is aborted, and the method returns false. public boolean insert(E object); // Removes the object of highest priority that has been in the // PQ the longest, and returns it. Returns null if the PQ is empty. public E remove(); // Deletes all instances of the parameter obj from the PQ if found, and // returns true. Returns false if no match to the parameter obj is found. public boolean delete(E obj); // Returns the object of highest priority that has been in the // PQ the longest, but does NOT remove it. // Returns null if the PQ is empty. public E peek(); // Returns true if the priority queue contains the specified element // false otherwise. public boolean contains(E obj); // Returns the number of objects currently in the PQ. public int size(); // Returns the PQ to an empty state. public void clear(); // Returns true if the PQ is empty, otherwise false public boolean isEmpty(); // Returns true if the PQ is full, otherwise false. List based // implementations should always return false. public boolean isFull(); // Returns an iterator of the objects in the PQ, in no particular // order. public Iterator iterator(); } Thus, your project will consist of the following files. You must use exactly these filenames. PriorityQueue.java The ADT interface (provided above) OrderedArrayPriorityQueue.java The ordered array implementation. UnorderedArrayPriorityQueue.java The unordered array implementation. Additional Details: Each method must be as efficient as possible. That is, a O(n) is unacceptable if the method could be written with O(log n) complexity. Accordingly, the ordered array implementation must use binary search where possible, such as the contains(), the delete(E obj) method and also to identify the correct insertion point for new additions. By convention, a lower number=higher priority. If there are five priorities for a given object, 1 .. 5, then 1 is the highest priority, and 5 the lowest priority. Your project must consists of only the three files specified (including the provided interface), no additional source code files are permitted. (Do not hand in a copy of PriorityQueue.java, as it is provided to you). You may not make any modifications to the PriorityQueue interface provided. I will grade your project with my copy of this file. All source code files must have your name and class account number at the beginning of the file. All of the above classes must be in a package named 'data_structures'. You may import java.util.Iterator, and java.util.NoSuchElementException only. If you feel that you need to import anything else, let me know. You are expected to write all of the code yourself, and you may not use the Java API for any containers. Your code must not print anything. Your code should never crash, but must handle any/all error conditions gracefully. i.e. if the user attempts to call the clear() method on an empty PQ, or remove an item from an empty PQ, the program should not crash. Be sure to follow the specifications for all methods. You must write generic code according to the interface provided. You may not add any public methods to the implementations, but you may add private ones, if needed. Your code may generate unchecked cast warnings when compiled, but it must compile and run correctly on edoras to receive any credit. Tester/driver programs will be provided going forward to help you test your code.

/* Driver.java

A simple driver to test program #1

*/

import data_structures.*;

public class Driver {

private int [] array;

private static final int SIZE = 100;

private PriorityQueue pq;

private PriorityQueue pq2;

public Driver() {

array = new int[SIZE];

// pq = new OrderedArrayPriorityQueue(SIZE);

pq = new UnorderedArrayPriorityQueue(SIZE);

//pq2 = new OrderedArrayPriorityQueue(SIZE);

pq2 = new UnorderedArrayPriorityQueue(SIZE);

initArray();

test1();

test2();

test3();

test4();

test5();

}

private void initArray() {

for(int i=0; i < SIZE; i++)

array[i] = i+1;

// now scramble array order

for(int i=0; i < SIZE; i++) {

int idx = (int) (SIZE*Math.random());

int tmp = array[i];

array[i] = array[idx];

array[idx] = tmp;

}

}

private void test1() {

pq.clear();

for(Integer i : pq)

throw new RuntimeException("Failed test #1, value returned in empty iterator");

for(int i=0; i < SIZE; i++)

if(!pq.insert(array[i]))

throw new RuntimeException("Failed test #1");

//////////////////////////////////////////////////////////////////////////////

// Comment this block for linked list based implementations

if(!pq.isFull())

throw new RuntimeException("Failed test #1, isFull reports false, but pq should be full");

//try to exceed the capacity

if(pq.insert(0))

throw new RuntimeException("Failed test1, exceeded capacity");

//////////////////////////////////////////////////////////////////////////////

System.out.println("Passed test #1, simple insert");

}

private void test2() {

for(int i=0; i < SIZE; i++){

int t = pq.remove();

//System.out.println(t);

if(t != (i+1))

throw new RuntimeException("Failed test #2, out of order removal");

}

if(pq.remove() != null)

throw new RuntimeException("Failed test #2, removal from empty pq did not return null");

if(!pq.isEmpty())

throw new RuntimeException("Failed test #2, isEmpty reports false, but pq is empty");

System.out.println("Passed test #2, simple removal");

}

private void test3() { // check FIFO behavior

int size=10;

pq2.clear();

int sequenceNumber = 0;

int midPoint = size >> 1;

for(int i=0; i < midPoint; i++)

pq2.insert(new PrioritizedItem(2,sequenceNumber++));

for(int i=midPoint; i < size; i++)

pq2.insert(new PrioritizedItem(1,sequenceNumber++));

PrioritizedItem item = pq2.peek();

if(item.getPriority() != 1 || item.getSequenceNumber() != 5)

throw new RuntimeException("Failed test #3, peek returns wrong element");

sequenceNumber = midPoint;

for(int i=0; i < midPoint; i++) {

PrioritizedItem tmp = pq2.remove();

if(tmp.getPriority() != 1)

throw new RuntimeException("Failed test #3, out of order removal");

if(tmp.getSequenceNumber() != (sequenceNumber++))

throw new RuntimeException("Failed test #3, out of order removal");

}

sequenceNumber = 0;

for(int i=midPoint; i < size; i++) {

PrioritizedItem tmp = pq2.remove();

if(tmp.getPriority() != 2)

throw new RuntimeException("Failed test #3, out of order removal");

if(tmp.getSequenceNumber() != (sequenceNumber++))

throw new RuntimeException("Failed test #3, out of order removal");

}

System.out.println("Passed test #3, FIFO check");

}

private void test4() {

pq2.clear();

int sequenceNumber = 0;

System.out.println(" Now checking iterators, output is below.");

System.out.println("NOTE: No specific order is required for these iterators.");

for(int i=0; i < 5; i++)

pq2.insert(new PrioritizedItem(10,sequenceNumber++));

for(int i=0; i < 5; i++)

pq2.insert(new PrioritizedItem(1,sequenceNumber++));

for(int i=0; i < 5; i++)

pq2.insert(new PrioritizedItem(5,sequenceNumber++));

for(PrioritizedItem item : pq2)

System.out.println(item);

System.out.println("Now removing elements with priority=5");

pq2.delete(new PrioritizedItem(5,100));

System.out.println(" Now removing items, they should be in proper order, "+

"with all priority=5 items removed.");

while(!pq2.isEmpty())

System.out.println(pq2.remove());

if(pq2.size() != 0)

System.out.println("Failed test #4, size is wrong.");

}

private void test5() {

final int TEST_5_SIZE = 1000;

pq = new OrderedArrayPriorityQueue(TEST_5_SIZE);

//pq = new UnorderedArrayPriorityQueue(TEST_5_SIZE);

for(int i=0; i

int someInteger = (int) (TEST_5_SIZE*Math.random());

if(!pq.insert(someInteger)) {

System.out.println("ERROR in test 5, insertion failed!");

System.exit(1);

}

}

int removed = pq.remove();

for(int i=1; i < TEST_5_SIZE; i++) {

int removed2 = pq.remove();

// System.out.println(removed2);

if(removed2 < removed) {

System.out.println("ERROR, out of order removal in test 5");

System.exit(1);

}

removed = removed2;

}

System.out.println("Passed test #5");

}

///////////////////////////////////////////////////////////////////////

private class PrioritizedItem implements Comparable{

private int priority;

private int itemNumber;

public PrioritizedItem(int p, int n) {

priority = p;

itemNumber = n;

}

public int compareTo(PrioritizedItem item) {

return priority - item.priority;

}

public String toString() {

return "Priority: " + priority + " Item Number: " + itemNumber;

}

public int getPriority() {

return priority;

}

public int getSequenceNumber() {

return itemNumber;

}

}

///////////////////////////////////////////////////////////////////////

public static void main(String [] args) {

new Driver();

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro SQL Server Administration

Authors: Peter Carter

1st Edition

1484207106, 9781484207109

More Books

Students also viewed these Databases questions