//******************************************************************** // StackViewer.java Author: Sophie Quigley // // Class StackViewer shows a transparent stack and its contents. // This class is intended to be subclassed. // //******************************************************************** public class StackViewer { // The stack viewer keeps a reference to the transparent stack inside its visible stack. // It will interact directly with it to get stack elements for its display. protected TransparentStack stack; // When a new stackviewer for a visible stack is created, // it is added to the list of viewers for that visible stack public StackViewer (VisibleStack stack) { stack.addViewer(this); } // After a viewer has been added to a visible stack's list of viewers,it is initialized. // At the very least the transparent stack reference is initialized. // This is why initialize must be invoked once before the other methods, // and why the functionality below should be duplicated if this method is overridden. public void initialize (TransparentStack stack) { this.stack = stack; } // Display which displays the stack should be overridden in the subclasses. public void display() { int size = stack.currentSize(); if (size == 0) System.out.println("The stack is empty"); else System.out.println("The stack has "+size+ " elements. The top is "+stack.top().toString()); } // ShowTop simply shows the top of the stack. public void showTop() { if (stack.empty()) System.out.println("There is no top because the stack is empty"); else System.out.println("The top of the stack is "+stack.top().toString()); } }