Object Stack ADT Implementation
CS2 Course Summary Slide 22

Stacks and queues are presented as abstract data types, implemented using the Java Vector class, and used in a number of simple applications.

An Object Stack ADT and its implementation are provided in the form of Java code (an interface and classes). Students are asked to study, enter, and test this code. The Vector class of the java.util Package is featured in this implementation.


ADT

/*
 * ObjectStackADT.java
 */

package datatypes.stack;

public interface ObjectStackADT {
   public boolean emptyp();
   public boolean fullp();
   public Object top();
   public void push(Object data) throws ObjectStackOverflowException;
   public Object pop() throws ObjectStackUnderflowException;
   public String toString();   
}

Exception Classes

/*
 * ObjectStackOverflowException.java
 */

package datatypes.stack;

public class ObjectStackOverflowException extends java.lang.Exception {
    
    public ObjectStackOverflowException() {
    }
    
    
    public ObjectStackOverflowException(String msg) {
        super(msg);
    }
}


/*
 * ObjectStackUnderflowException.java
 */

package datatypes.stack;

public class ObjectStackUnderflowException extends java.lang.Exception {
    
    public ObjectStackUnderflowException() {
    }
    
    public ObjectStackUnderflowException(String msg) {
        super(msg);
    }
}

Implementation

/*
 * ObjectStack.java
 */

package datatypes.stack.sequential;

import java.util.*;
import datatypes.stack.*;

public class ObjectStack implements ObjectStackADT {
    
    // instance variables
    private Vector s;
    private int limit;
    
    // constructors
    
    // Use this constructor if you want Java to worry about
    // the stack being full
    
    public ObjectStack()
    {
        this.limit = -1;
        s = new Vector(0);
    }
    
    // Use this constructor if YOU want to worry about
    // the stack being full
    
    public ObjectStack(int limit)
    {
        this.limit = limit;
        s = new Vector(0);
    }
    
    // instance methods
    
    public boolean fullp()
    {
        return ( s.size() == limit );
    }
    
    public boolean emptyp()
    {
        return ( s.size() == 0 );
    }
    
    public Object top()
    {
        return s.elementAt(s.size()-1);
    }
    
    public void push(Object data) throws ObjectStackOverflowException
    {
        if ( fullp() ) {
            throw new ObjectStackOverflowException();
        }
        s.addElement(data);
    }
    
    public Object pop() throws ObjectStackUnderflowException
    {
        Object result = null;
        if ( emptyp() ) {
            throw new ObjectStackUnderflowException();
        } else {
            result = s.elementAt(s.size()-1);
            s.removeElementAt(s.size()-1);
        }
        return result;
    }
    
    private int size()
    {
        return s.size();
    }
    
    public String toString()
    {
        String result = "";
        result = result + "TOP of stack\n";                 
        for ( int i = s.size() - 1; i >= 0; i-- ) {
                result = result + s.elementAt(i).toString() + "\n";
        }
        result = result + "BOTTOM of stack";
        return result;
    }
    
}

Test Program and Demo

/*
 * ObjectStackTest.java
 */

package testers;

import datatypes.stack.*;
import datatypes.stack.sequential.*;

public class ObjectStackTest {
    
    public ObjectStackTest() {
    }
    
    public static void main(String[] args) {
        test_part1();
        test_part2();
        test_part3();
    }

    static private void test_part1()
    {
        System.out.println(">>> Testing unlimited stack constructor, push, pop, and toString");
        ObjectStack  stack = new ObjectStack();
        try {
            stack.push("A");
            stack.push("B");
            stack.push("C");
            stack.pop();
            stack.push("D");
            stack.push("E");
            stack.pop();
            stack.push("F");
            stack.push("G");
            stack.pop();
            stack.push("H");
        } catch ( ObjectStackOverflowException e ) {
            System.out.println("Stack overflow occurred");
            display(stack);
        } catch ( ObjectStackUnderflowException e ) {
            System.out.println("Stack overflow occurred");
            display(stack);
        }
        display(stack);
    }


    static private void test_part2()
    {
        System.out.println(">>> Testing limited stack constructor");
        ObjectStack  stack = new ObjectStack(4);
        System.out.println(">>> Testing overflow handling");
        try {
            stack.push("one");
            stack.push("two");
            stack.push("three");
            stack.push("four");
            stack.push("five");
        } catch ( ObjectStackOverflowException e ) {
            System.out.println("Stack overflow occurred");
            display(stack);
        } 
        System.out.println(">>> Testing underflow handling");
        try {
            stack.pop();
            stack.pop();
            stack.pop();
            stack.pop();
            stack.pop();
            stack.pop();
        } catch ( ObjectStackUnderflowException e ) {
            System.out.println("Stack underflow occurred");
            display(stack);
        }
    }

    static private void test_part3()
    {
        System.out.println(">>> Testing emptyp, fullp, and top");
        ObjectStack  stack = new ObjectStack(3);
        try {
            stack.push("red");
            stack.push("yellow");
            stack.push("blue");
            display(stack);
            if ( stack.emptyp() ) {
                System.out.println("stack is empty");
            } else if ( stack.fullp() ) {
                System.out.println("stack is full");
            }
            System.out.println("top = " + stack.top());
            stack.pop();
            stack.pop();
            stack.pop();
            display(stack);
            if ( stack.emptyp() ) {
                System.out.println("stack is empty");
            } else if ( stack.fullp() ) {
                System.out.println("stack is full");
            }
        } catch ( ObjectStackOverflowException e ) {
            System.out.println("Stack overflow occurred");
            display(stack);
        } catch ( ObjectStackUnderflowException e ) {
            System.out.println("Stack overflow occurred");
            display(stack);
        }
    }

    static private void display(ObjectStack s)
    {
        System.out.println(s.toString());
    }

}


init:
deps-jar:
compile-single:
run-single:
>>> Testing unlimited stack constructor, push, pop, and toString
TOP of stack
H
F
D
B
A
BOTTOM of stack
>>> Testing limited stack constructor
>>> Testing overflow handling
Stack overflow occurred
TOP of stack
four
three
two
one
BOTTOM of stack
>>> Testing underflow handling
Stack underflow occurred
TOP of stack
BOTTOM of stack
>>> Testing emptyp, fullp, and top
TOP of stack
blue
yellow
red
BOTTOM of stack
stack is full
top = blue
TOP of stack
BOTTOM of stack
stack is empty
BUILD SUCCESSFUL (total time: 1 second)