Question


Description Testing is very important in large software projects, and it is common to test individual classes and methods in

Moditying or removing the line below the BUG Comment. You can add and remove the bug to make sure your test detects its pres

AQueue.java

class AQueue implements Queue {
private E queueArray[]; // Array holding queue elements
private static final int DEFAULT_SIZE = 10;
private int maxSize; // Maximum size of queue
private int front; // Index of front element
private int rear; // Index of rear element

// Constructors
@SuppressWarnings("unchecked") // Generic array allocation
AQueue(int size) {
//BUG #1: maxSize = size
maxSize = size+1; // One extra space is allocated
rear = 0; front = 1;
queueArray = (E[])new Object[maxSize]; // Create queueArray
}
AQueue() { this(DEFAULT_SIZE); }

// Reinitialize
public void clear() { rear = 0; front = 1; }

// Put "it" in queue
public boolean enqueue(E it) {
//BUG #2: remove or comment out the line below this comment
if (((rear+2) % maxSize) == front) return false; // Full
rear = (rear+1) % maxSize; // Circular increment
queueArray[rear] = it;
return true;
}

// Remove and return front value
public E dequeue() {
if (length() == 0) return null;
E it = queueArray[front];
//BUG #3: front = front+1
front = (front+1) % maxSize; // Circular increment
return it;
}

// Return front value
public E frontValue() {
//BUG #4: remove or comment out the line below this comment
if (length() == 0) return null;
return queueArray[front];
}

// Return queue size
public int length() { return ((rear+maxSize) - front + 1) % maxSize; }
  
//Tell if the queue is empty or not
//BUG #5: public boolean isEmpty() { return front - rear == 1; }
public boolean isEmpty() { return front == (rear + 1) % maxSize; }
}

Queue.java

public interface Queue { // Queue class ADT
// Reinitialize queue
public void clear();

// Put element on rear
public boolean enqueue(E it);

// Remove and return element from front
public E dequeue();

// Return front element
public E frontValue();

// Return queue size
public int length();
  
//Tell if the queue is empty or not
public boolean isEmpty();
}

TestQueue.java (Starter Code)

/**
* @author
*/

public class TestQueue
{
public static void main(String[] args)
{
System.out.println("Bug 4 test: " + (testBug4() ? "passed" : "failed"));
}

public static boolean testBug1()
{
return false;
}

public static boolean testBug2()
{
return false;
}

public static boolean testBug3()
{
return false;
}

/**
* This bug will allow frontValue to return something from the array even if the
* queue is empty.
*
* Unless the queue contains another bug (and we're allowed to assume it does
* not in this assignment), this will not cause an exception. When the queue is
* created, the array will initially contain nothing but null. In order to
* detect this bug, we need to fill every spot in the array and then empty the
* queue. If front value returns something other than null, then we will know
* it's accessing the array.
*/
public static boolean testBug4()
{
AQueue queue = new AQueue(4);

for (int i = 0; i < 5; i++)
{
queue.enqueue("A");
queue.dequeue();
}

return queue.frontValue() == null;
}

public static boolean testBug5()
{
return false;
}
}

In TestQueue is should return true if AQueue passes by not containing any bugs and return false if it contains any bugs. It says it in the first two pictures.

