Question: I need to write a method splice2 in this class i leave it blank public class ArrayQueue implements QueueInterface { private T[] queue; private int

I need to write a method splice2 in this class i leave it blank

public class ArrayQueue implements QueueInterface {

private T[] queue;

private int frontIndex;

private int backIndex;

private int initialCapacity = 10;

public ArrayQueue()

{

@SuppressWarnings("unchecked")

T[] tempQueue = (T[]) new Object[10];

queue = tempQueue;

frontIndex = 0;

backIndex = 9;

}

public void enqueue(T newEntry)

{

ensureCapacity();

backIndex = (backIndex + 1) % queue.length;

queue[backIndex] = newEntry;

}

public T dequeue() {

T dequeue = getFront();

queue[frontIndex] = null;

frontIndex = (frontIndex + 1) % queue.length;

return dequeue;

}

public T getFront()

{

if(isEmpty())

throw new IllegalStateException("List is null");

else

return queue[frontIndex];

}

public boolean isEmpty()

{

return frontIndex == ((backIndex + 1) % queue.length);

}

public void clear()

{

@SuppressWarnings("unchecked")

T[] tempQueue = (T[]) new Object [10];

queue = tempQueue;

frontIndex = 0;

backIndex = 9;

}

public void splice(QueueInterface q)

{

while(!q.isEmpty())

this.enqueue(q.dequeue());

}

public void splice2 (ArrayQueue q) {

}

private void ensureCapacity()

{

if(frontIndex == ((backIndex + 2) % queue.length))

{

T[] oldQueue = queue;

int oldSize = oldQueue.length;

int newSize = 2 * oldSize;

@SuppressWarnings("unchecked")

T[] tempQueue = (T[]) new Object[newSize];

for(int i = 0; i < oldSize - 1; i ++)

{

tempQueue[i] = oldQueue[frontIndex];

frontIndex = (frontIndex + 1) % oldSize;

}

queue = tempQueue;

frontIndex = 0;

backIndex = oldSize - 2;

}

}

public String toString()

{

String result = "";

for(int i = 0; i < queue.length; i++)

result += queue[i] + ", ";

return result;

}

}

my question is only part3 how can i implement the splice2 methods for both classes by doing take full advantage of your access to instance variables. other parts already done...

Assignment - Two Queues

Part 1: Create two working versions of the books Queue implementations:

ArrayQueue (Use a circular array with an unused location. Based on section 11.10)

LinkedQueue (Use a two part circular linked chain. Modeled on section 11.20)

Part 2: Add a method named splice to the Queue interface and to each class. The purpose of the splice method is to splice the parameter queue onto the end of the receiving queue. It should have the following signature:

Public void splice(QueueInterface q)

Notice that the parameter is the interface type. The code in splice should be exactly the same for both of your queue classes. It should only use interface methods on itself, and argument q.

Part 3: Add a method named splice2 to each class. This method should accept a class type parameter, and when you write it you should take full advantage of your access to instance variables. The signature would be different in each class. Here are the signatures for ArrayQueue and LinkedQueue respectively. Notice the parameter types differ:

Public void splice2(ArrayQueue q)

Public void splice2(LinkedQueue q)

What to Submit: Submit the following 5 files:

ArrayQueue.java

LinkedQueue.java

QueueInterface.java

A simple testing class that invokes all for methods: splice on each class, and splice2 on each class.

A text file with a brief description of your experience for the graders.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!