import java.util.Vector; import java.awt.Point; /* By Futrelle for COM1370 6/30/01 A stack of Point objects. Done according to API for Java 1.1; */ /** * Is a stack that holds Point objects.
* Includes push(), pop() and empty(). * @version 1.0 June 30, 2001. * @author R. P. Futrelle for COM1370 **/ public class PointStack { public static void main(String[] args) { PointStack ps = new PointStack(); System.out.println("pushing Point(1,2)"); ps.push(new Point(1,2)); System.out.println("pushing Point(3,4)"); ps.push(new Point(3,4)); Point p; p = ps.pop(); System.out.println("Popping, x and y are: " + p.x + " " + p.y); p = ps.pop(); System.out.println("Popping, x and y are: " + p.x + " " + p.y); System.out.println("Should empty (true) and null now: " + ps.empty() + " " + ps.pop()); } // main() private Vector stack; PointStack() { stack = new Vector(); } /** * True initially or if all added items have been popped. */ public boolean empty() { return (stack.size() == 0); } /** * Adds a Point to the top of the stack. */ public void push(Point p){ stack.addElement(p); } /** * Removes and returns the Point on the top of the stack, * or null if the stack is empty. */ public Point pop() { if(stack.size() == 0) return null; Point p = (Point) stack.lastElement(); stack.removeElementAt(stack.size() - 1); return p; } }