Question: I need help with this! I posted a question earlier with the ArrayBoundedQueue2 that had the methods needed. Code for ArrayBoundedQueue2(I'm not sure if it

I need help with this! I posted a question earlier with the ArrayBoundedQueue2 that had the methods needed.

I need help with this! I posted a question earlier with the

Code for ArrayBoundedQueue2(I'm not sure if it is correct or not):

import ch04.queues.QueueInterface; import ch04.queues.QueueOverflowException; import ch04.queues.QueueUnderflowException;

public class ArrayBoundedQueue2 implements QueueInterface { protected final int DEFCAP = 100; // default capacity protected T[] elements; // array that holds queue elements protected int numElements = 0; // number of elements in this queue protected int front = 0; // index of front of queue protected int rear; public ArrayBoundedQueue2() { elements = (T[]) new Object[DEFCAP]; rear = DEFCAP - 1; }

public ArrayBoundedQueue2(int maxSize) { elements = (T[]) new Object[maxSize]; rear = maxSize - 1; }

public void enqueue(T element) // Throws QueueOverflowException if this queue is full; // otherwise, adds element to the rear of this queue. { if (isFull()) throw new QueueOverflowException("Enqueue attempted on a full queue."); else { rear = (rear + 1) % elements.length; elements[rear] = element; numElements = numElements + 1; } }

public T dequeue() // Throws QueueUnderflowException if this queue is empty; // otherwise, removes front element from this queue and returns it. { if (isEmpty()) throw new QueueUnderflowException("Dequeue attempted on empty queue."); else { T toReturn = elements[front]; elements[front] = null; front = (front + 1) % elements.length; numElements = numElements - 1; return toReturn; } }

public boolean isEmpty() // Returns true if this queue is empty; otherwise, returns false. { return (numElements == 0); }

public boolean isFull() // Returns true if this queue is full; otherwise, returns false. { return (numElements == elements.length); } public int size() // Returns the number of elements in this queue. { return numElements; }

public static String toString(Queue q) { String str=q.element(); str+=str+" "; return str; }

public static int space(Queue q) { int count=0; Iterator i =q.i(); while(i.hasNext()) { if(i.hasNext()=" ") { count++; } } }

public static int remove(Queue q) { Iterator i=q.i(); if(i.hasNext()) { q.remove(); } else { System.out.println("Queue underflow exception"); } } public static boolean swapStart(Queue q) { int count=0; Iterator i=q.i(); if(i.hasNext()) { count++; } if(count>2) { return true; } else { return false; } } public static boolean swapEnds(Queue q) { int count=0; Iterator i=q.i(); int temp=i; if(i.hasNext()) { count++; } if(count>2) { return true; } else { return false; } } }

ArrayBoundedQueue2Demo: (5 total points) The program must test the ADT to ensure that all added methods function correctly. If any result from the program is incorrect, that means the tested method is not implemented correctly. You may use any of the methods in the queue class for testing. 1) (1 point) Test the toString method by doing the following: a. Use toString and output the status of an empty queue. Is the output what you expect? b. Use toString and output the status of a queue containing one element. Is the output what you expect? c. Use toString and output the status of a queue containing more than two elements. Is the output what you expect? 2) (1 point) Test the space method by doing the following: a. Call space on an empty queue and output the result. Is the output what you expect? b. Call space on a queue containing one element and output the result. Is the output what you expect? c. Call space on a queue containing several elements and output the result. Is the output what you expect? d. Call space on a full queue and output the result. Is the output what you expect? 3) (1 point) Test the remove method by doing the following: a. Call remove on an empty queue, and catch the exception that should occur. You will want to use a try/catch block for this part of code so that the exception does not crash the program. Is the exception correctly thrown? b. Call remove on a non-empty queue to remove less elements than are in the queue (for example, remove 5 elements from a queue containing 10 elements), and then outputting the contents of the queue using toString. Is the output what you expect? 4) (1 point) Test the swapstart method by doing the following: a. Call swapstart on an empty queue and output the result. Is the output what you expect? b. Call swapstart on a queue containing only one element and output the result. Is the output what you expect? C. Call swapstart on a queue containing two elements and output the result along with the contents of the queue using toString. Is the output what you expect? d. Call swapstart on a queue containing more than two elements and output the result along with the contents of the queue using toString. Is the output what you expect? 5) (1 point) Test the swapends method by doing the following: a. Call swapends on an empty queue and output the result. Is the output what you expect? b. Call swapends on a queue containing only one element and output the result. Is the output what you expect? c. Call swapends on a queue containing two elements and output the result along with the contents of the queue using toString. Is the output what you expect? d. Call swapends on a queue containing more than two elements and output the result along with the contents of the queue using toString. Is the output what you expect? ArrayBoundedQueue2Demo: (5 total points) The program must test the ADT to ensure that all added methods function correctly. If any result from the program is incorrect, that means the tested method is not implemented correctly. You may use any of the methods in the queue class for testing. 1) (1 point) Test the toString method by doing the following: a. Use toString and output the status of an empty queue. Is the output what you expect? b. Use toString and output the status of a queue containing one element. Is the output what you expect? c. Use toString and output the status of a queue containing more than two elements. Is the output what you expect? 2) (1 point) Test the space method by doing the following: a. Call space on an empty queue and output the result. Is the output what you expect? b. Call space on a queue containing one element and output the result. Is the output what you expect? c. Call space on a queue containing several elements and output the result. Is the output what you expect? d. Call space on a full queue and output the result. Is the output what you expect? 3) (1 point) Test the remove method by doing the following: a. Call remove on an empty queue, and catch the exception that should occur. You will want to use a try/catch block for this part of code so that the exception does not crash the program. Is the exception correctly thrown? b. Call remove on a non-empty queue to remove less elements than are in the queue (for example, remove 5 elements from a queue containing 10 elements), and then outputting the contents of the queue using toString. Is the output what you expect? 4) (1 point) Test the swapstart method by doing the following: a. Call swapstart on an empty queue and output the result. Is the output what you expect? b. Call swapstart on a queue containing only one element and output the result. Is the output what you expect? C. Call swapstart on a queue containing two elements and output the result along with the contents of the queue using toString. Is the output what you expect? d. Call swapstart on a queue containing more than two elements and output the result along with the contents of the queue using toString. Is the output what you expect? 5) (1 point) Test the swapends method by doing the following: a. Call swapends on an empty queue and output the result. Is the output what you expect? b. Call swapends on a queue containing only one element and output the result. Is the output what you expect? c. Call swapends on a queue containing two elements and output the result along with the contents of the queue using toString. Is the output what you expect? d. Call swapends on a queue containing more than two elements and output the result along with the contents of the queue using toString. Is the output what you expect

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!