Question: The JCF class Deque had a method called contains that would return true if an Object was in the deque. For a que, the method

The JCF class Deque had a method called contains that would return true if an Object was in the deque. For a que, the method could be specified as follows:

public boolean contains(Object o) //Returns true if this queue contains the specified element.

a. Add this method to the QueueListBased implementation gien in this chapter.

public class QueueListBased implements QueueInterface{

private ListInterface aList;

public QueueListBased(){

aList=new ListReferenceBased();

}

public boolean isEmpty(){

return aList.isEmpty();

}

public void enqueue(Object newItem){

aList.add(aList.size(),newItem);

}

public Object dequeue() throws QueueException{

if(!isEmptry()){

Object queueFront=aList.get(0);

aList.remove(0);

return queueFront;

}else{

throw new QueueException("Queue exception on dequeue: "+ " queue empty");

}

}

public void dequeueAll(){

aList.removeAll();

}

public Object peek() throws QueueException{

if(!isEmptry()){

return aList.get(0);

}else{

throw new QueueException("Queue exception on peek " +" queue emptry");

}

}

}

b. Add this method to the QueueArrayBased implementation given in this chapter.

public class queueArrayBased implements QueueInterface{

private final int MAX_QUEUE=50;

private Object[] items;

private int front, back, count;

public QueueArrayBased(){

items = new Object[MAX_QUEUE];

front=0;

back=MAX_QUEUE-1;

count=0;

}

public boolean isEmptry(){

return count ==0;

}

public boolean isFull(){

return count ==MAX_QUEUE;

}

public void enqueue(Object newItem) throws QueueException{

if(!isFull()){

back=(back+1) %(MAX_QUEUE);

items[back]=newItem;

++count;

}else{

throw new QueueException("QueueException on enqueue: " + "Queue full");

}

}

public Object dequeue() throws QueueException{

if(!isEmptry()){

Object queueFront =items[front];

front=(front+1)%(MAX_QUEUE);

--count;

return queuFront;

}else{

throw new QueueException("QueueExcetion on dequeue: " + "Queue empty");

}

}

public void dequeueAll(){

items=new Object[MAX_QUEUE];

front =0;

back=MAX_QUEUE-1;

count=0;

}

public Object peek() throws QueueException{

if(!isEmptry()){

return items[front];

}else{

thrownew QueueException("Queue exception on peek: "+"Queue empty");

}

}

}

c. Add this method to the QueueReferenceBased implementation given in this chapter.

public class QueueReferenceBased implements QueueInterface{

private Noce lastNode;

public QueueReferenceBased(){

lastNode=null;

}

public boolean isEmpty(){

return lastNode==null;

}

public void dequeAll(){

lastNode=null;

}

public void enqueue(Object newItem){

Node newNode=new Node(newItem);

if(!isEmpty)){

newNode.next=newNode;

}else{

newNode.next=lastNode.next;

lastNode.next=newNode;

}

lastNode=newNode;

}

public Object dequeue() throws QueueException{

if(!isEmpty()){

Node firstNode=lastNode.next;

if(firstNode==lastNode){

lastNode=null;

}else{

lastNode.next=firstNode.next;

}

return firstNode.item;

}else{

throw new QueueExceptioin("QueueException on dequeue:" +"queue empty");

}

}

public Object peek() throws QueueException{

if(!isEmpty)){

Node firstNode = astNode.next;

return firstNode.item;

}else{

throw new QueueException("QueueException on peek:" +queue empty");

}

}

}

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!