the first code which has a class in which I tried implementing the main methods
ID: 3928055 • Letter: T
Question
the first code which has a class in which I tried implementing the main methods needed for a proper queue. Unfortunately, I did not get it to work. I left the store field protected, so you should be able to directly access it from another class in the edu.buffalo.cse116 package. I also wrote the EmptyQueueException class for you to use. Write a JUnit test class whose methods call the enqueue(), dequeue(), or peek() method and verify they correctly update storeand fix the bugs in this class.
first code:
EmptyQueueException class
Explanation / Answer
import junit.framework.TestCase; public class TestQueue extends TestCase { /** The Queue object to use in most test cases. */ private Queue testQueue; /** * Performs the base initial setup for all test cases. */ public void setUp() { this.testQueue = new Queue(5); } /** * Tests the two available constructor methods to verify * correctness. */ public void testConstructor() { try { this.testQueue = new Queue(-1); fail("Expected exception when creating Queue with invalid size."); } catch (Exception ex) { // Expected exception. } this.testQueue = new Queue(); assertEquals("Testing Queue object created with zero size.", 0, this.testQueue.getSize()); this.testQueue = new Queue(3); assertEquals("Testing Queue object created with size set to 3.", 3, this.testQueue.getSize()); } /** * Tests that the dequeue method properly removes the data * stored at the front of the queue. */ public void testDequeue() { try { this.testQueue.dequeue(); fail("Expected QueueException when dequeuing from an empty queue."); } catch (QueueException ex) { // Expected QueueException } catch (Exception ex) { fail("Unexpected exception occurred when dequeuing from an empty queue. Message: " + ex.getMessage()); } try { this.testQueue.enqueue("3"); assertEquals("Testing dequeuing a single object.", "3", this.testQueue.dequeue()); assertEquals("Testing queue is empty after removing last item.", true, this.testQueue.isEmpty()); this.testQueue.enqueue("4"); this.testQueue.enqueue("5"); this.testQueue.enqueue("2"); assertEquals("Testing dequeuing a single object.", "4", this.testQueue.dequeue()); this.testQueue.enqueue("3"); this.testQueue.enqueue("1"); this.testQueue.enqueue("6"); assertEquals("Testing dequeuing a single object.", "5", this.testQueue.dequeue()); } catch (QueueException ex) { fail("Unexpected QueueException occurred while enqueuing in dequeue test."); } } /** * Tests the dequeueAll method to ensure all data is properly * removed when called. */ public void testDequeueAll() { try { this.testQueue.enqueue("1"); this.testQueue.enqueue("2"); this.testQueue.enqueue("3"); this.testQueue.enqueue("4"); this.testQueue.enqueue("5"); } catch (QueueException ex) { fail("Unexpected QueueException occurred during enqueue in dequeueAll test."); } assertEquals("Testing queue is full before calling dequeueAll method.", true, this.testQueue.isFull()); this.testQueue.dequeueAll(); assertEquals("Testing queue is not full after calling dequeueAll method.", false, this.testQueue.isFull()); assertEquals("Testing queue is empty after calling dequeueAll method.", true, this.testQueue.isEmpty()); } /** * Tests the enqueue method to ensure data is added properly * under possible conditions. */ public void testEnqueue() { try { this.testQueue.enqueue("1"); assertEquals("Testing enqueue method after adding one item.", 1, this.testQueue.getCount()); this.testQueue.enqueue("2"); this.testQueue.enqueue("3"); this.testQueue.enqueue("4"); this.testQueue.enqueue("5"); } catch (QueueException ex) { fail("Unexpected QueueException occurred during enqueue test."); } assertEquals("Testing enqueue method that queue is full.", true, this.testQueue.isFull()); try { this.testQueue.enqueue("6"); fail("Expected QueueException when adding to a full queue."); } catch (QueueException ex) { // Expected QueueException } catch (Exception ex) { fail("Unexpected exception occurred when adding to a full queue. Message: " + ex.getMessage()); } } /** * Tests the getCount method to ensure an accurate count * is maintained. */ public void testGetCount() { try { assertEquals("Testing getCount method in empty queue.", 0, this.testQueue.getCount()); this.testQueue.enqueue("1"); assertEquals("Testing getCount method after adding one object.", 1, this.testQueue.getCount()); this.testQueue.enqueue("2"); this.testQueue.enqueue("3"); assertEquals("Testing getCount method.", 3, this.testQueue.getCount()); this.testQueue.enqueue("4"); this.testQueue.enqueue("5"); } catch (QueueException ex) { fail("Unexpected QueueException occurred during enqueue in getCount test."); } assertEquals("Testing getCount method after queue is full.", 5, this.testQueue.getCount()); try { this.testQueue.dequeue(); } catch (QueueException ex) { fail("Unexpected QueueException occurred during dequeue in getCount test."); } assertEquals("Testing getCount method after dequeuing one object.", 4, this.testQueue.getCount()); this.testQueue.dequeueAll(); assertEquals("Testing getCount method after dequeuing all objects.", 0, this.testQueue.getCount()); } /** * Tests the getSize method to ensure that the size is reflected * properly as to how much it can store. */ public void testGetSize() { assertEquals("Testing size of the queue.", 5, this.testQueue.getSize()); this.testQueue = new Queue(); assertEquals("Testing size of the queue.", 0, this.testQueue.getSize()); } /** * Tests the isEmpty method for correctness. */ public void testIsEmpty() { assertEquals("Testing the isEmpty method in an empty queue.", true, this.testQueue.isEmpty()); try { this.testQueue.enqueue("1"); } catch (QueueException ex) { fail("Unexpected QueueException occurred during enqueue in isEmpty test."); } assertEquals("Testing the isEmpty method in after data has been added.", false, this.testQueue.isEmpty()); try { this.testQueue.dequeue(); } catch (QueueException ex) { fail("Unexpected QueueException occurred during dequeue in isEmpty test."); } assertEquals("Testing the isEmpty method in after all data has been dequeued.", true, this.testQueue.isEmpty()); } /** * Tests the isFull method for correctness. */ public void testIsFull() { assertEquals("Testing the isFull method in an empty queue.", false, this.testQueue.isFull()); try { this.testQueue.enqueue("1"); this.testQueue.enqueue("2"); this.testQueue.enqueue("3"); this.testQueue.enqueue("4"); assertEquals("Testing the isFull method just before the queue is full.", false, this.testQueue.isFull()); this.testQueue.enqueue("5"); } catch (QueueException ex) { fail("Unexpected QueueException occurred during enqueue in isFull test."); } assertEquals("Testing the isFull method in a full queue.", true, this.testQueue.isFull()); try { this.testQueue.dequeue(); } catch (QueueException ex) { fail("Unexpected QueueException occurred during dequeue in isFull test."); } assertEquals("Testing the isFull method.", false, this.testQueue.isFull()); } /** * Tests the peek method to ensure data can be retrieved, but * not removed from the Queue. */ public void testPeek() { try { this.testQueue.peek(); fail("Expected QueueException when peeking from an empty queue."); } catch (QueueException ex) { // Expected QueueException } catch (Exception ex) { fail("Unexpected exception occurred when peeking from an empty queue. Message: " + ex.getMessage()); } try { this.testQueue.enqueue("3"); assertEquals("Testing peeking a single object.", "3", this.testQueue.peek()); assertEquals("Testing queue is not empty after peeking last item.", false, this.testQueue.isEmpty()); this.testQueue.enqueue("4"); this.testQueue.enqueue("5"); this.testQueue.enqueue("2"); assertEquals("Testing peeking a single object.", "3", this.testQueue.peek()); this.testQueue.enqueue("1"); } catch (QueueException ex) { fail("Unexpected QueueException occurred while enqueuing in peek test."); } try { assertEquals("Testing peeking a single object.", "3", this.testQueue.peek()); } catch (QueueException ex) { fail("Unexpected QueueException occurred during a valid peek test."); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.