Question
public final class ArrayStack implements StackInterface { private T[] stack ; // Array of stack entries private int topIndex ; // Index of top entry
public final class ArrayStack
private T[] stack ; // Array of stack entries private int topIndex ; // Index of top entry private boolean initialized = false ; private static final int DEFAULT_CAPACITY = 50 ; private static final int MAX_CAPACITY = 10000 ;
/** * Create an ArrayStack with a default/initial capacity of DEFAULT_CAPACITY */ public ArrayStack() { this( DEFAULT_CAPACITY ) ; } // end no-arg constructor
/** * Create an ArrayStack with a default/initial capacity specified by the * application * * @param initialCapacity the initial capacity (limited by MAX_CAPACITY) */ public ArrayStack( int initialCapacity ) { checkCapacity( initialCapacity ) ;
// The cast is safe because the new array contains null entries @SuppressWarnings( "unchecked" ) T[] tempStack = ( T[] ) new Object[ initialCapacity ] ; stack = tempStack ; topIndex = -1 ; initialized = true ; } // end 1-arg (initial capacity) constructor
/* (non-Javadoc) * @see edu.wit.dcsn.comp2000.stackadt.StackInterface#push(java.lang.Object) */ @Override public void push( T newEntry ) { checkInitialization() ; ensureCapacity() ;
stack[ topIndex + 1 ] = newEntry ; topIndex++ ; } // end push()
/* (non-Javadoc) * @see edu.wit.dcsn.comp2000.stackadt.StackInterface#peek() */ @Override public T peek() { checkInitialization() ; if ( isEmpty() ) { throw new EmptyStackException() ; } else { return stack[ topIndex ] ; } } // end peek()
/* (non-Javadoc) * @see edu.wit.dcsn.comp2000.stackadt.StackInterface#pop() */ @Override public T pop() { checkInitialization() ; if ( isEmpty() ) { throw new EmptyStackException() ; } else { T top = stack[ topIndex ] ; stack[ topIndex ] = null ; topIndex-- ; return top ; } // end if
} // end pop()
/* (non-Javadoc) * @see edu.wit.dcsn.comp2000.stackadt.StackInterface#isEmpty() */ @Override public boolean isEmpty() { return topIndex
} // end isEmpty()
/* (non-Javadoc) * @see edu.wit.dcsn.comp2000.stackadt.StackInterface#clear() */ @Override public void clear() { checkInitialization() ;
// Remove references to the objects in the stack, // but do not deallocate the array while ( topIndex > -1 ) { stack[ topIndex ] = null ; topIndex-- ; } // end while // Assertion: topIndex is -1 } // end clear()
/** * Throws an exception if this object is not initialized. */ private void checkInitialization() { if ( !initialized ) { throw new SecurityException( "ArrayStack object is not initialized properly." ) ; } } // end checkInitialization()
/** * Throws an exception if the client requests a capacity that is too large. * * @param capacity the desired capacity - must be less than or equal to MAX_CAPAACITY */ private void checkCapacity( int capacity ) { if ( capacity > MAX_CAPACITY ) { throw new IllegalStateException( "Attempt to create a stack " + "whose capacity exceeds " + "allowed maximum." ) ; } } // end checkCapacity()
/** * Doubles the size of the array stack if it is full * *
Precondition: checkInitialization has been called. */ private void ensureCapacity() { if ( topIndex >= stack.length - 1 ) // If array is full, double its size { int newLength = 2 * stack.length ; checkCapacity( newLength ) ; stack = Arrays.copyOf( stack, newLength ) ; } // end if } // end ensureCapacity()
/** * For testing: * Return the contents of the stack in an array where array[0] is the top of the stack * * @return array containing the contents of the stack (sized to the number of entries * on the stack) */ T[] toArray() { // TODO Auto-generated method stub return null ; } // end toArray()
/* (non-Javadoc) * @see java.lang.Object#toString() */ @Override public String toString() { return Arrays.toString( toArray() ) ; } // end toString() /** * @param args -unused- */ public static void main( String[] args ) { // STUB for testing } // end main() } // end class ArrayStack
Modify the provided stack ADT implementation (ArrayStack.java) maintain the stack's bottom entry in stack[stack.length - 1] implement the toArray() method You must also implement a comprehensive set of unit tests using the main() method (and private utility methods) in ArrayStack.java and/or using JUnit 5 (separate from the provided JUnit test class)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started