Question: Lab 3 : The remaining files for this program are provided on Blackboard: ArrayStack and Main. The implementation of ArrayStack is currently incomplete ( the
Lab : The remaining files for this program are provided on Blackboard: ArrayStack and Main.
The implementation of ArrayStack is currently incomplete the imports and private methods
needed for this implementation have been provided Based on the StackInterface as well as the
directions that follow, complete the ArrayStack class.
The implementation of the stack data structure will include the following fields:
An array of elements of type T that represents the stack
An int that represents the top index of the stack
A boolean that represents the integrity of the data structure
An int that represents the default capacity of the stack
An int that represents the maximum capacity of the stack
The two fields related to the stacks capacity should be made static and final.
There will be two constructors:
The noargs constructor will call the parameterized constructor with the default capacity
passed in
The parameterized constructor will take an int argument to specify the capacity of the
stack. First, this capacity is checked using the checkCapacity method. Next, a new
Object array is constructed using the capacity, cast to an array of elements of type T and
assigned to the field that represents the stack. Lastly, the top index is initialized to
and the integrity is initialized to true.
The push method will take an argument of type T that represents the new entry. The push
method checks the integrity and ensures the capacity of the stack using the checkIntegrity and
ensureCapacity methods. The top index is incremented, and the new entry is added to the stack
at this position. The push method does not return any values.
The pop method will take no arguments. The peek method checks the integrity of the stack. If
the stack is empty, an EmptyStackException must be thrown. Otherwise, the entry at the top of
the stack is removed and returned. To remove the top entry, this position of the stack must be
set to null and the top index must be decremented.
The peek method will take no arguments. The peek method checks the integrity of the stack. If
the stack is empty, an EmptyStackException must be thrown. Otherwise, the entry at the top of
the stack is returned.
The isEmpty method will take no arguments. This method returns a boolean indicating if the
stack contains no entries. The value of the top index can be used to determine if the stack is
currently empty.
The clear method will take no arguments. The clear method checks the integrity of the stack.
Next, each entry in the stack is set to null. Lastly, the top index is set to
public class Main
public static void mainString args
StackInterface pile new ArrayStack;
pile.pushJazmin;
pile.pushJess;
pile.pushJack;
pile.pushpilepop;
pile.pushpilepeek;
pile.pushSeiji;
String name pile.pop;
pile.pushpilepeek;
System.out.printlnThe person removed from the pile is name ;
System.out.printlnThe person at the top of the pile is pile.peek;
int n ;
StackInterface numbers new ArrayStack;
while n
numbers.pushn;
n;
int result ;
while numbers.isEmpty
result numbers.pop;
System.out.printlnThe result is result ;
import java.util.Arrays;
import java.util.EmptyStackException;
public class ArrayStack implements StackInterface
Add your code here
private void checkIntegrity
if integrity
throw new SecurityExceptionStack object is corrupt";
private void checkCapacityint capacity
if capacity MAXCAPACITY
throw new IllegalStateExceptionConstructing stack whose
"capacity exceeds MAXCAPACITY;
private void ensureCapacity
if topIndex stack.length
int newLength stack.length ;
checkCapacitynewLength;
stack Arrays.copyOfstack newLength;
public interface StackInterface
public void pushT newEntry;
public T pop;
public T peek;
public boolean isEmpty;
public void clear;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
