Question: here is everything you need. // ArrayStack.java import java.util.Arrays; import java.util.EmptyStackException; /** A class of stacks whose entries are stored in an array. @author Frank


here is everything you need. // ArrayStack.java import java.util.Arrays;
import java.util.EmptyStackException;
/**
A class of stacks whose entries are stored in an array.
@author Frank M. Carrano
@author Timothy M. Henry
@version 4.0
*/
public final class ArrayStack
{
private T[] stack; // Array of stack entries
private int topIndex; // Index of top entry
private boolean initialized = false;
private static final int DEFAULT_CAPACITY = 50;
private static final int MAX_CAPACITY = 10000;
public ArrayStack()
{
this(DEFAULT_CAPACITY);
} // end default constructor
public ArrayStack(int initialCapacity)
{
checkCapacity(initialCapacity);
// The cast is safe because the new array contains null entries
@SuppressWarnings("unchecked")
T[] tempStack = (T[])new Object[initialCapacity];
stack = tempStack;
topIndex = -1;
initialized = true;
} // end constructor
public void push(T newEntry)
{
checkInitialization();
ensureCapacity();
stack[topIndex + 1] = newEntry;
topIndex++;
} // end push
public T peek()
{
checkInitialization();
if (isEmpty())
throw new EmptyStackException();
else
return stack[topIndex];
} // end peek
public T pop()
{
checkInitialization();
if (isEmpty())
throw new EmptyStackException();
else
{
T top = stack[topIndex];
stack[topIndex] = null;
topIndex--;
return top;
} // end if
} // end pop
public boolean isEmpty()
{
return topIndex
} // end isEmpty
public void clear()
{
checkInitialization();
// Remove references to the objects in the stack,
// but do not deallocate the array
while (topIndex > -1)
{
stack[topIndex] = null;
topIndex--;
} // end while
// Assertion: topIndex is -1
} // end clear
// Throws an exception if this object is not initialized.
private void checkInitialization()
{
if (!initialized)
throw new SecurityException ("ArrayStack object is not initialized properly.");
} // end checkInitialization
// Throws an exception if the client requests a capacity that is too large.
private void checkCapacity(int capacity)
{
if (capacity > MAX_CAPACITY)
throw new IllegalStateException("Attempt to create a stack " +
"whose capacity exceeds " +
"allowed maximum.");
} // end checkCapacity
// Doubles the size of the array stack if it is full
// Precondition: checkInitialization has been called.
private void ensureCapacity()
{
if (topIndex >= stack.length - 1) // If array is full, double its size
{
int newLength = 2 * stack.length;
checkCapacity(newLength);
stack = Arrays.copyOf(stack, newLength);
} // end if
} // end ensureCapacity
} // end ArrayStack // driver.java
/**
A driver that demonstrates the class ArrayStack.
@author Frank M. Carrano
@author Timothy M. Henry
@version 4.0
*/
public class Driver
{
public static void main(String[] args)
{
testStackOperations();
System.out.println(" Done.");
} // end main
public static void testStackOperations()
{
System.out.println("Create a stack: ");
StackInterface
System.out.println("isEmpty() returns " + myStack.isEmpty());
System.out.println(" Add to stack to get " +
"Joe Jane Jill Jess Jim");
myStack.push("Jim");
myStack.push("Jess");
myStack.push("Jill");
myStack.push("Jane");
myStack.push("Joe");
System.out.println(" isEmpty() returns " + myStack.isEmpty());
System.out.println(" Testing peek and pop:");
while (!myStack.isEmpty())
{
String top = myStack.peek();
System.out.println(" " + top + " is at the top of the stack.");
top = myStack.pop();
System.out.println(top + " is removed from the stack.");
} // end while
System.out.print(" The stack should be empty: ");
System.out.println("isEmpty() returns " + myStack.isEmpty());
System.out.println(" Add to stack to get " +
"Jim Jess Joe ");
myStack.push("Joe");
myStack.push("Jess");
myStack.push("Jim");
System.out.println(" Testing clear:");
myStack.clear();
System.out.println("The stack should be empty: ");
System.out.println(" isEmpty() returns " + myStack.isEmpty());
System.out.println(" myStack.peek() returns ");
System.out.println(myStack.peek());
System.out.println(" myStack.pop() returns ");
System.out.println(myStack.pop());
} // end testStackOperations
} // end Driver
/*
Create a stack:
isEmpty() returns true
Add to stack to get
Joe Jane Jill Jess Jim
isEmpty() returns false
Testing peek and pop:
Joe is at the top of the stack.
Joe is removed from the stack.
Jane is at the top of the stack.
Jane is removed from the stack.
Jill is at the top of the stack.
Jill is removed from the stack.
Jess is at the top of the stack.
Jess is removed from the stack.
Jim is at the top of the stack.
Jim is removed from the stack.
The stack should be empty: isEmpty() returns true
Add to stack to get
Jim Jess Joe
Testing clear:
The stack should be empty:
isEmpty() returns true
myStack.peek() returns
Exception in thread "main" java.util.EmptyStackException
at ArrayStack.peek(ArrayStack.java:46)
at Driver.testStackOperations(Driver.java:58)
at Driver.main(Driver.java:12)
*/ //StackInterface.Java /** An interface for the ADT stack. @author Frank M. Carrano @author Timothy M. Henry @version 4.0 */ public interface StackInterface
1. Implement the ADT stack by using an array stack to contain its entries. Expand the array dynamically, as necessary. Maintain the stack's bottom entry in stack[stack.1ength - 1] 1. Implement the ADT stack by using an array stack to contain its entries. Expand the array dynamically, as necessary. Maintain the stack's bottom entry in stack[stack.1ength - 1]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
