0. Introduction. For this laboratory assignment, you must implement an iterator
ID: 3699799 • Letter: 0
Question
0. Introduction.
For this laboratory assignment, you must implement an iterator for the Java class ArrayQueue that was discussed in the lectures. The class ArrayQueue implements a fixed length queue using a circular array.
1. Theory.
Suppose that we want to visit the elements stored in a sequence, like a stack or a queue. Also suppose that we are not allowed to modify the sequence to visit its elements. Then we can visit a sequence’s elements by using an iterator. An iterator is class whose instances can visit the elements of a sequence. Each iterator typically has a method called hasNext that tests if there are more elements to be visited. It also has a method called next that returns the next element to be visited and advances to the following element. An iterator that visits the elements of a linked stack was discussed in the lectures.
We can simplify an iterator’s design by assuming that the sequence will not change while we visit its elements. For example, if we use an iterator to visit the elements of a stack, then we assume that the stack will not be push’ed or pop’ped. Similarly, if we use an iterator to visit the elements of a queue, then we assume that the queue will not be enqueue’d or dequeue’d. If a sequence changes while an iterator visits its elements, then the actions of the iterator become undefined—which means they don’t have to work correctly if that happens.
2. Implementation.
You must add the following members to the class ArrayQueue, whose Java source code is available on Moodle. These members implement an iterator for ArrayQueue. You are not allowed to modify ArrayQueue except to add these additional members.
public class Iterator
This class must be nested inside ArrayQueue. An instance of this class may be used to visit the current elements of an instance of ArrayQueue. It must have one or more private variables that let it ‘‘know’’ which elements of ArrayQueue are to be visited next. You must decide what those private variables are.
private Iterator(...)
This is Iterator’s constructor. Of course it must be inside Iterator. It must set Iterator’s private variables to the values of its parameters. You must decide what these parameters are.
public boolean hasNext()
This method must be inside Iterator. It must return true if there are more elements of ArrayQueue that remain to be visited. It must return false otherwise. This method must use Iterator’s private variables only. Hint: use ideas from ArrayQueue’s method isEmpty.
public Base next()
This method must be inside Iterator. It must return the next Base element to be visited from ArrayQueue. If no more elements remain to be visited, then it must throw an IllegalStateException. This method must use Iterator’s private variables only. Hint: use ideas from ArrayQueue’s method dequeue.
public Iterator iterator()
This method must be inside ArrayQueue. It must call Iterator’s constructor to make a new instance of Iterator. It must then return the new instance.
Be careful to put these members in the right places. For example, Iterator must be nested inside ArrayQueue, and next must be inside Iterator, etc. If the members are in the wrong places, then the iterator will not work.
This gives us a slightly different kind of iterator from the one discussed in the lecture, and also different from the ones provided by Java. It does not have a remove method, and it is not accessed using an interface.
------------------ARRAY QUEUE.Java----------------------
----------------------------TESTS-----------------------
Explanation / Answer
Given below is the modified code for the ArrayQueue class as per specification. The output generated from test program matches the expected output as mentioned in the comments in the test program.
NOTE: PLEASE ADD THE KEYWORD public IN FRONT OF class QueIterator, so that you are able to run the main() method.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
class ArrayQueue<Base>
{
private int front; // Index of front object in OBJECTS.
private int rear; // Index of rear object in OBJECTS.
private Base[] objects; // The OBJECTs in the queue.
// Constuctor. Make a new empty queue that can hold SIZE - 1 elements.
public ArrayQueue(int size)
{
if (size <= 1)
{
throw new IllegalArgumentException("Illegal size.");
}
else
{
front = 0;
rear = 0;
objects = (Base []) new Object[size];
}
}
// DEQUEUE. Remove an object from the queue.
public Base dequeue()
{
if (front == rear)
{
throw new IllegalStateException("Queue is empty.");
}
else
{
front = (front + 1) % objects.length;
Base temp = objects[front];
objects[front] = null;
return temp;
}
}
// ENQUEUE. Add a new OBJECT to the queue.
public void enqueue(Base object)
{
int nextRear = (rear + 1) % objects.length;
if (front == nextRear)
{
throw new IllegalStateException("Queue is full.");
}
else
{
rear = nextRear;
objects[rear] = object;
}
}
// IS EMPTY. Test if the queue is empty.
public boolean isEmpty()
{
return front == rear;
}
// IS FULL. Test if the queue is full.
public boolean isFull()
{
return front == (rear + 1) % objects.length;
}
public Iterator iterator()
{
return new Iterator(front, rear);
}
public class Iterator
{
private int first, last;
private Iterator(int f, int l)
{
first = f;
last = l;
}
public boolean hasNext()
{
if(first == last)
return false;
else
return true;
}
public Base next()
{
if (first == last)
{
throw new IllegalStateException("Queue is empty.");
}
else
{
first = (first + 1) % objects.length;
Base temp = objects[first];
return temp;
}
}
}
}
output
======
A
B
C
false
A
B
C
true
X
Y
Z
false
X
Y
Z
true
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.