Question
Not entirely sure if those are the correct java files. Complete Programming Project 1, page 194 In the test application, start with a stack size
Not entirely sure if those are the correct java files.
Complete Programming Project 1, page 194
In the test application, start with a stack size of 5 and create a stack of 10 or 12 elements to verify that the dynamics of the stack are functional. Remove several elements. Print the list before and after the copies to demonstrate the proper functioning of your methods.
---------------------------------
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 < 0;
} // 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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started