jdsl.simple.ref
Class ArrayStack

java.lang.Object
  |
  +--jdsl.simple.ref.ArrayStack

public class ArrayStack
extends java.lang.Object
implements Stack

Implementation of the Stack interface using an array. The stack can store at most a given number of elements; if the user tries to insert more than that, number, an exception is thrown.

Author:
Natasha Gelfand

Field Summary
static int CAPACITY
          Default maximum capacity of the stack.
 
Constructor Summary
ArrayStack()
          This constructor initializes the stack with the default capacity specified by the CAPACITY constant.
ArrayStack(int cap)
          This constructor initializes the stack with the capacity specified by the user.
 
Method Summary
 boolean isEmpty()
          This function returns true if and only if the stack is empty
 java.lang.Object pop()
          Remove the top element on the stack, moving all other elements up.
 void push(java.lang.Object obj)
          Put an element onto the top of the stack, and move all previously inserted elements down.
 int size()
          Return the size of the stack, that is the number of elements it has.
 java.lang.Object top()
          Look at the top element on the stack, without removing it or otherwise affecting the stack.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CAPACITY

public static final int CAPACITY
Default maximum capacity of the stack.
Constructor Detail

ArrayStack

public ArrayStack()
This constructor initializes the stack with the default capacity specified by the CAPACITY constant.

ArrayStack

public ArrayStack(int cap)
This constructor initializes the stack with the capacity specified by the user.
Parameters:
cap - Capacity of the stack
Method Detail

size

public int size()
Return the size of the stack, that is the number of elements it has.
Specified by:
size in interface Stack
Returns:
Number of elements in the stack

isEmpty

public boolean isEmpty()
This function returns true if and only if the stack is empty
Specified by:
isEmpty in interface Stack
Returns:
true if the stack is empty, false otherwise

push

public void push(java.lang.Object obj)
Put an element onto the top of the stack, and move all previously inserted elements down.
Specified by:
push in interface Stack
Parameters:
element -  

top

public java.lang.Object top()
                     throws StackEmptyException
Look at the top element on the stack, without removing it or otherwise affecting the stack.
Specified by:
top in interface Stack
Returns:
The element at the top of the stack

pop

public java.lang.Object pop()
                     throws StackEmptyException
Remove the top element on the stack, moving all other elements up.
Specified by:
pop in interface Stack
Returns:
The top element of the stack