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: 3730682 • 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

Given below are the modified files code for the question.
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


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){
for(int i = 0; i < aList.size(); i++)
{
if(aList.get(i).equals(o))
return true;
}
return false;
}

}


-------------------

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 isEmpty() {

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 (!isEmpty()) {

Object queueFront = items[front];

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

--count;

return queueFront;

} 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(!isEmpty()){

return items[front];

}else{

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

}

}

public boolean contains(Object o)
{

for(int i = 0, j = front; i < count; i++, j++)
{
if(items[j].equals(o))
return true;
}

return false;//if not found in the loop above
}

}

-------------------

public class QueueReferenceBased implements QueueInterface {

private Node 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 QueueException("QueueException on dequeue:" + "queue empty");

}

}

public Object peek() throws QueueException{

if(!isEmpty()){

Node firstNode = lastNode.next;

return firstNode.item;

}else{

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

}

}

public boolean contains(Object o)
{

if(isEmpty())
return false;

Node firstNode = lastNode.next;
Node curr = firstNode;
do
{
if(curr.item.equals(o))
return true;
curr = curr.next;
}while(curr != firstNode);

return false;//when not found
}

}

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