Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Fix the ArrayStack.java file so you add the required method declared in StackInterface, but not defined ArrayStack yet (so write the function definition), AND fix

Fix the ArrayStack.java file so you add the required method declared in StackInterface, but not defined ArrayStack yet (so write the function definition), AND fix the isEmpty() method. In the StackDriver file, where it says to "fill in for Ex. 2.1" put the correct code so an ArrayStack is instantiated, and also where it says "add code to print size of stack", put a System.out.println statement as it specifies Copy and paste the output to the end of the file, commented out. Also, answer the following questions (also at the end of the file):

1. Was the "UNABLE TO PUSH..." message displayed? If so, why?

2. What happens when you try to pop() from an empty stack?

// ArrayStack.java

import java.util.*;

/** A class of stacks whose entries are stored in an array. @author Frank M. Carrano @author Timothy M. Henry @version 4.0 UPDATED by C. Lee-Klawender */ public class ArrayStack implements StackInterface { private T[] stack; // Array of stack entries private int topIndex; // Index of top entry private static final int DEFAULT_CAPACITY = 5; private static final int MAX_CAPACITY = 100;

public ArrayStack() { this(DEFAULT_CAPACITY); } // end default constructor

public ArrayStack(int initialCapacity) { if(initialCapacity > MAX_CAPACITY) initialCapacity = MAX_CAPACITY; else if( initialCapacity < DEFAULT_CAPACITY ) initialCapacity = DEFAULT_CAPACITY;

// The cast is safe because the new array contains null entries @SuppressWarnings("unchecked") T[] tempStack = (T[])new Object[initialCapacity]; stack = tempStack; topIndex = -1; } // end constructor

public boolean push(T newEntry) { if( topIndex+1 < stack.length ) { stack[topIndex + 1] = newEntry; topIndex++; return true; } return false; } // end push

public T peek() { if (isEmpty()) // UPDATE FOR HW#1 return null; else return stack[topIndex]; } // end peek

public T pop() { if (isEmpty()) // UPDATE FOR HW#1 return null; else { T top = stack[topIndex]; stack[topIndex] = null; topIndex--; return top; } // end if } // end pop

// TWO MORE METHODS ARE REQUIRED HERE (PART OF EXERCISE 2.1)

} // end ArrayStack

//StackDriver

/** A driver that demonstrates the using an implementation of a Stack.

@author Frank M. Carrano @author Timothy M. Henry @version 4.0 UPDATED by C. Lee-Klawender */ public class StackDriver { public static void main(String[] args) { System.out.println("Create a stack: "); StackInterface myStack = new _____________<>(); // FILL IN FOR EX. 2.1

System.out.println("Results of testing the FIRST stack: "); testStackOperations(myStack);

// Code will be added for Ex. 2.2 here

System.out.println(" Done."); } // end main

public static void testStackOperations(StackInterface myStack) {

System.out.println("isEmpty() returns " + myStack.isEmpty());

System.out.println(" Add to stack to get " + "Joe Jane Jill Jess Jim"); String [] strArray = {"Jim", "Jess", "Jill", "Jane", "Joe", "Jack"}; for( int i=0; i < strArray.length ; ++i ) { if( myStack.push( strArray[i] ) ) System.out.println("Pushed " + strArray[i] + " successfully"); else System.out.println("UNABLE TO PUSH " + strArray[i] ); }

System.out.println(" isEmpty() returns " + myStack.isEmpty());

// FOR LAB EXERCISE 2.1, ADD CODE HERE TO GET THE SIZE FROM THE Stack AND DISPLAY IT

System.out.println(" Testing peek and pop:"); while (!myStack.isEmpty()) { String top = myStack.peek(); System.out.println(" " + top + " is at the top of the stack.");

top = myStack.pop(); System.out.println(top + " is removed from the stack."); } // end while

System.out.print(" The stack should be empty: "); System.out.println("isEmpty() returns " + myStack.isEmpty()); System.out.println(" myStack.peek() returns "); System.out.println(myStack.peek()); System.out.println(" myStack.pop() returns "); System.out.println(myStack.pop());

System.out.println(" End of Stack Test "); } // end testStackOperations } // end Driver

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Administrator Limited Edition

Authors: Martif Way

1st Edition

B0CGG89N8Z

More Books

Students also viewed these Databases questions

Question

Comment on the pH value of lattice solutions of salts.

Answered: 1 week ago

Question

3. What are potential solutions?

Answered: 1 week ago