/** realizza l'ADT Stack con un array a dimensione fissa @author @version @see Stack */ public class FixedArrayStack implements Stack { private final int CAPACITY = 100; // v è un array riempito solo in parte protected Object[] v; // considerare protected int vSize; // protected // uguale a private public FixedArrayStack() { /* per rendere vuota la struttura, invoco il metodo makeEmpty; è sempre meglio evitare di scrivere codice ripetuto */ makeEmpty(); } /** rende vuoto il contenitore */ public void makeEmpty() { vSize = 0; v = new Object[CAPACITY]; } /** verifica se il contenitore e’ vuoto @return true se vuota, false altrimenti */ public boolean isEmpty() { return (vSize == 0); } /** calcola il numero di elementi nel contenitore @return il numero di elementi nel contenitore */ public int size() { return vSize; } public void push(Object obj) throws FullStackException { if (vSize >= CAPACITY) throw new FullStackException(); v[vSize++] = obj; } public Object top() throws EmptyStackException { if (isEmpty()) throw new EmptyStackException(); return v[vSize - 1]; } public Object pop() throws EmptyStackException { Object obj = top(); // lascia a top() l’eccezione vSize--; v[vSize] = null; //garbage collection return obj; } }