/** * Class to define a Quack (Stack + Queue) that is implemented with an array used as * a Ring Buffer. * * @author YOUR NAME HERE * Colaboration statement: INSERT IT HERE */ public class RingQuack implements Quack { // Use only the following object fields. You may not add more to the class definition! // (You may add/use local variales inside individual methods) // Maximum number of elements in the QUACK is: public final int MAX_SIZE = 3; // Use a 3 for testing purposes! // Data is stored in this array. You may ONLY store elements in this array. private Object[] store = new Object[MAX_SIZE]; private int head = 0; private int tail = 0; private int numStored = 0; /** * Return the size of the Quack * @return the number of elements in the Quack */ public int size() { return(numStored); } /** * Method to see if the Quack is empty * @return true if the Quack is empty, else false */ public boolean isEmpty() { return(numStored == 0); } /** * Peek at, but don't remove, the head of the Quack * @return the head of the Quack (top) */ public E peek() { // This method is defined for you, yey! // Because we store objects, we must cast them to the type E before returning. // The cast causes the compiler to issue a warning, which we suppress here. // By suppressing this warning, we are claiming that we understand the possible // Trouble we can get into, and have taken steps to prevent it! // (in this case, because all of our push/enqueue methods only accept objects of type // E, we are safe in casting the objects in our "Object list" back to type E ) @SuppressWarnings( "unchecked" ) E retVal = (E) store[head]; return( retVal ); } // ASSIGNMENT STARTS HERE: /** * Add an element onto the tail of the Quack * Throws a RuntimeException if the array is out of room * @param element the element to add to the end of the Quack */ public void enqueue(E element) throws java.lang.RuntimeException { if (numStored == MAX_SIZE) { throw new java.lang.RuntimeException("Array out of Memory!"); } // YOUR CODE GOES HERE! } /** * Push an element onto the top (head) of the Quack * Throws a RuntimeException if the array is out of room * @param element the element to add to the top of the Quack */ public void push(E element) throws java.lang.RuntimeException { // YOUR CODE GOES HERE!! } /** * Pop an object from the Quack * @return the head (top) of the Quack and * remove it from the Quack - If empty, return null */ public E pop() { // YOUR CODE GOES HERE } } /** * Dequeue an object from the Quack (Note: Same behavior as Pop!) * * @return the head (top) of the Quack and * remove it from the Quack - Returns null if the Quack is empty */ public E dequeue() { // YOUR CODE GOES HERE } // YOU ARE DONE WITH THE ASSIGNMENT. // This main method will test your QUACK! public static void main( String [] args ) { RingQuack rq = new RingQuack(); if (rq.isEmpty()) { System.out.println("isEmpty works when empty...Good!"); } else {System.out.println("Failure!");} if ( rq.pop() == null && rq.dequeue() == null) { System.out.println("pop and dequeue work when empty...Good!"); } else {System.out.println("Failure!");} System.out.println("Adding 2 elements!"); rq.push("CS1316"); rq.push("Hello "); if (rq.size() == 2) { System.out.println("size reports that Quack has 2 elements...Good!"); } else {System.out.println("Failure!");} System.out.println("Should say: Hello CS 1316: " + rq.pop() + rq.pop()); if (rq.size() == 0) { System.out.println("size reports that Quack has 0 elements...Good!"); } else {System.out.println("Failure!");} System.out.println("Adding 2 MORE elements! (Watch out for wraparound!)"); rq.enqueue("second "); rq.enqueue("test"); if (rq.size() == 2) { System.out.println("size reports that Quack has 2 elements...Good!"); } else {System.out.println("Failure!");} System.out.println("Should say: second test: " + rq.pop() + rq.pop()); if (rq.size() == 0) { System.out.println("size reports that Quack has 0 elements...Good!"); } else {System.out.println("Failure!");} System.out.println("Adding 3 elements! (Can it use the whole array?)"); rq.enqueue("third "); rq.enqueue("test"); rq.push("the "); System.out.println("Should say: the third test: " + rq.dequeue() + rq.dequeue() + rq.peek() ); if (rq.size() == 1) { System.out.println("size reports that we left 1 element on the quack...Good!"); } else {System.out.println("Failure!");} rq.push("two!"); rq.enqueue("Three!"); System.out.println("Trying to overflow the array! Your code SHOULD throw an exception!"); try { rq.enqueue("Four!"); // Should be thrown here... } catch (RuntimeException e) { System.out.println("Exception thrown:" + e); } System.out.println("Should say: two!two!testThree!: " +rq.peek() + rq.dequeue() + rq.pop() + rq.dequeue()); } // end public void main } // End class RingQuack