Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The JCF class Deque had a method called contains that would return true if an Ob

ID: 3730592 • Letter: T

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 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");

}

}

}

Explanation / Answer

a)

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");

}

}

public boolean contains(Object o){

if(o==null){

return false;

}

int i=0;

while(i<aList.size()){

if(o.equals(aList.get(i))){

return true;

}

i++;

}

return false;

}

}

b)

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");

}

}

public boolean contains(Object o){

if(o==null){

return false;

}

int i=front;

while(i<back){

if(o.equals(items[i])){

return true;

}

i++;

}

return false;

}

}

c)

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");

}

}

public boolean contains(Object o){

if(o==null){

return false;

}

Node itr=firstNode;

while(itr!=lastNode){

if(o.equals(itr.item)){

return true;

}

itr=itr.next;

}

return false;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote