CSC300: XFixedCapacityStack [10/15] Previous pageContentsNext page

file:XFixedCapacityStack.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package algs13;

import stdlib.*;

public class XFixedCapacityStack<T> {
  private final T[] a; // holds the items
  private int N; // number of items in stack

  @SuppressWarnings("unchecked")
  public XFixedCapacityStack (int capacity) {
    this.a = (T[]) new Object[10]; // no generic array creation
    this.N = 0;
  }
  public int size ()        { return N; }
  public boolean isEmpty () { return (N == 0); }
  public void push (T item) {
    a[N] = item;
    N++;
  }
  public T pop () {
    N--;
    T result = a[N];
    a[N] = null;
    return result;
  }

  public static void main (String[] args) {
    Trace.showObjectIdsRedundantly (true);
    Trace.showBuiltInObjects (true);
    //Trace.showBuiltInObjectsVerbose (true);
    Trace.drawStepsOfMethod ("main");
    Trace.run ();
    
    XFixedCapacityStack<Integer> s1 = new XFixedCapacityStack<> (5);
    XFixedCapacityStack<String> s2 = new XFixedCapacityStack<> (3);
    s1.push (11);
    s1.push (21);
    s1.push (31);

    //s2.push (41);
    s2.push ("duck");
    s2.push ("goose");
  }
}

Previous pageContentsNext page