Question: // GenericStack.java // Test Class class TestGenericStack { public static void main(String[] args) { GenericStack stack1 = new GenericStack(); stack1.push(London); stack1.push(Paris); stack1.push(Berlin); log(stack1); log(stack1.pop()); log(stack1.pop());

// GenericStack.java
// Test Class class TestGenericStack { public static void main(String[] args) { GenericStack
public class GenericStack
public int getSize() { return list.size(); }
// peek at the next item public E peek() { return list.get(getSize() - 1); }
// push an item onto the stack public void push(E o) { list.add(o); }
// pop an item from the stack public E pop() { E o = list.get(getSize() - 1); list.remove(getSize() - 1); return o; }
public boolean isEmpty() { return list.isEmpty(); }
public String toString() { return "stack: " + list.toString(); } }
A queue is an abstract data type similar to the stack as we discussed in class, but it is a FIFO (first-in, first-out) structure as compared to the stack, which is LIFO (last-in, first-out). Think of it as waiting in line for a movie ticket (the British word for line is "queue"). As the line grows, the first person in the line is "removed" to buy a ticket, while the end of the line continues to grow. So the first person in line is the first person to leave, the second in line is second to leave, and so on. Modify the Generic Stack class provided in this unit's course content (Generic Stack.java) to implement a generic queue. You may use the original source file and just make necessary changes (including the class names). The push and pop methods will need to be changed to enque (adds an element to the end of the queue) and deque (removes an element from the front of the queue). You can retain the getSize, peek, isEmpty, and to String methods from the Generic Stack class; some of them will need to be tweaked, some can be used as is. Change the "Test Generic Stack" class in that file to "TestGenericQueue", here is the main method to use in your TestGenericQueue class which will test your Generic Queue class: public static void main(String[] args) { GenericQueue queuel = new GenericQueue(); queuel. ..enque ("London"); queuel.enque ("Paris"); queuel.enque ("Berlin"); log (queuel); log (queuel.deque ()); log (queuel.deque ()); log (queuel.deque ()); GenericQueue queue2 = new GenericQueue(); queue.enque (1); queue2.enque (2); queue. .enque (3); log (queue2); log (queue2. deque ()); log (queue2. deque ()); log (queue2.deque ()); } Expected Output -- note that elements are removed in the same order as they are inserted (FIFO), compared to the Test Generic Stack program output which removes them in reverse order (LIFO): queue: (London, Paris, Berlin] London Paris Berlin queue:[1,2,3] 1 2 3
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
