Question: Complete the ArrayStack.java, an array based implementation of the stack.java interface. Modify only the bodies of the following methods in the ArrayStack class: push pop
Complete the ArrayStack.java, an array based implementation of the stack.java interface.
Modify only the bodies of the following methods in the ArrayStack class:
push
pop
top
isEmpty
isFull
Nothing else should be modified in ArrayStack.
Write the class ArrayStackTest to test your implementation. Print only the names of the tests and error messages. Give the test method meaningful names (e.g., emptyStackTest).
Test push, pop, top, and isEmpty immediately after the stack has been instantiated.
Push an element on an empty stack. Test pop, top, and isEmpty.
Instantiate a stack with initialCapacity of 1. Push two elements. Check capacity.
Other tests you deem necessary.
ArrayStack.java
import java.util.EmptyStackException;
public class ArrayStack
public ArrayStack() { this(DEFAULT_CAPACITY); } public ArrayStack(int initialCapacity) { initialCapacity = initialCapacity < 0 ? 0 : initialCapacity; stack = (E[]) new Object[initialCapacity]; }
@Override public void push(E element) { // TBD as part of an assignment } @Override public E pop() { // TBD as part of an assignment } @Override public E top() { // TBD as part of an assignment } @Override public boolean isEmpty() { // TBD as part of an assignment } private boolean isFull() { // TBD as part of an assignment } private void expandCapacity() { E[] temp = (E[]) new Object[stack.length * 2 + 1]; System.arraycopy(stack, 0, temp, 0, stack.length); stack = temp; } public int capacity() { return stack.length; } @Override public String toString() { StringBuilder result = new StringBuilder(); result.append("["); if (! isEmpty()) { for (int i = top; 0 < i; i -= 1) { result.append(stack[i] + ", "); } result.append(stack[0]); } result.append("]"); return result.toString(); } }
Stack.java
public interface Stack
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
