Question: Please use java. Thanks! The ArrayQueue.java code is : public interface Queue { boolean isEmpty(); void add(T item); T peek(); T remove(); } public class
Please use java. Thanks!
1 Array-based Implementation To start with, let's create an interface for a Deque that allplemetions will implement. Create a le called Deque java and add the following code public interface DequecT boolean isEmpty void addPirst (T item)a void addlaat IT item) ? peekFirst(); T peeklast T renoveFirst renovela st(); Now find the ArrayQorue-java implementation from lecture last week and download it. Change the name of the class(and the filename) to ArrayDeque, and declare that it implements Deque instead of Queue. Change the name of add) to addt.ast O.peek 0 to peekFirst , and zemove to removeFiret )You will also need to download EmptyQueue xception java-if you want, you could even rename this to EmptyDequeExceptlon, but this is not mecessary We need three more methods to complete an implementation of a deque. We need to write addFlrst 0-peekLast Oand zenovelast 0- For ow add placebholders for these methods by adding the following code to Arraybeque: public void addFirst (7 item) public 7 peekLast return nu1l: public T removeLast returm null Complle your code to make sure that it works at this point
The ArrayQueue.java code is :
public interface Queue
{
boolean isEmpty();
void add(T item);
T peek();
T remove();
}
public class ArrayQueue implements Queue
{
private Object[] array;
private int size = 0;
private int first = 0;
private int last = -1;
public ArrayQueue(int capacity)
{
array = new Object[capacity];
}
public boolean isEmpty()
{
return size == 0;
}
private int incrementIndex(int index)
{
int newIndex = index + 1;
if (newIndex == array.length)
{
return 0;
}
else
{
return newIndex;
}
}
private void resize(int newCapacity)
{
Object[] newArray = new Object[newCapacity];
int oldIndex = first;
for (int i = 0; i
{
newArray[i] = array[oldIndex];
oldIndex = incrementIndex(oldIndex);
}
array = newArray;
first = 0;
last = size - 1;
}
public void add(T element)
{
if (size == array.length)
{
resize(size * 2);
}
last = incrementIndex(last);
array[last] = element;
size++;
}
@SuppressWarnings("unchecked")
public T peek()
{
if (size == 0)
{
throw new EmptyQueueException();
}
else
{
return (T) array[first];
}
}
@SuppressWarnings("unchecked")
public T remove()
{
if (size == 0)
{
throw new EmptyQueueException();
}
else
{
size--;
T result = (T) array[first];
first = incrementIndex(first);
return result;
}
}
}
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
