After been taught about Queues in lecture I've tried to do my own implementation.
I like to mention: The ideas were shown in lecture on a blackboard using a pseudocode notation.
The following code has been figured out by myself based upon my lecture notes. I've avoided having a closer look upon a concrete Java-implementations. I hope to get more familiar with the Queue concept if I first try to do it by myself.
Queue
class:
package playground;
public class Queue
{
private int[] items;
private int first;
private int last;
private int countItems;
public Queue(int itemsLength) {
items = new int[itemsLength];
countItems = 0;
first = 0;
last = -1;
}
public void addToQueue(int item) {
// If current count items is smaller then the
// maximal size of the array ...
if (countItems < items.length) {
last = (last + 1) % items.length;
items[last] = item;
countItems++;
} else {
throw new IndexOutOfBoundsException(
"Can not add another item. Queue is already full.");
}
}
public int removeFromQueue() {
if (countItems > 0) {
int currentFirst = items[first];
first = (first + 1) % items.length;
countItems--;
return currentFirst;
} else {
throw new IndexOutOfBoundsException(
"There are no items to remove because the Queue is empty.");
}
}
}
The test-class I've made:
package playground;
import static java.lang.System.*;
public class Playground
{
public static void main(String[] args) {
Queue qu = new Queue(3);
try {
qu.addToQueue(2);
qu.addToQueue(4);
qu.addToQueue(8);
qu.addToQueue(100);
} catch (Exception e) {
out.println(e.getMessage());
}
try {
out.println("First remove: " + qu.removeFromQueue());
} catch (Exception e) {
out.println(e.getMessage());
}
try {
qu.addToQueue(16);
} catch (Exception e) {
out.println(e.getMessage());
}
try {
out.println("Second remove: " + qu.removeFromQueue());
out.println("Second remove: " + qu.removeFromQueue());
out.println("Second remove: " + qu.removeFromQueue());
} catch (Exception e) {
out.println(e.getMessage());
}
try {
qu.addToQueue(32);
} catch (Exception e) {
out.println(e.getMessage());
}
try {
out.println("Third remove: " + qu.removeFromQueue());
out.println("Third remove: " + qu.removeFromQueue());
} catch (Exception e) {
out.println(e.getMessage());
}
try {
qu.addToQueue(64);
qu.addToQueue(128);
// Only two items in t. queue. But trying to remove three items ...
out.println("Fourth remove: " + qu.removeFromQueue());
out.println("Fourth remove: " + qu.removeFromQueue());
out.println("Fourth remove: " + qu.removeFromQueue());
} catch (Exception e) {
out.println(e.getMessage());
}
}
}
The output of the test-class:
Can not add another item. Queue is already full.
First remove: 2
Second remove: 4
Second remove: 8
Second remove: 16
Third remove: 32
There are no items to remove because the Queue is empty.
Fourth remove: 64
Fourth remove: 128
There are no items to remove because the Queue is empty.
The output of the test-class is what I've expected, so the Queue
seems to work correctly to me. But there might be cases in which it produces errors, which I haven't thought about.
Is my implementation correct? Or does I have to improve it? Moreover, is my error handling done in an appropriate way?
I throw exceptions when someone tries to do a forbidden action (removing items from an empty queue, adding items to a full queue). But I'm not sure because throwing an exception might be to drastically.
In lecture a -1 was returned for showing that something has gone wrong. But I'm not convinced about that approach. -1 could be in the Queue just as a normal value. If I then write an error-handling routing which reacts to a return value of -1 then things could go wrong in an unforeseeable way.
How should the error-handling be done?