generics
Class Stack<T>

java.lang.Object
  extended by generics.Stack<T>
All Implemented Interfaces:
StackADT<T>

public class Stack<T>
extends java.lang.Object
implements StackADT<T>

This Stack class represents a last-in-first-out (LIFO) stack of objects. The usual push and pop operations are provided, as well as a method to peek at the top item on the stack, and a method to test for whether the stack is empty. When a stack is first created, it contains no items.


Field Summary
private  T[] data
          The array into which the objects of the stack are stored.
private static int MAX_SIZE
          The default capacity of this stack.
private  int size
          The number of objects in this stack.
 
Constructor Summary
Stack()
          Constructs a new Stack with capacity for 100 objects
Stack(int size)
          Constructs a new Stack with capacity specified by user
 
Method Summary
 int getSize()
          Returns the number of objects on the stack.
 boolean isEmpty()
          Tests if this stack is empty.
 boolean isFull()
          Tests if this stack is full.
 T peek()
          Returns the object at the top of this stack without removing it.
 T pop()
          Removes and returns the object at the top of this stack.
 void push(T x)
          Pushes an object onto the top of this stack.
 java.lang.String toString()
          Returns the current state of this stack.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

data

private T[] data
The array into which the objects of the stack are stored.


size

private int size
The number of objects in this stack.


MAX_SIZE

private static final int MAX_SIZE
The default capacity of this stack.

See Also:
Constant Field Values
Constructor Detail

Stack

public Stack()
Constructs a new Stack with capacity for 100 objects


Stack

public Stack(int size)
Constructs a new Stack with capacity specified by user

Parameters:
size - the size of the Stack
Method Detail

getSize

public int getSize()
Returns the number of objects on the stack.

Specified by:
getSize in interface StackADT<T>
Returns:
The number of objects on the stack.

toString

public java.lang.String toString()
Returns the current state of this stack.

Overrides:
toString in class java.lang.Object
Returns:
String representation of this stack.

push

public void push(T x)
          throws StackFullException
Pushes an object onto the top of this stack.

Specified by:
push in interface StackADT<T>
Parameters:
x - The object to be stored onto the stack.
Throws:
StackFullException - - if this stack is full

pop

public T pop()
      throws StackEmptyException
Removes and returns the object at the top of this stack.

Specified by:
pop in interface StackADT<T>
Returns:
The object at the top of the stack.
Throws:
StackEmptyException - - if this stack is full

peek

public T peek()
       throws StackEmptyException
Returns the object at the top of this stack without removing it.

Specified by:
peek in interface StackADT<T>
Returns:
The object at the top of the stack.
Throws:
StackEmptyException - - if this stack is full

isEmpty

public boolean isEmpty()
Tests if this stack is empty.

Specified by:
isEmpty in interface StackADT<T>
Returns:
true if and only if this stack is empty; false otherwise.

isFull

public boolean isFull()
Tests if this stack is full.

Specified by:
isFull in interface StackADT<T>
Returns:
true if and only if this stack is full; false otherwise.