Stack ADT in Java

Java

Program to design an interface

for Stack ADT.and implement Stack ADT using both Array and Linked List.
public class ArrayStack implements Stack {

        private Object[] theArray;
        private int topOfStack;
        private static final int DEFAULT_CAPACITY = 10;

        /**
         * Construct the stack.
         */
        public ArrayStack() {
            theArray = new Object[DEFAULT_CAPACITY];
            topOfStack = -1;
        }

        /**
         * Test if the stack is logically empty.
         *
         * @return true if empty, false otherwise.
         */
        public boolean isEmpty() {
            return topOfStack == -1;
        }

        /**
         * Make the stack logically empty.
         */
        public void makeEmpty() {
            topOfStack = -1;
        }

        /**
         * Get the most recently inserted item in the stack. * Does not alter
         * the stack.
         *
         * @return the most recently inserted item in the stack.
         * @throws UnderflowException if the stack is empty.
         */
        public Object top() {
            if (isEmpty()) {
                throw new UnderflowException("ArrayStack top");
            }
            return theArray[ topOfStack];
        }

        /**
         * Remove the most recently inserted item from the stack.
         *
         * @throws UnderflowException if the stack is empty.
         */
        public void pop() {

            if (isEmpty()) {
                throw new UnderflowException("ArrayStack pop");
            }
            topOfStack--;
        }

        /**
         * Return and remove the most recently inserted item * from the stack.
         *
         * @return the most recently inserted item in the stack.
         * @throws Underflow if the stack is empty.
         */
        public Object topAndPop() {
            if (isEmpty()) {
                throw new UnderflowException("ArrayStack topAndPop");
            }
            return theArray[ topOfStack--];
        }

        /**
         * Insert a new item into the stack.
         *
         * @param x the item to insert.
         */
        public void push(Object x) {
            if (topOfStack + 1 == theArray.length) {
                doubleArray();
            }
            theArray[ ++topOfStack] = x;
        }

        /**
         * Internal method to extend theArray.
         */
        private void doubleArray() {
            Object[] newArray;

            newArray = new Object[theArray.length * 2];
            for (int i = 0; i < theArray.length; i++) {
                newArray[ i] = theArray[ i];
            }
            theArray = newArray;
        }
    }
//ListStack class 
// 
// CONSTRUCTION: with no initializer 
// 
// ******************PUBLIC OPERATIONS********************* 
// void push( x ) 
// void pop( ) 
// Object top( ) 
// Object topAndPop( ) 
// boolean isEmpty( ) 
// void makeEmpty( ) 
    --> Insert x 
--> Remove most recently inserted item --> Return most recently inserted item --> Return and remove most recent item 
--> Return true if empty ;
    else false 
