The existing ArrayList class already has methods for adding, removing, and looki
ID: 3756442 • Letter: T
Question
The existing ArrayList class already has methods for adding, removing, and looking at elements in an array. We can use inheritance to extend this class, building on existing methods to implement the required Queue methods. For example, the definition of the removemethod in the new class ArrayQueue is provided below:
public class ArrayQueue<E> extends ArrayList<E> implements Queue<E> { public E remove() { return super.remove(0); } // definitions of the other methods required by Queue }
Complete the definition of the ArrayQueue class so that it fully implements the Queue interface.
Explanation / Answer
In order to answer your problem correctly, you would need to provide me the implementation of ArrayList class and interface Queue. Without queue interface, I cannot possibly know which methods are present in the interface and hence, I cannot provide you the complete implementation of ArrayQueue.
For time being, I am adding the basic methods in the queue interface and implementing them. For any query, please reach out to me.
CODE
========================
interface Queue<E> {
public boolean isEmpty();
public void add(E item);
public E remove();
public void display();
}
public class ArrayQueue<E> extends ArrayList<E> implements Queue<E> {
@Override
public boolean isEmpty() {
return super.size() == 0;
}
@Override
public void add(E item) {
super.add(item);
}
@Override
public E remove() {
if (isEmpty()) {
return null;
}
return super.remove(0);
}
@Override
public void display() {
for (int i=0; i<super.size(); i++) {
System.out.println(super.get(i) + " ");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.