Question: DOTODOplease class EmptyQueueE extends Exception {} abstract class Queue { abstract void enqueue (E elem); abstract void dequeue () throws EmptyQueueE; abstract E getFront ()

DO"TODO"please class EmptyQueueE extends Exception {} abstract class Queue { abstract void enqueue (E elem); abstract void dequeue () throws EmptyQueueE; abstract E getFront () throws EmptyQueueE; } // ---------------------------------------------------------------------- class SlowQueue extends Queue { // Our queue is represented by an object of our Stack class in Stack.java // So, for clarification, whenever the method docs ask to "modify our queue", // we are actually modifying this object. // Read Stack.java please:) private Stack stack; /** * This initializes our private var "stack" to be a new EmptyS (see -Stack.java-) */ SlowQueue () { stack = new EmptyS<>(); } /** * Although the method itself does not return anything, enqueue modifies our queue by adding given element to the * end. Hint: look in -Stack.java- * * @param elem a generic element to be added to the queue */ void enqueue(E elem) { // TODO } /** * This method should remove the first element of our queue. Hint: look in -Stack.java-. * * @throws ????? (throws what and why) */ void dequeue() throws EmptyQueueE { // TODO } /** * getFront returns an element of **generic type**. * This method returns the first element of our queue by using -Stack.java-'s getFront() * * @return the removed first element of out queue, E * @throws ????? (throws what and why) */ E getFront() throws EmptyQueueE { try { return stack.getTop(); } catch (EmptyStackE e) { throw new EmptyQueueE(); } } } 

____________________________________________________________Stack.java-----------------------------------

class EmptyStackE extends Exception {} abstract class Stack { abstract boolean isEmpty (); // O(1) abstract int size(); // O(1) abstract Stack push (E elem); // O(1) abstract Stack pop() throws EmptyStackE; // O(1) abstract E getTop() throws EmptyStackE; // O(1) abstract Stack addLast (E elem); // O(N) abstract E getLast() throws EmptyStackE; // O(N) } class EmptyS extends Stack { boolean isEmpty() { return true; } int size() { return 0; } Stack push(E elem) { return new NonEmptyS<>(elem, this); } Stack pop() throws EmptyStackE { throw new EmptyStackE(); } E getTop() throws EmptyStackE { throw new EmptyStackE(); } Stack addLast(E elem) { return new NonEmptyS<>(elem, this); } E getLast() throws EmptyStackE { throw new EmptyStackE(); } public String toString () { return ""; } } class NonEmptyS extends Stack { private final E top; private final Stack rest; private final int size; NonEmptyS (E top, Stack rest) { this.top = top; this.rest = rest; this.size = rest.size() + 1; } boolean isEmpty() { return false; } int size() { return size; } Stack push(E elem) { return new NonEmptyS<>(elem, this); } Stack pop() { return rest; } E getTop() { return top; } Stack addLast(E elem) { return new NonEmptyS<>(top, rest.addLast(elem)); } E getLast() throws EmptyStackE { if (rest.isEmpty()) { return top; } else { return rest.getLast(); } } public String toString () { return String.format("%s; %s", top, rest.toString()); } }

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!