Objective: 1. Modify the given Stack.java file to add a method public T pop() to
ID: 3660546 • Letter: O
Question
Objective: 1. Modify the given Stack.java file to add a method public T pop() to return the top element if not empty; else throw EmptyStackException 2. Modify the given StackTest.java file to add a method public static < T > void testPop( String name, Stack< T > stack ) To generic method testPop pops all elements from a Stack Need to use try/catch for EmptyStackException ----------------------------------------------------------------------------------------------------------------------- // Fig. 21.8: EmptyStackException.java // EmptyStackException class declaration. public class EmptyStackException extends RuntimeException { // no-argument constructor public EmptyStackException() { this( "Stack is empty" ); } // end no-argument EmptyStackException constructor // one-argument constructor public EmptyStackException( String exception ) { super( exception ); } // end one-argument EmptyStackException constructor } // end class EmptyStackException ---------------------------------------------------------------------------------------------------------------------- // Generic class Stack declaration. import java.util.ArrayList; public class Stack< T > { private ArrayList< T > elements; // ArrayList stores stack elements // no-argument constructor creates a stack of the default size public Stack() { this( 10 ); // default stack size } // end no-argument Stack constructor // constructor creates a stack of the specified number of elements public Stack( int capacity ) { int initCapacity = capacity > 0 ? capacity : 10; // validate elements = new ArrayList< T >( initCapacity ); // create ArrayList } // end one-argument Stack constructor // push element onto stack public void push( T pushValue ) { elements.add( pushValue ); // place pushValue on Stack } // end method push // To do: write a methed public T pop() // to return the top element if not empty; else throw EmptyStackException } // end class Stack< T > ----------------------------------------------------------------------------------------------------------------------- // Passing generic Stack objects to generic methods. public class StackTest { public static void main( String[] args ) { Double[] doubleElements = { 1.1, 2.2, 3.3, 4.4, 5.5 }; Integer[] integerElements = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // Create a Stack< Double > and a Stack< Integer > Stack< Double > doubleStack = new Stack< Double >( 5 ); Stack< Integer > integerStack = new Stack< Integer >(); // push elements of doubleElements onto doubleStack testPush( "doubleStack", doubleStack, doubleElements ); testPop( "doubleStack", doubleStack ); // pop from doubleStack // push elements of integerElements onto integerStack testPush( "integerStack", integerStack, integerElements ); testPop( "integerStack", integerStack ); // pop from integerStack } // end main // generic method testPush pushes elements onto a Stack public static < T > void testPush( String name , Stack< T > stack, T[] elements ) { System.out.printf( " Pushing elements onto %s ", name ); // push elements onto Stack for ( T element : elements ) { System.out.printf( "%s ", element ); stack.push( element ); // push element onto stack } // end for } // end method testPush // To do : generic method testPop pops all elements from a Stack // Need to use try/catch for EmptyStackException //public static < T > void testPop( String name, Stack< T > stack ) } // end class StackTestExplanation / Answer
// EmptyStackException class declaration. public class EmptyStackException extends RuntimeException { // no-argument constructor public EmptyStackException() { this("Stack is empty"); } // end no-argument EmptyStackException constructor // one-argument constructor public EmptyStackException(String exception) { super(exception); } // end one-argument EmptyStackException constructor } // end class EmptyStackException ================================================= // Generic class Stack declaration. import java.util.ArrayList; public class Stack< T > { private ArrayList< T > elements; // ArrayList stores stack elements // no-argument constructor creates a stack of the default size public Stack() { this( 10 ); // default stack size } // end no-argument Stack constructor // constructor creates a stack of the specified number of elements public Stack( int capacity ) { int initCapacity = capacity > 0 ? capacity : 10; // validate elements = new ArrayList<>( initCapacity ); // create ArrayList } // end one-argument Stack constructor // push element onto stack public void push( T pushValue ) { elements.add( pushValue ); // place pushValue on Stack } // end method push // To do: write a methed public T pop() to return the top element if not empty; else throw EmptyStackException public T pop() { T tmpele; if(elements.size()<= 0) { throw new EmptyStackException(); } else { tmpele = elements.get(elements.size() - 1); elements.remove(elements.size() - 1); } return tmpele; } }// end class Stack< T > ====================================== // Generic class Stack declaration. import java.util.ArrayList; public class Stack< T > { private ArrayList< T > elements; // ArrayList stores stack elements // no-argument constructor creates a stack of the default size public Stack() { this( 10 ); // default stack size } // end no-argument Stack constructor // constructor creates a stack of the specified number of elements public Stack( int capacity ) { int initCapacity = capacity > 0 ? capacity : 10; // validate elements = new ArrayList<>( initCapacity ); // create ArrayList } // end one-argument Stack constructor // push element onto stack public void push( T pushValue ) { elements.add( pushValue ); // place pushValue on Stack } // end method push // To do: write a methed public T pop() to return the top element if not empty; else throw EmptyStackException public T pop() { T tmpele; if(elements.size()<= 0) { throw new EmptyStackException(); } else { tmpele = elements.get(elements.size() - 1); elements.remove(elements.size() - 1); } return tmpele; } }// end class Stack< T > ====================================== public class StackTest { public static void main( String[] args ) { Double[] doubleElements = { 1.1, 2.2, 3.3, 4.4, 5.5 }; Integer[] integerElements = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // Create a Stack< Double > and a Stack< Integer > Stack<Double> doubleStack = new Stack<>( 5 ); Stack<Integer> integerStack = new Stack<>(); // push elements of doubleElements onto doubleStack testPush( "doubleStack", doubleStack, doubleElements ); testPop( "doubleStack", doubleStack ); // pop from doubleStack // push elements of integerElements onto integerStack testPush( "integerStack", integerStack, integerElements ); testPop( "integerStack", integerStack ); // pop from integerStack } // end main // generic method testPush pushes elements onto a Stack public static<T> void testPush(String name , Stack<T> stack,T[] elements) { System.out.printf( " Pushing elements onto %s ", name ); // push elements onto Stack for ( T element : elements ) { System.out.printf( "%s ", element ); stack.push( element ); // push element onto stack } // end for } // end method testPush // To do : generic method testPop pops all elements from a Stack // Need to use try/catch for EmptyStackException public static<T> void testPop(String name, Stack<T> stack ) { System.out.printf( " pop elements from %s ", name ); // pop elements from Stack try { while(true) { T popele = stack.pop(); System.out.printf( "popped element = %s ", popele); } } catch(EmptyStackException exp) { System.out.println(exp.getMessage()); } } } // end class StackTest public class StackTest { public static void main( String[] args ) { Double[] doubleElements = { 1.1, 2.2, 3.3, 4.4, 5.5 }; Integer[] integerElements = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // Create a Stack< Double > and a Stack< Integer > Stack<Double> doubleStack = new Stack<>( 5 ); Stack<Integer> integerStack = new Stack<>(); // push elements of doubleElements onto doubleStack testPush( "doubleStack", doubleStack, doubleElements ); testPop( "doubleStack", doubleStack ); // pop from doubleStack // push elements of integerElements onto integerStack testPush( "integerStack", integerStack, integerElements ); testPop( "integerStack", integerStack ); // pop from integerStack } // end main // generic method testPush pushes elements onto a Stack public static<T> void testPush(String name , Stack<T> stack,T[] elements) { System.out.printf( " Pushing elements onto %s ", name ); // push elements onto Stack for ( T element : elements ) { System.out.printf( "%s ", element ); stack.push( element ); // push element onto stack } // end for } // end method testPush // To do : generic method testPop pops all elements from a Stack // Need to use try/catch for EmptyStackException public static<T> void testPop(String name, Stack<T> stack ) { System.out.printf( " pop elements from %s ", name ); // pop elements from Stack try { while(true) { T popele = stack.pop(); System.out.printf( "popped element = %s ", popele); } } catch(EmptyStackException exp) { System.out.println(exp.getMessage()); } } } // end class StackTestRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.