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 implements Stack { private final static int DEFAULT_CAPACITY = 100; private final static int EMPTY_INDEX = -1; private E[] stack; private int top = EMPTY_INDEX; // Index of the top element on the stack

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 { /** * Push an element onto the stack. * * @param element, a value to be pushed on the stack */ public void push(E element); /** * Pop the top element off the stack. */ public E pop(); /** * Return the top element on the stack. */ public E top(); /** * Return True if the stack contains no elements. * * @return true if there are no elements in the stack */ public boolean isEmpty(); }

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!