Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Collection assignment: Modify the given Stack.java file to add a method public T

ID: 3847451 • Letter: C

Question

Collection assignment:

Modify the given Stack.java file to add a method

public T pop()

to return the top element if not empty; else throw EmptyStackException

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

The   output should be like:

EmptyStackException.java:

// 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


/**************************************************************************
* (C) Copyright 1992-2012 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/

Stack.java:


// 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 >

StackTest.java:


// 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 StackTest

----------------------------------Thank You-----------------------------------

CAC Command Prompt C: My Work homework java Stack Test Pushing elements onto doubleStack Popping elements 1 doubles tack from Empt yStackExcept ion Stack is empt y, cannot pop at Stack pop (Stack java 32 Iest Pop CStack Test. java 49) at Stack Test ma in Stack Test java: 16) Pushing elements onto integerStack 4 9 elements from integerStack 10 9 8 7 3 Empt Stack Except ion Stack is empt y, cannot pop at Stack pop (Stack java 32 Stack Iest test Pop CStack Test. java 49) at Stack, Test. main(Stack Test. java:20) C: My Work homework

Explanation / Answer

StackTest.java


import java.util.EmptyStackException;


//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 ) {
   try {
       System.out.printf( " Puping elements from %s ", name );
   while(!stack.isEmpty()) {
   T element= stack.pop();
   System.out.printf( "%s ", element );
   }
   } catch(EmptyStackException e ){
       System.out.println(" Stack is an empty");
   }
}

} // end class StackTest

Stack.java


//Generic class Stack declaration.
import java.util.ArrayList;
import java.util.EmptyStackException;
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
public T pop() {
   if(isEmpty())
   {
   throw new EmptyStackException();
   }
T returnObj = elements.get(elements.size()-1);
   elements.remove(elements.size()-1);
   return returnObj;

}
public boolean isEmpty() {
   return elements.size() <= 0;
   }

} // end class Stack< T >

Output:


Pushing elements onto doubleStack
1.1 2.2 3.3 4.4 5.5
P0ping elements from doubleStack
5.5 4.4 3.3 2.2 1.1
Pushing elements onto integerStack
1 2 3 4 5 6 7 8 9 10
P0ping elements from integerStack
10 9 8 7 6 5 4 3 2 1