Question: ArrayStack Code- public class ArrayStack implements StackADT { private E stackArray[]; // Array holding stack private static final int DEFAULT_SIZE = 10; private int maxSize;

 ArrayStack Code- public class ArrayStack implements StackADT { private E stackArray[];

ArrayStack Code-

public class ArrayStack implements StackADT {

private E stackArray[]; // Array holding stack private static final int DEFAULT_SIZE = 10; private int maxSize; // Maximum size of stack private int top; // First free position at top

// Constructors @SuppressWarnings("unchecked") // Generic array allocation public ArrayStack(int size) { maxSize = size; top = 0; stackArray = (E[]) new Object[size]; // Create stackArray }

public ArrayStack() { this(DEFAULT_SIZE); }

// Modify this method // so that it all elements are null public void clear() { top = 0; } // Reinitialize stack

// Push "element" onto stack // Changed return type of method to void public void push(E element) { // modify this method // if (top >= maxSize) // move this to a private method called isFull() return false; // call growStack() instead // no changes after this stackArray[top++] = element; // return true; // you may remove this line altogether }

// Remove and return top element public E pop() { if (top == 0) return null; return stackArray[--top]; }

public E peek() { // Return top element if (top == 0) return null; return stackArray[top - 1]; }

public int length() { return top; } // Return stack size

public boolean isEmpty() { return top == 0; } // Tell if the stack is empty // add new method growStack() // growStack() that returns void // 1. create a new local array, call it newStack (see the constructor for an example) // 2. set the size of the new array to stackArray.length + DEFAULT_SIZE // 3. copy all the elements from stackArray to newStack (for loop is perfect) // 4. set stackArray to reference the newStack }

ArrayStackTester Code-

public class ArrayStackTester {

public static void main(String[] args) { // TODO Auto-generated method stub

// ArrayStack myStack = new ArrayStack(); AnotherStack myStack = new AnotherStack(2); try { myStack.peek(); } catch (EmptyCollectionException e) { // TODO Auto-generated catch block //e.printStackTrace(); System.out.println(e.getMessage()); } System.out.println("Push number 4"); myStack.push(4); System.out.println("Test peek [4]. Method returned: " + myStack.peek());

// Test new item at top after a second push System.out.println("Push number 7"); myStack.push(7); System.out.println("Test peek [7]: " + myStack.peek()); System.out.println("Push number 3");

myStack.push(3); }

}

StackADT Interface Code -

public interface StackADT { public void push(T element); public T pop(); public T peek(); public boolean isEmpty(); public int length(); public String toString();

}

1. Modify the clear() method so that all the elements in the array are reset to null a. Identify the complexity () of the clear() method before and after your changes. Explain. 2. Add a new method growStack() a. The method should initialize a new array. The size of the new array should be the current size plus the default size b. Update the stackArray reference to point to the new array c. Identify the complexity of the growStack() method. Explain. 3. Add a new method isFull() a. The method should return true when the stack array is full b. Identify the complexity of the isFull() method. Explain. 4. Modify the push() method a. When the stack is full, call the growStack method. Note that no other changes should be made to place the new element to the top of the stack. b. Identify the complexity O() of the push() method before and after the changes. Explain. 5. Update the ArrayStackTester a. Test the boundary of the array by pushing more elements to the stack to confirm the stack does not have a max capacity anymore b. Test the growStack "inefficiency". Push elements so that the stack grows at least twice. *You can add some comments to the growStack() method to display when the method runs and the new size of the array. i. Pop all but two elements of the stack ii. Use the debugger to check the size of the array or create a protected method that returns the length of the array (not the length of the stack!) so you can display the length of the array in your tester ii. Explain what is the "inefficiency introduced by making the ArrayStack unbounded. Use comments in your tester to include your answer. 1. Modify the clear() method so that all the elements in the array are reset to null a. Identify the complexity () of the clear() method before and after your changes. Explain. 2. Add a new method growStack() a. The method should initialize a new array. The size of the new array should be the current size plus the default size b. Update the stackArray reference to point to the new array c. Identify the complexity of the growStack() method. Explain. 3. Add a new method isFull() a. The method should return true when the stack array is full b. Identify the complexity of the isFull() method. Explain. 4. Modify the push() method a. When the stack is full, call the growStack method. Note that no other changes should be made to place the new element to the top of the stack. b. Identify the complexity O() of the push() method before and after the changes. Explain. 5. Update the ArrayStackTester a. Test the boundary of the array by pushing more elements to the stack to confirm the stack does not have a max capacity anymore b. Test the growStack "inefficiency". Push elements so that the stack grows at least twice. *You can add some comments to the growStack() method to display when the method runs and the new size of the array. i. Pop all but two elements of the stack ii. Use the debugger to check the size of the array or create a protected method that returns the length of the array (not the length of the stack!) so you can display the length of the array in your tester ii. Explain what is the "inefficiency introduced by making the ArrayStack unbounded. Use comments in your tester to include your

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!