The array based implementation of the ADT queue in this chapter assumed a maximu
ID: 3565118 • Letter: T
Question
The array based implementation of the ADT queue in this chapter assumed a maximum queue size of 50 items. Modify this implementation so that when the queue becomes full, the size of the array is doubled. Write a test program which can demonstrate the added feature of doubling size of the array.
QueueInterface.java
public interface QueueInterface {
public boolean isEmpty();
// Determines whether a queue is empty.
// Precondition: None.
// Postcondition: Returns true if the queue is empty;
// otherwise returns false.
public void enqueue(Object newItem) throws QueueException;
// Adds an item at the back of a queue.
// Precondition: newItem is the item to be inserted.
// Postcondition: If the operation was successful, newItem
// is at the back of the queue. Some implementations may
// throw QueueException if newItem cannot be added to the
// queue.
public Object dequeue() throws QueueException;
// Retrieves and removes the front of a queue.
// Precondition: None.
// Postcondition: If the queue is not empty, the item
// that was added to the queue earliest is returned and
// the item is removed. If the queue is empty, the
// operation is impossible and QueueException is thrown.
public void dequeueAll();
// Removes all items of a queue.
// Precondition: None.
// Postcondition: The queue is empty.
public Object peek() throws QueueException;
// Retrieves the item at the front of a queue.
// Precondition: None.
// Postcondition: If the queue is not empty, the item
// that was added to the queue earliest is returned.
// If the queue is empty, the operation is impossible
// and QueueException is thrown.
} // end QueueInterface
QueueException.java
public class QueueException extends RuntimeException {
public QueueException(String s) {
super(s);
} // end constructor
} // end QueueException
Explanation / Answer
import java.io.*; import java.util.Scanner; public class ArrayDoubling { public static void main(String[] args) throws IOException { File inpFile = new File( "ArrayDoubling.inp" ); Scanner inp = new Scanner ( inpFile ); double[] a = new double[1]; double x; int n; // Number of items n = 0; while ( inp.hasNextDouble() ) { x = inp.nextDouble(); /* --------------------------------------- Check if array has space to store x --------------------------------------- */ if ( n == a.length ) { /* ========================================== The array double algorithm ========================================== */ double[] h; // help variable to make a bigger array h = new double[ 2*a.length ]; // make aray twice as big for ( int i = 0; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.