In: Computer Science
Define a class of queues that implements the interface QueueInterface, as defined in Listing 7-1 in Chapter 7. Use an instance of the class ArrayList to contain a queues entries. Then write a driver/test program adequately demonstrates your new class. Note that you might have to handle exceptions thrown by methods of ArrayList.
Deliverables:
QueInterface:
@author Frank M. Carrano
@author Timothy M. Henry
@version 5.0
*/
public interface QueueInterface<T>
{
/** Adds a new entry to the back of this queue.
@param newEntry An object to be added. */
public void enqueue(T newEntry);
/** Removes and returns the entry at the front of this queue.
@return The object at the front of the queue.
@throws EmptyQueueException if the queue is empty before the
operation. */
public T dequeue();
/** Retrieves the entry at the front of this queue.
@return The object at the front of the queue.
@throws EmptyQueueException if the queue is empty. */
public T getFront();
/** Detects whether this queue is empty.
@return True if the queue is empty, or false otherwise. */
public boolean isEmpty();
/** Removes all entries from this queue. */
public void clear();
} // end QueueInterface
EmptyQueueException:
/**
A class of runtime exceptions thrown by methods to
indicate that a queue is empty.
@author Frank M. Carrano
@author Timothy M. Henry
@version 5.0
*/
public class EmptyQueueException extends RuntimeException
{
public EmptyQueueException()
{
this(null);
} // end default constructor
public EmptyQueueException(String message)
{
super(message);
} // end constructor
} // end EmptyQueueException
//QueInterface.java
/**
@author Frank M. Carrano
@author Timothy M. Henry
@version 5.0
*/
public interface QueueInterface<T>
{
/** Adds a new entry to the back of this queue.
@param newEntry An object to be added. */
public void enqueue(T newEntry);
/** Removes and returns the entry at the front of this queue.
@return The object at the front of the queue.
@throws EmptyQueueException if the queue is empty before the
operation. */
public T dequeue();
/** Retrieves the entry at the front of this queue.
@return The object at the front of the queue.
@throws EmptyQueueException if the queue is empty. */
public T getFront();
/** Detects whether this queue is empty.
@return True if the queue is empty, or false otherwise. */
public boolean isEmpty();
/** Removes all entries from this queue. */
public void clear();
} // end QueueInterface
//EmptyQueueException.java
/**
A class of runtime exceptions thrown by methods to
indicate that a queue is empty.
@author Frank M. Carrano
@author Timothy M. Henry
@version 5.0
*/
public class EmptyQueueException extends RuntimeException
{
public EmptyQueueException()
{
this(null);
} // end default constructor
public EmptyQueueException(String message)
{
super(message);
} // end constructor
} // end EmptyQueueException
//ArrayListQueue.java
import java.util.ArrayList;
public class ArrayListQueue<T> implements
QueueInterface<T>
{
ArrayList<T> arr;
//constructor
public ArrayListQueue()
{
arr = new
ArrayList<>();
}
/** Adds a new entry to the back of this queue.
@param newEntry An object to be added. */
public void enqueue(T newEntry)
{
arr.add(newEntry);
}
/** Removes and returns the entry at the front of this
queue.
@return The object at the front of the queue.
@throws EmptyQueueException if the queue is empty
before the operation. */
public T dequeue()
{
if(isEmpty())
throw new
EmptyQueueException();
T entry = arr.get(0);
arr.remove(0);
return entry;
}
/** Retrieves the entry at the front of this
queue.
@return The object at the front of the queue.
@throws EmptyQueueException if the queue is empty.
*/
public T getFront()
{
if(isEmpty())
throw new
EmptyQueueException();
return arr.get(0);
}
/** Detects whether this queue is empty.
@return True if the queue is empty, or false
otherwise. */
public boolean isEmpty()
{
return arr.size()==0;
}
/** Removes all entries from this queue. */
public void clear()
{
arr.clear();
}
}
//Lab7.java
public class Lab7
{
//main method
public static void main (String[] args) throws
EmptyQueueException
{
//create object of ArrayListQueue
of integers
ArrayListQueue<Integer> aq =
new ArrayListQueue<>();
//enqueue 5 items
for(int i=1; i<=5; i++)
{
int x =
i*i;
aq.enqueue(x);
System.out.println ("Enqueue: " + x);
}
//check if queue is empty
System.out.println ("Is Queue
empty? " + aq.isEmpty());
///dequeue 3 items
for(int i=1; i<=3; i++)
{
System.out.println ("Dequeue: " + aq.dequeue());
}
//check if queue is empty
System.out.println ("Is Queue
empty? " + aq.isEmpty());
//clear the queue
System.out.println ("Clear the
queue.");
aq.clear();
//check if queue is empty
System.out.println ("Is Queue
empty? " + aq.isEmpty());
}
}
Solving your question and
helping you to well understand it is my focus. So if you face any
difficulties regarding this please let me know through the
comments. I will try my best to assist you. However if you are
satisfied with the answer please don't forget to give your
feedback. Your feedback is very precious to us, so don't give
negative feedback without showing proper reason.
Always avoid copying from existing answers to avoid
plagiarism.
Thank you.