Question: /** * This interface describes the simplest form of queue. */ public interface Queue { /** * Add to the tail of the queue. *
/** * This interface describes the simplest form of queue. */ public interface Queue{ /** * Add to the tail of the queue. * @param element The value to be added to the tail * of the queue */ public void enqueue(E element); /** * Remove from the head of the queue. * @return The value from the head of the queue */ public E dequeue(); /** * Peek at the head of the queue. * @return The value at the head of the queue */ public E peek(); /** * Check for elements in the queue. * @return True if the queue is empty */ public boolean isEmpty(); }
Using the starting code, Queue.Java, use a partially-filled array as the underlying storage mechanism.
For the queue, we will need to decide where the head and tail of the queue are relative to the array. It's probably easiest to make the head of the queue index zero of the array.
The general formal parameter of the interface will be filled in with the type java.lang.String. Name your implementation StringQueue as shown below:
public class StringQueue implements Queue
The constructor shall allocate space for 10 strings in the underlying array.
The method that will require the most thoughtful coding is the dequeue method. Remember that we have established the head of the queue at index zero, [0]. So, when that value is dequeued, the remaining values in the queue will need to slide over to fill in the newly emptied spot in the array.
Taking an example for the Queue, let's say we have enqueued the first three names: Ahmed, Bobby, and Chris. Schematically, here is the state of the queue:
storage: ["Ahmed", "Bobby", "Chris", null, null, null, null, null, null, null] count: 3
Or pictorially:
[Ahmed, Bobby, Chris, null, null, null, null, null, null, null] count: ^
After we call serviceQueue.dequeue( ), we want the state of the queue to be:
storage: ["Bobby", "Chris", null, null, null, null, null, null, null, null] count: 2
Or pictorially:
[Bobby, Chris, null, null, null, null, null, null, null, null] count: ^
Decrementing count is simple and straight forward. Notice that the rest of the values in the array need to move down by one place. It is also wise to null out (assign null) to the newly opened up spot.
Update the StringQueue class to check for preconditions. Throw an appropriate exception is any precondition is not met. You can use the predefined IllegalStateException as was shown in the demo.
Also, overload the constructor. The new constructor shall take a single int parameter, the size of the queue. If the argument is negative, the constructor shall throw an IllegalArgumentException. Provide an appropriate error message in the exception.
Create a new implementation of Queue which is a generic class. Name this new class GenericQueue. There should be no warning or error message from either the Java compiler or the JavaDoc documentation tool.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