--> Remove all items // ******************ERRORS******************************** 
            // top, pop, or topAndPop on empty stack 
            /**
             * List-based implementation of the stack.
             */

    public class LinkedListStack implements Stack {

        /**
         * Construct the stack.
         */
        public LinkedListStack() {
            topOfStack = null;
        }

        /**
         * Test if the stack is logically empty.
         *
         * @return true if empty, false otherwise.
         */
        public boolean isEmpty() {
            return topOfStack == null;
        }

        /**
         * Make the stack logically empty.
         */
        public void makeEmpty() {
            topOfStack = null;
        }

        /**
         * Insert a new item into the stack.
         *
         * @param x the item to insert.
         */
        public void push(Object x) {
            topOfStack = new ListNode(x, topOfStack);
        }

        /**
         * Remove the most recently inserted item from the stack.
         *
         * @throws UnderflowException if the stack is empty.          *
         */
        public void pop() {
            if (isEmpty()) {
                throw new UnderflowException("ListStack pop");
            }
            topOfStack = topOfStack.next;
        }

        /**
         * Get the most recently inserted item in the stack. * Does not alter
         * the stack.
         *
         * @return the most recently inserted item in the stack.
         * @throws UnderflowException if the stack is empty.
         */
        public Object top() {
            if (isEmpty()) {
                throw new UnderflowException("ListStack top");
            }
            return topOfStack.element;
        }

        /**
         * Return and remove the most recently inserted item * from the stack.
         *
         * @return the most recently inserted item in the stack.
         * @throws UnderflowException if the stack is empty.
         */
        public Object topAndPop() {
            if (isEmpty()) {
                throw new UnderflowException("ListStack topAndPop");
            }

            Object topItem = topOfStack.element;
            topOfStack = topOfStack.next;
            return topItem;
        }
        private ListNode topOfStack;
    }

    public class ListNode {

        public Object element;
        public ListNode next;

// Constructors 
        public ListNode(Object theElement) {
            this(theElement, null);
        }

        public ListNode(Object theElement, ListNode n) {
            element = theElement;

            next = n;
        }
    }

    public interface Stack {

        /**
         * Insert a new item into the stack.
         *
         * @param x the item to insert.
         */
        void push(Object x);

        /**
         * Remove the most recently inserted item from the stack.
         *
         * @exception UnderflowException if the stack is empty.
         */
        void pop();

        /**
         * Get the most recently inserted item in the stack. * Does not alter
         * the stack.
         *
         * @return the most recently inserted item in the stack.
         * @exception UnderflowException if the stack is empty.
         */
        Object top();

        /**
         * Return and remove the most recently inserted item * from the stack.
         *
         * @return the most recently inserted item in the stack.
         * @exception UnderflowException if the stack is empty.
         */
        Object topAndPop();

        /**
         * Test if the stack is logically empty.
         *
         * @return true if empty, false otherwise.
         */
        boolean isEmpty();

        /**
         * Make the stack logically empty.
         */
        void makeEmpty();
    }

    public class StackTester {

        public static void main(String[] args) {



            System.out.println("******************************************");
            System.out.println(
            "Stack using Array & Linked List 
example
            "); 


System.out.println("******************************************");

            ArrayStack arrayStack = new ArrayStack();
            arrayStack.push(new String("a"));
            arrayStack.push(new String("b"));
            arrayStack.push(new String("c"));
            System.out.println("Stack[using array] elements -> a, b, c");
            System.out.println("Stack LIFO and POP -> " + arrayStack.topAndPop());
            System.out.println("Stack LIFO -> " + arrayStack.top());
            arrayStack.pop();
            try {
                arrayStack.pop();
                arrayStack.topAndPop();
            } catch (RuntimeException rte) {
                System.err.println("Exception occured while POP operation is happened on Stack[by using array]");
            }
            System.out.println("\n\n******************************");
            System.out.println("Stack using Linked List example");
            System.out.println("******************************");

            LinkedListStack linkedListStack = new LinkedListStack();
            linkedListStack.push(new Integer(10));
            linkedListStack.push(new Integer(20));
            linkedListStack.push(new Integer(30));
            linkedListStack.push(new Integer(40));
            System.out.println(
            "Stack[using linked list] elements -> 
10, 20, 30, 40"); 
System.out.println("Stack TOP ->" + linkedListStack.top());
            linkedListStack.pop();
            System.out.println("Stack TOP after POP ->" + linkedListStack.top());
            linkedListStack.pop();
            linkedListStack.pop();
            linkedListStack.pop();
            try {
                linkedListStack.pop();
            } catch (RuntimeException rte) {
                System.err.println(
                "Exception occured while POP 
operation is happened on Stack[by 
                using linked list
            
        
    

    ]"); 
}
 
}
}


/** 
* Exception class for access in empty containers * such as stacks, queues, and priority queues. 
* 
*/ 
public class UnderflowException extends RuntimeException {

        /**
         * Construct this exception object.
         *
         * @param message the error message.
         */
        public UnderflowException(String message) {
            super(message);
        }
    }