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

Code a testing application. Your app must satisfy the following requirements: Cr

ID: 3671833 • Letter: C

Question


Code a testing application. Your app must satisfy the following requirements: Create an Integer slack for 5 elcments.y y middot Push 4 integers. -J Print the top element/Print each element and pop it. Check if the stack is empty. J Push one element and print thejop element.v Cheek if the stack is empty. ^ Pop the element. ^ Push 5 integers, and check if the stack is full. Push an clement. Your code must catch StackOvcr f lowException and print its message, o Your app shouldn't terminate, instead it should move to the next testing block Pop all of the elements, and check if the stack is empty. Pop an element. Your code must catch StackUr.de r f lowException and print its message, o Again, move to the next testing block after the message. Check if the stack is empty. Top an clement. Your code must catch StackUr.derf lowException and print its message.J1MII 'IIA'N'HIVH.

Explanation / Answer

import java.util.Stack;

public class StackDemo
{
public static void main(String args[])
{
   int a=5;
// Create a new, empty stack
Stack lifo = new Stack(5);

// Let's add some items to it
for (int i = 1; i <= 4; i++)
{
lifo.push ( new Integer(i) );
}

   System.out.println(lifo);
// Last in first out means reverse order
while ( !lifo.empty() )
{
System.out.print ( lifo.pop() );
System.out.print ( ',' );
}
   if(lifo.empty())
   {
   lifo.push ( new Integer(a) );
   System.out.println(lifo);
   }

   if(lifo.empty())
   {
   }
   else
   lifo.push ( new Integer(a) );
  
   }
for (int i = 1; i <= 5; i++)
{
lifo.push ( new Integer(i) );
}
  
}

}