Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can someone write a JUnit test for my CircularArrayQueue class that implements ItemQueue. I use EclipseIDE and Java, Thank You. CircluarArrayQueue.java: import java.util.NoSuchElementException; /** *

Can someone write a JUnit test for my CircularArrayQueue class that implements ItemQueue. I use EclipseIDE and Java, Thank You.

CircluarArrayQueue.java:

import java.util.NoSuchElementException;

/**

* Circular array implementation of a queue

*

* @author

*

*/

public class CircularArrayQueue implements ItemQueue {

private static final int capacity = 4;

private Object[] Q;

private int N; // capacity

private int f = 0;

private int r = 0;

public CircularArrayQueue() {

this(capacity);

}

public CircularArrayQueue(int capacity) {

N = capacity;

Q = new Object[N];

}

@Override

public void enqueueItem(E item) {

if (isFull()) {

throw new NoSuchElementException("Queue is Full.");

} else {

Q[r] = item;

r = (r + 1) % N;

}

}

@SuppressWarnings("unchecked")

@Override

public E dequeueItem() {

E item;

if (isEmpty()) {

throw new NoSuchElementException("Queue is Empty.");

} else {

item = (E) Q[f];

Q[f] = null;

f = (f + 1) % N;

}

return (E) item;

}

@SuppressWarnings("unchecked")

@Override

public E peekItem() {

if (isEmpty()) {

throw new NoSuchElementException("Queue is Empty.");

} else {

return (E) Q[r];

}

}

/**

* check for empty queue

*

*/

@Override

public boolean isEmpty() {

return (r == f) ? true : false;

}

/**

* check for full queue

*

*/

public boolean isFull() {

int diff = r - f;

if (diff == -1 || diff == (N - 1))

return true;

return false;

}

/**

* check number of items in the queue

*

*/

@Override

public int noItems() {

if (r > f)

return r - f;

return N - f + r;

}

}

ItemQueue.java:

/**

* An abstract data type for a Queue. Specifies the 5

* main methods of a queue

*

* @author

*/

public interface ItemQueue {

/**

* Enqueue an object to the end of the queue

* @param item the object to be enqueued

*/

public void enqueueItem(E item);

/**

* Dequeue an object from the front of the queue

* @return the item removed from the front of the queue

* @throws NoSuchElementException if the queue is empty

*/

public E dequeueItem();

/**

* Examine the object at the head of the queue, but do

* not remove it.

* @return the item at the head of the queue

* @throws NoSuchElementException if the queue is empty

*/

public E peekItem();

/**

* Test if the queue is empty

* @return true if the queue is empty, otherwise false

*/

public boolean isEmpty();

/**

* Get the number of items in the queue

*

* @return the number of items in the queue

*/

public int noItems();

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions