import java.util.Stack; import java.util.Random; public class StackSharing { public static void main(String [] args){ // create a new stack of strings Stack stack = new Stack(); // push some strings onto the stack stack.push("apple"); stack.push("banana"); stack.push("kiwi"); // Java allows you to print the entire stack System.out.println(stack); System.out.println(stack.empty()); Stack stackToo = new Stack(); stackToo.push("tomato"); stack.pop(); stackToo = stack; stack.pop(); System.out.println("Before m:"); System.out.println(stack); System.out.println(stackToo); Stack newStack = m(stack); System.out.println(stack); System.out.println(stackToo); System.out.println(newStack); newStack.pop(); System.out.println(stack); System.out.println(stackToo); System.out.println(newStack); Stack newStack1 = mm(stack); newStack1.pop(); System.out.println(stack); System.out.println(newStack1); } public static Stack m(Stack s) { s.push("pear"); return s; } public static Stack mm(Stack s) { Stack newStack = new Stack(); String str = s.pop(); s.push(str); newStack.push(str); return newStack; } }