Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write the class ArrayPriorityQueue that extends Comparable and Implements PriorityQueueInterface THIS IS THE PriorityQueueInterface class that is given: package edu.iastate.cs228.hw05; /** An interface for the

Write the class ArrayPriorityQueue that extends Comparable and Implements PriorityQueueInterface

THIS IS THE PriorityQueueInterface class that is given:

package edu.iastate.cs228.hw05; /** An interface for the ADT priority queue. @author Frank M. Carrano @author Timothy M. Henry @version 4.0 */ public interface PriorityQueueInterface> { /** Adds a new entry to this priority queue. @param newEntry An object to be added. */ public 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. */ public T remove();

/** Retrieves the entry having the highest priority. @return Either the object having the highest priority or, if the priority queue is empty, null. */ public T peek();

/** Detects whether this priority queue is empty. @return True if the priority queue is empty, or false otherwise. */ public boolean isEmpty();

/** Gets the size of this priority queue. @return The number of entries currently in the priority queue. */ public int getSize();

/** Removes all entries from this priority queue. */ public void clear(); } // end PriorityQueueInterface

THIS IS THE ArrayPriorityQueue skeleton class that is given:

public class ArrayPriorityQueue>

implements PriorityQueueInterface

{

private T[] queue; // The contents of the priority queue

private int frontIndex; // The index of the current front entry

private boolean initialized = false;

private static final int DEFAULT_CAPACITY = 50;

private static final int MAX_CAPACITY = 10000;

public ArrayPriorityQueue()

{

this(DEFAULT_CAPACITY);

} // end default constructor

public ArrayPriorityQueue(int initialCapacity)

{

checkCapacity(initialCapacity);

// The cast is safe because the new array contains null entries

@SuppressWarnings("unchecked")

T[] tempQueue = (T[]) new Comparable[initialCapacity]; // Unchecked cast

queue = tempQueue;

frontIndex = -1;

initialized = true;

}

public void add(T newEntry)

{

checkInitialization();

ensureCapacity();

//TODO

}

public T remove()

{

checkInitialization();

//TODO

return null;

}

public T peek()

{

checkInitialization();

//TODO

return queue[frontIndex];

}

/**

* If queue is empty returns [].

* Else, returns as [1, 2, 3]

* Important: note a comma and single space before every

* item except the last, and after last there is no space.

* In both cases before and after square brackets there

* is no space.

*

*/

@Override

public String toString()

{

//TODO

return null;

}

public boolean isEmpty()

{

return (frontIndex < 0);

}

public int getSize()

{

return frontIndex + 1;

}

public void clear()

{

checkInitialization();

for (int x = 0; x < queue.length; x++)

queue[x] = null;

frontIndex = -1;

}

// Throws an exception if this object is not initialized.

private void checkInitialization()

{

if (!initialized)

throw new SecurityException ("ArrayPriorityQueue object is not "

+ "initialized properly.");

}

// Throws an exception if the client requests a capacity that is too large.

private void checkCapacity(int capacity)

{

if (capacity > MAX_CAPACITY)

throw new IllegalStateException("Attempt to create a priority queue " +

"whose capacity exceeds allowed maximum.");

}

// Doubles the size of the array queue if it is full.

// Precondition: checkInitialization has been called.

private void ensureCapacity()

{

if (frontIndex == queue.length - 1)

{

checkCapacity(queue.length * 2);

queue = java.util.Arrays.copyOf(queue, queue.length * 2);

}

}

} // end ArrayPriorityQueue

PLEASE Implement all the methods in the ArrayPriorityQueue class that have the //TODO in them. Should be the add(), remove(), peek() and toString() methods

NOTE:

1) You cannot create any additional data fields. Only use the ones provided

2) NO custom class can be used or introduced

3) NO IMPORT STATEMENTS ALLOWED

Please follow these directions and help me implment this class. Thank you very much!!

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

Oracle 10g SQL

Authors: Joan Casteel, Lannes Morris Murphy

1st Edition

141883629X, 9781418836290

More Books

Students also viewed these Databases questions