import java.util.
*;
class Stack {
int maxSize;
int[] stackArray;
int top;
Stack(int size) {
maxSize = size;
stackArray = new int[maxSize];
top = -1;
void push(int value) {
if (top == maxSize - 1) {
System.out.println("Stack overflow");
return;
stackArray[++top] = value;
System.out.println(value + " pushed into the stack");
int pop() {
if (top == -1) {
System.out.println("Stack underflow");
return -1;
int popped = stackArray[top--];
System.out.println(popped + " popped from the stack");
return popped;
int top() {
if (top == -1) {
System.out.println("Stack is empty");
return -1;
return stackArray[top];
boolean isEmpty() {
return top == -1;
public class Main {
public static void main(String[] args) {
Stack stack = new Stack(5);
stack.push(10);
stack.push(20);
stack.push(30);
System.out.println("Top element: " + stack.top());
stack.pop();
stack.pop();
stack.pop();
stack.pop();
System.out.println("Is stack empty? " + stack.isEmpty());