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 3: 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 no-args 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 -1,
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 -1.
public class Main {
public static void main(String[] args){
StackInterface pile = new ArrayStack<>();
pile.push("Jazmin");
pile.push("Jess");
pile.push("Jack");
pile.push(pile.pop());
pile.push(pile.peek());
pile.push("Seiji");
String name = pile.pop();
pile.push(pile.peek());
System.out.println("The person removed from the pile is "+ name +".");
System.out.println("The person at the top of the pile is "+ pile.peek()+".");
int n =12;
StackInterface numbers = new ArrayStack<>();
while (n >0){
numbers.push(n);
n--;
}
int result =0;
while (!numbers.isEmpty()){
result += numbers.pop();
}
System.out.println("The 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 SecurityException("Stack object is corrupt");
}
}
private void checkCapacity(int capacity){
if (capacity > MAX_CAPACITY){
throw new IllegalStateException("Constructing stack whose "+
"capacity exceeds "+ MAX_CAPACITY);
}
}
private void ensureCapacity(){
if (topIndex == stack.length -1){
int newLength = stack.length *2;
checkCapacity(newLength);
stack = Arrays.copyOf(stack, newLength);
}
}
}
public interface StackInterface {
public void push(T 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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!