Answered You can hire a professional tutor to get the answer.

QUESTION

import public class Stack { ArrayListlt;Integergt; myNum = new ArrayListlt;Integergt;() ArrayListlt; st; public Stack() { st = new } public void...

import

public class Stack

{  

   ArrayList<Integer> myNum = new ArrayList<Integer>()

   ArrayList< st;      

   public Stack()   

   {

         st = new

   }

   public void push(int val)

   {

   }

   public int pop()

   {

   }

   public int size()

   {     

   }       

   public boolean isEmpty()

   {    

   }     

   public void clearStack()

   {     

   }

}

//========== Stack.java End ==============

//========= StackDemo.java  Beginning ===============

public class StackDemo

{

   public static void main(String[] args)

   {

        Stack s = new Stack();

        s.push(10);

        s.push(20);

        s.push(30);

        System.out.println ("Current stack size: " + s.size());

        System.out.println ("Next number in the stack: " + s.pop());

        System.out.println ("Current stack size: " + s.size());

        while (!s.isEmpty())

        {           

              System.out.println ("Next number in the stack: " + s.pop());

        }

        s.push(40);

        System.out.println ("Current stack size: " + s.size());

        s.clearStack();

        System.out.println ("Stack Cleared; Current stack size: " + s.size());

         }

}

//========= StackDemo.java End =============== 

Note1: Both files should be saved in the same folder.

Note2: The StackDemo.java file should not be changed.

The Stack.java class is incomplete. Your job is to complete the Stack.java class as follows such that when you run StackDemo.java, the output that is shown below, at the end of this question, is printed.  

Note: The Stack.java class uses an ArrayList of integer numbers to implement the stack.

Tasks:

1- Complete the Stack.java including the following methods:

a) public Stack() creates a stack (The constructor method).

b) public void push(int val) adds the integer input parameter (val) to the top of the stack.

c) public int pop() removes the next integer from the top of the stack and returns the removed value.

d) public int size() returns the number of elements (integer numbers) that are currently in the stack.

e) public boolean isEmpty() returns true if the stack is empty, otherwise false.

f) public void clearStack() removes all the elements that are currently in the stack.

2- In order to test your Stack.java class, run StackDemo.java and verify that the following output is generated:

Output:

Current stack size: 3

Next number in the stack: 30

Current stack size: 2

Next number in the stack: 20

Next number in the stack: 10

Current stack size: 1

Stack Cleared;

Current stack size: 0

Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question