Write a java class called Stack which provides methods for push(item) and pop() operations. Capture instance variables and constructors as needed. Demonstrate the stack operations with help of a testing class called DemoStack code example

Example: stack class in java

import java.util.Stack;
class Main {
    public static void main(String[] args) {
        Stack<String> animals= new Stack<>();
        // Add elements to Stack
        animals.push("Dog");
        animals.push("Horse");
        // Remove element from Stack
      	animals.pop();
      	// Access element from top of Stack
      	animals.peek();
    }
}

Tags:

Java Example