Create a LLQueue class that implements the interface below. 2) The implementatio
ID: 3701885 • Letter: C
Question
Create a LLQueue class that implements the interface below. 2) The implementation must be a linked list-based. 3) The implementation must add a max_capacity to prevent users from adding over the limit. 4) For this program use Tool objects as your testing data. 5) Add a Driver/Demo/Test class to test all the methods of LLQueue. 6) Test your project and screen-capture your sample testing to be sent along with the project public interface QueueInterface<T> { public void enqueue(T newEntry); public T dequeue(); public T getFront(); public boolean isEmpty(); public void clear(); public int size(); public T[] toArray(); public String toString(); } Create a LLQueue class that implements the interface below. 2) The implementation must be a linked list-based. 3) The implementation must add a max_capacity to prevent users from adding over the limit. 4) For this program use Tool objects as your testing data. 5) Add a Driver/Demo/Test class to test all the methods of LLQueue. 6) Test your project and screen-capture your sample testing to be sent along with the project public interface QueueInterface<T> { public void enqueue(T newEntry); public T dequeue(); public T getFront(); public boolean isEmpty(); public void clear(); public int size(); public T[] toArray(); public String toString(); } Create a LLQueue class that implements the interface below. 2) The implementation must be a linked list-based. 3) The implementation must add a max_capacity to prevent users from adding over the limit. 4) For this program use Tool objects as your testing data. 5) Add a Driver/Demo/Test class to test all the methods of LLQueue. 6) Test your project and screen-capture your sample testing to be sent along with the project public interface QueueInterface<T> { public void enqueue(T newEntry); public T dequeue(); public T getFront(); public boolean isEmpty(); public void clear(); public int size(); public T[] toArray(); public String toString(); } Create a LLQueue class that implements the interface below. 2) The implementation must be a linked list-based. 3) The implementation must add a max_capacity to prevent users from adding over the limit. 4) For this program use Tool objects as your testing data. 5) Add a Driver/Demo/Test class to test all the methods of LLQueue. 6) Test your project and screen-capture your sample testing to be sent along with the project public interface QueueInterface<T> { public void enqueue(T newEntry); public T dequeue(); public T getFront(); public boolean isEmpty(); public void clear(); public int size(); public T[] toArray(); public String toString(); }Explanation / Answer
Below is your code. I was not able to give the possible implementation of public T[] toArray(); . Please ask this method in a different question. Below is your rest of the code.
QueueInterface.java
public interface QueueInterface<T> {
public void enqueue(T newEntry);
public T dequeue();
public T getFront();
public boolean isEmpty();
public void clear();
public int size();
//public T[] toArray();
public String toString();
}
LLQueue.java
public class LLQueue<T> implements QueueInterface<T> {
private Node<T> first; // beginning of queue
private Node<T> last; // end of queue
private int n; // number of elements on queue
private final int max_capacity = 30;
// helper linked list class
private static class Node<T> {
private T item;
private Node<T> next;
}
/**
* Initializes an empty queue.
*/
public LLQueue() {
first = null;
last = null;
n = 0;
}
/**
* Returns true if this queue is empty.
*
* @return {@code true} if this queue is empty; {@code false} otherwise
*/
public boolean isEmpty() {
return first == null;
}
/**
* Returns the number of items in this queue.
*
* @return the number of items in this queue
*/
public int size() {
return n;
}
@Override
public void enqueue(T newEntry) {
if (n <= max_capacity) {
Node<T> oldlast = last;
last = new Node<T>();
last.item = newEntry;
last.next = null;
if (isEmpty())
first = last;
else
oldlast.next = last;
n++;
} else {
System.out.println("List is full");
}
}
@Override
public T dequeue() {
if (isEmpty())
throw new NoSuchElementException("Queue underflow");
T item = first.item;
first = first.next;
n--;
if (isEmpty())
last = null; // to avoid loitering
return item;
}
@Override
public T getFront() {
if (isEmpty())
throw new NoSuchElementException("Queue underflow");
return first.item;
}
@Override
public void clear() {
first = null;
last = null;
n = 0;
}
/*@Override
public T[] toArray() {
// TODO Auto-generated method stub
if (isEmpty()) {
throw new NoSuchElementException("Queue underflow");
}
ArrayList<T> newList = new ArrayList<>();
Node<T> f = first;
Node<T> l = last;
while (f != l.next) {
newList.add(f.item);
f = f.next;
}
T[] ts = (T[])new Object[newList.size()];
return ts;
}*/
public String toString() {
if (isEmpty()) {
return "";
}
String str = "";
Node<T> f = first;
Node<T> l = last;
while (f != l.next) {
str = str + f.item + " ";
f = f.next;
}
return str;
}
}
DriverList.java
public class DriverList {
public static void main(String[] args) {
LLQueue<Integer> queue = new LLQueue<>();
System.out.println("Is queue empty? "+queue.isEmpty());
System.out.println("Queue before insertion: "+queue+" and size is : "+queue.size());
queue.enqueue(10);
queue.enqueue(13);
queue.enqueue(2);
queue.enqueue(15);
queue.enqueue(20);
queue.enqueue(60);
System.out.println("Queue after insertion: "+queue+" and size is : "+queue.size());
queue.dequeue();
queue.dequeue();
System.out.println("Queue after deletion: "+queue+" and size is : "+queue.size());
System.out.println("Front element of the queue: "+queue.getFront());
/* Integer[] array = (Integer[])queue.toArray();
System.out.println("printing queue in terms of array: ");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}*/
queue.clear();
System.out.println("Queue after clearing: "+queue+" and size is : "+queue.size());
}
}
Output
Is queue empty? true
Queue before insertion: and size is : 0
Queue after insertion: 10 13 2 15 20 60 and size is : 6
Queue after deletion: 2 15 20 60 and size is : 4
Front element of the queue: 2
Queue after clearing: and size is : 0