001package algs13;
002import stdlib.*;
003public class XFixedCapacityStack<T> {
004        private T[] a; // holds the items
005        private int N; // number of items in stack
006
007        @SuppressWarnings("unchecked")
008        public XFixedCapacityStack (int capacity) {
009                this.a = (T[]) new Object[capacity]; // no generic array creation
010                this.N = 0;
011        }
012        public int size ()        { return N; }
013        public boolean isEmpty () { return (N == 0); }
014        public void push (T item) {
015                if (item == null) throw new IllegalArgumentException ();
016                if (N >= a.length) throw new RuntimeException ("Stack full");
017                a[N] = item;
018                N++;
019        }
020        public T pop () {
021                if (N <= 0) throw new RuntimeException ("Stack empty");
022                N--;
023                T result = a[N];
024                a[N] = null;
025                return result;
026        }
027
028        public static void main (String[] args) {
029                //Trace.showObjectIdsRedundantly (true);
030                //Trace.showBuiltInObjects (true);
031                //Trace.showBuiltInObjectsVerbose (true);
032                Trace.drawStepsOfMethod ("main");
033                Trace.run ();
034                
035                XFixedCapacityStack<Integer> s1 = new XFixedCapacityStack<> (5);
036                XFixedCapacityStack<String> s2 = new XFixedCapacityStack<> (3);
037                s1.push (11);
038                s1.push (21);
039                s1.push (31);
040
041                //s2.push (41);
042                s2.push ("duck");
043                s2.push ("goose");
044        }
045}