// Name:
Bradley Shedd
// Program: LAB#11ExtraCredit
// Date: December
1, 2010
// Class: Java1302
// Filename: ArrayStack.java
// Purpose: This class is an
array-based Object stack class with operations push,
//
pop, and isEmpty and isFull.
// ****************************************************************
public
class ArrayStack
implements
StackADT
{
private
int stackSize = 5;
// capacity
of stack
private
int top;
//
index of slot for next element
private
Object[] elements;
//
--------------------------------------------------
// Constructor -- initializes top and creates array
// --------------------------------------------------
public
ArrayStack()
{
elements
=
new
Object [stackSize];
top = -1;
}
//
--------------------------------------------------
// Adds element to top of stack if it's not full, else
// does nothing.
// --------------------------------------------------
public
void push(Object val)
{
if
( isFull())
increaseSize();
elements[++top] = val;
}
//
--------------------------------------------------
// Removes and returns value at top of stack.
If stack
// is empty returns null.
// --------------------------------------------------
public
Object pop()
{
if
(!isEmpty())
return
(elements[top--]);
else
System.err.println("Stack
is empty!");
return
null;
}
//
--------------------------------------------------
// Returns true if stack is empty, false otherwise.
// --------------------------------------------------
public
boolean isEmpty()
{
return
( top == -1 );
}
//
--------------------------------------------------
// Returns true if stack is full, false otherwise.
// --------------------------------------------------
public
boolean isFull()
{
return
( top == ( stackSize - 1 ));
}
private
void increaseSize()
{
Object [] temp =
new
Object [ 2 * elements.length];
for
(
int i = 0; i < elements.length;
++i)
temp [i] = elements [i];
elements = temp;
stackSize *= 2;
}
}