Description Testing is very important in large software projects, and it is common to test individual classes and methods in isolation [referred to as unit testing). Unit tests are effective for ensuring a small part of the program meets its specifications, and bugs detected by unit tests tend to be easy to locate and fix (we know they're in the class or method that was tested]. This activity is meant to provide some practice writing tests, as well as improve your understanding of the inner workings of the circular queue implementation in the text. Rather than writing comprehensive unit tests, you will instead be writing tests for a set of 4 bugs in the AQueue class (there are 5, but one test has already been written for you as an example). When writing each method, you may also assume that no bugs exist except for the bug you are testing. This is not normally a realistic assumption, but it allows us to use a much simpler testing scenario in this introduction. The starter code for this lab contains a modified version of the AQueue from Chapter 9, the Queue interface, and an outline for the test class. If you look at the AQueue code, you should find 5 comments starting with the word "BUG. Each of these comments shows how to create a bug thet must be detected by one of the tests This involves modifying or removing the line below the "BUG comment. You can add and remove the bug to make sure your test detects its presence or absence. In the TestQueue class you will find 5 static methods, one for each of the bugs in AQueue. Your job is to complete the 4 unfinished tests. Each test should return true if the AQueue passes (does not contain the bug) or false if it fails (does contain the bug). Grading Submit TestQueue.java. This assignment will be graded by Mimir's automated tests. Each test will run one of your methods on two versions of the AQueue: one with the bug and one without the bug. Make sure each method is testing for the appropriete bug, and thet it returns true if the bug is not present Advice When beginning to write a test for one of the bugs, it can be helpful to answer the following questions 1. What is the purpose of the original line of code? 2 How does the bug change the queue's behavior? 3. How can I cause the queue to exhibit this behavior? We can look at the bug #4 test for an example of this process. If we answer the above questions for bug #4 1. The original line of code prevents the queue from accessing the array when the queue is empty. The array can contain garbage data, but if the queue is empty then the frontValuel method should always return null (the queue has no front value when empty 2. Removing that line of code allows frontValue to return garbage data from the array when the queue is empty 3. If we fill the entire array with non-null values and dequeue each value, then the queue will be empty, but each space in the array contains a value other than null. if we then call frontValue, it will access the array alinahas is and nnn
Moditying or removing the line below the "BUG Comment. You can add and remove the bug to make sure your test detects its presence or absence. In the TestQueue class you will find 5 static methods, one for each of the bugs in AQueue. Your job is to complete the 4 unfinished tests. Each test should return true if the AQueue passes (does not contain the buglc false if it fails (does contain the bug). Grading Submit TestQueue.java. This assignment will be graded by Mimir's automated tests. Each test will run one of your methods on two versions of the AQueue: one with the bug and one without the bug. Make sure each method is testing for the appropriate bug, and that it returns true if the bug is not present Advice When beginning to write a test for one of the bugs, it can be helpful to an: answer the following questions: 1. What is the purpose of the original line of code? 2 How does the bug change the queue's behavior? 3. How can I cause the queue to exhibit this behavior? We can look at the bug # 4 test for an example of this process. If we answer the above questions for bug # 4: 1. The original line of code prevents the queue from accessing the array when the queue is empty. The array can contain garbage data, but if the queue is empty then the frontValue(] method should always return null (the queue has no front value when empty). 2 Removing that line of code allows frontValue to return garbage data from the array when the queue is empty. 3. If we fill the entire array with non-null values and dequeue each value, then the queue will be empty, but each space in the array contains a value other than null. If we then call frontValue, it will access the array and return a value that isn't null. With the answers to these questions, it's much easier to write our test. We make a new queue, enqueue and dequeue an arbitrary value (anything but null) until the entire array has been overwritten, and then check whether frontValue) returns null. If it does supposed to be empty, indicating that bug # 4 is present not return null, then it is accessing the array while the queue is
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please do rate the answer if it was helpful. Thank you
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i

import java.util.NoSuchElementException;
public class ArrayQueue<T> implements QueueInterface<T> {
// Do not add new instance variables.
private T[] backingArray;
private int front;
private int size;
/**
* Constructs a new ArrayQueue.
*/
public ArrayQueue() {
backingArray = (T[])new Object[INITIAL_CAPACITY];
front = 0;
size = 0;
}
/**
* Dequeue from the front of the queue.
*
* Do not shrink the backing array.
* If the queue becomes empty as a result of this call, you should
* explicitly reset front to 0.
*
* You should replace any spots that you dequeue from with null. Failure to
* do so can result in a loss of points.
*
* See the homework pdf for more information on implementation details.
*
* @see QueueInterface#dequeue()
*/
/**
* Dequeue from the front of the queue.
*
* This method should be implemented in O(1) time.
*
* @return the data from the front of the queue
* @throws java.util.NoSuchElementException if the queue is empty
*/
@Override
public T dequeue() {
if(isEmpty())
throw new NoSuchElementException();
T val = backingArray[front];
backingArray[front] = null;
front++;
size--;
if(isEmpty())
front = 0;
return val;
}
/**
* Add the given data to the queue.
*
* If sufficient space is not available in the backing array, you should
* regrow it to double the current length. If a regrow is necessary,
* you should copy elements to the front of the new array and reset
* front to 0.
*
* @see QueueInterface#enqueue(T)
*/
/**
* Add the given data to the queue.
*
* This method should be implemented in (if array-backed, amortized) O(1)
* time.
*
* @param data the data to add
* @throws IllegalArgumentException if data is null
*/
@Override
public void enqueue(T data) {
int last = front + size;
//expand the array if the new data can't fit
if(last >= backingArray.length)
{
T[] temp = (T[]) new Object[2*backingArray.length];
for(int i = 0, j = front; i < size; i++, j++)
temp[i] = backingArray[j];
backingArray = temp;
front = 0;
last = size;
}
backingArray[last] = data;
}
@Override
public T peek() {
return backingArray[front];
}
@Override
public boolean isEmpty() {
// DO NOT MODIFY THIS METHOD!
return size == 0;
}
@Override
public int size() {
// DO NOT MODIFY THIS METHOD!
return size;
}
/**
* Returns the backing array of this queue.
* Normally, you would not do this, but we need it for grading your work.
*
* DO NOT USE THIS METHOD IN YOUR CODE.
*
* @return the backing array
*/
public Object[] getBackingArray() {
// DO NOT MODIFY THIS METHOD!
return backingArray;
}
}

Add a comment
Know the answer?
Add Answer to:
AQueue.java class AQueue implements Queue { private E queueArray[]; // Array holding queue elements private static...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Are based on the following Queue class code segment class QueueFull {/* Empty exception class */};...

    Are based on the following Queue class code segment class QueueFull {/* Empty exception class */}; Class Queue Empty {/* Empty exception class */}; struct Node//Node structure int data;//Holds an integer Node* next;//Pointer to next node in the queue}; Class Queue//Linked node implementation of Queue ADT {Private: Node* front;//Pointer to front node of queue Node* rear;//pointer to last node of queue Public: Queue ()://default constructor initializes queue to be empty -Queue ();//Deallocates all nodes in the queue Void Add (int...

  • PROBLEM- void CBQueue::printF2B( ) If the queue is empty, the method should print “Queue is empty”,...

    PROBLEM- void CBQueue::printF2B( ) If the queue is empty, the method should print “Queue is empty”, otherwise, this method should print the items in the queue starting at the front of the queue and proceeding to the rear of the queue. The items should be printed one per line. Now that this method is written, you can do a more thorough job of testing enqueue( ). You will want to call printF2B( ) and printB2F( ) after implementing each method...

  • e. public class Queue { // Uses the correct Stack class from ME2 Ex 19 private Stack mStack; public Queue() { setStack(new Stack()); } public Queue enqueue(E pData) { getStack().push(pData); return t...

    e. public class Queue { // Uses the correct Stack class from ME2 Ex 19 private Stack mStack; public Queue() { setStack(new Stack()); } public Queue enqueue(E pData) { getStack().push(pData); return this; } public E dequeue() { return getStack().pop(); } public E peek() { return getStack.peek(); } private Stack getStack() { return mStack; } private void setStack(Stack pStack) { mStack = pStack; } } f. public class Queue extends Stack { // Uses the correct Stack class from ME2 Ex...

  • Step 3 Develop the following class: Class Name: ImprovedQueue<T> Access Modifier: public Implements: QueueInterface<T> Instance variables...

    Step 3 Develop the following class: Class Name: ImprovedQueue<T> Access Modifier: public Implements: QueueInterface<T> Instance variables Name: front Access modifier: private Data type: QueueNode<T> Constructors: Name: ImprovedQueue Access modifier: public Parameters: none (default constructor) Task: sets the value of front to null Methods Name: isEmpty Access modifier: public Parameters: none Return type: boolean Task: returns true if the front is equal to null; otherwise return false Name: dequeue                           Access modifier: public Parameters: none Return type: T (parameterized type) Task: makes...

  • Collect/finish the Java code (interface and the complete working classes) from lecture slides for the for...

    Collect/finish the Java code (interface and the complete working classes) from lecture slides for the for the following ADT: 3) Queue ADT that uses an array internally (call it AQueue) Make sure you keep the same method names as in the slides (automatic testing will be performed)! Make sure your classes implement the corresponding interfaces. Put your classes in a package called cse11. Try to make the code robust and try to use generics. The Queue ADT The Queue ADT...

  • (C++) (VISUAL STUDIO) Circular Queue is a linear data structure in which the operations are performed...

    (C++) (VISUAL STUDIO) Circular Queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. In a normal Queue, we can insert elements until queue becomes full. But once queue becomes full, we cannot insert the next element even if there is a space in front of queue. Efficiently implement a queue class using a circular...

  • Complete the following code. /** Insert an item at the rear of the queue. post: item...

    Complete the following code. /** Insert an item at the rear of the queue. post: item is added to the rear of the queue. @param item The element to add @return true (always successful) */ public boolean ____(E item) {    // Check for empty queue.    if (front == null) {   rear = new Node<E>(item);   front = rear;    } else {   // Allocate a new node at end, store item in it, and   // link it to old end of queue....

  • Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds value...

    Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds values of double data type. 1.) Create a program that produces the following output OUTPUT: Q Quit Enter your choice: e Enter an item: 1.1 E Enqueue D Dequeue s-show queue ← showMenuO function called in main) OQuit // screen clears-.. continue enqueuing.screen clearing with each iteration Enter your choice: e Queue is full. E Enqueue D Dequeue s Show queue 0...

  • use intellij idea main java Step 3 Develop the following class: Class Name: ImprovedQueue<T> Access Modifier:...

    use intellij idea main java Step 3 Develop the following class: Class Name: ImprovedQueue<T> Access Modifier: public Implements: QueueInterface<T> Instance variables Name: front Access modifier: private Data type: Queue Node<T> Constructors: Name: ImprovedQueue Access modifier: public Parameters: none (default constructor) Task: sets the value of front to null Methods Name: isEmpty Access modifier: public Parameters: none Return type: boolean Task: returns true if the front is equal to null; otherwise return false Name: dequeue Access modifier: public Parameters: none Return...

  • Java/Queues ** Task: Write a JUnit test that shows a failure in some part of the...

    Java/Queues ** Task: Write a JUnit test that shows a failure in some part of the ADT -----ArrayQueue.java------- public class ArrayQueue {    private static final int INITIAL_CAPACITY = 2; // to permit easier testing    private Object[] contents;    private int front, rear;       /**    * Create an empty queue with an initial capacity.    */    public ArrayQueue() {        contents = new Object[INITIAL_CAPACITY];    }       /**    * Add an element to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT