Lecture overview -- Keyboard shortcut: 'u'  Previous page: Abstrakte metoder i Java -- Keyboard shortcut: 'p'  Next page: Eksempel: Comparable -- Keyboard shortcut: 'n'  Lecture notes - all slides and notes together  Annotated slide -- Keyboard shortcut: 't'  Alphabetic index  Help page about these notes  Course home    Design af klassehierarkier - slide 7 : 26

Eksempel: Stakken igen 

AbstractClassEx.javaDen abstrakte klasse Stack igen.
 

class UnboundedStack extends Stack{

  private int topIndex;
  private Vector store;

  public UnboundedStack(){
    store = new Vector(100);
    topIndex = 0;
  }

  public void push(Object el){
    if (topIndex > store.capacity() - 5)
       store.ensureCapacity(store.capacity() + 100);
    if (! full()){  
      store.add(topIndex, el);
      topIndex = topIndex + 1;
    }
  }
     
  public void pop(){
    if (! empty()){
      topIndex = topIndex - 1;
    }
  }

  public Object top(){
    if (!empty()){
      return (store.elementAt(topIndex - 1));
    } 
    else return (null);
  }
 
  public boolean full(){
    return false;
  }

  public boolean empty(){
    return(topIndex <=  0);
  }
    
  public int size(){
    return (topIndex);
  }

  private String stackContents(){
    StringBuffer st = new StringBuffer();
    for (int i = topIndex - 1 ; i >= 0; i--){
      st.append(store.elementAt(i).toString());
      st.append(".");
    }
      
    return (st.toString());
  }

  public String toString(){
    return(super.toString() + ": " +  stackContents());
  }
} // end UnboundedStack
 

AbstractClassEx.javaEt eksempel på en anvendelse af klassen UnboundedStack.