JAVA Suppose you have the following Stack class public class
JAVA: Suppose you have the following Stack class:
public class Stack {
public Stack() { ... }
public String pop() { ... }
public String top() { ... }
public void push( int newItem ) { ... }
public boolean isEmpty() { ... }
}
The actual details of the implementation of the class are unknown, but are functional.
(a) Using these three methods (do not assume that you know anything about the
internal implementation of Stack), write a method with signature
public Stack copy()
that copies the contents of Stack into another Stack and returns it. Calling
copy() should not modify the contents of the Stack at all.
(b) Repeat part (a), but if your solution was recursive, do an iterative implementation
(and vice-versa).
Solution
Please find the required program along with its output. Please see the comments against each line to understand the step.
a)
---------------------------------------
OUTPUT:
Source: [1, 2, 3, 4, 5]
Target: [1, 2, 3, 4, 5]
b)
------------------------------------
OUTPUT:
Source: [1, 2, 3, 4, 5]
Target: [1, 2, 3, 4, 5]
