Programming Pandit

c/c++/c#/Javav/Python


Latest Update

Monday, February 24, 2025

Java Interface for ADT Stack and its Array Implementation with Exception Handling

 Objective: Design a Java interface for ADT Stack. Implement this interface using array. Provide necessary exception handling in both the implementations.


Code: 

// Custom Exception for Stack Overflow

class StackOverflowException extends Exception {

    public StackOverflowException(String message) {

        super(message);

    }

}


// Custom Exception for Stack Underflow

class StackUnderflowException extends Exception {

    public StackUnderflowException(String message) {

        super(message);

    }

}


// Stack Interface

interface StackADT {

    void push(int item) throws StackOverflowException;

    int pop() throws StackUnderflowException;

    int peek() throws StackUnderflowException;

    boolean isEmpty();

    boolean isFull();

    int size();

}


// Stack Implementation using Array

class ArrayStack implements StackADT {

    private int[] stack;

    private int top;

    private int capacity;


    // Constructor to initialize stack

    public ArrayStack(int capacity) {

        this.capacity = capacity;

        stack = new int[capacity];

        top = -1; // Stack is empty initially

    }


    // Push an element onto the stack

    @Override

    public void push(int item) throws StackOverflowException {

        if (isFull()) {

            throw new StackOverflowException("Stack Overflow! Cannot push " + item);

        }

        stack[++top] = item;

        System.out.println("Pushed: " + item);

    }


    // Pop an element from the stack

    @Override

    public int pop() throws StackUnderflowException {

        if (isEmpty()) {

            throw new StackUnderflowException("Stack Underflow! Cannot pop from an empty stack.");

        }

        int poppedItem = stack[top--];

        System.out.println("Popped: " + poppedItem);

        return poppedItem;

    }


    // Peek at the top element of the stack

    @Override

    public int peek() throws StackUnderflowException {

        if (isEmpty()) {

            throw new StackUnderflowException("Stack is empty! No element to peek.");

        }

        return stack[top];

    }


    // Check if stack is empty

    @Override

    public boolean isEmpty() {

        return top == -1;

    }


    // Check if stack is full

    @Override

    public boolean isFull() {

        return top == capacity - 1;

    }


    // Get the size of the stack

    @Override

    public int size() {

        return top + 1;

    }

}


// Main class to test the stack implementation

public class StackDemo {

    public static void main(String[] args) {

        ArrayStack stack = new ArrayStack(5); // Create stack of size 5


        try {

            stack.push(10);

            stack.push(20);

            stack.push(30);

            stack.push(40);

            stack.push(50);


            // Uncomment to test Stack Overflow

            // stack.push(60);


            System.out.println("Top element is: " + stack.peek());

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


            stack.pop();

            stack.pop();


            System.out.println("Top element after pop: " + stack.peek());

            System.out.println("Stack size after pop: " + stack.size());


            // Uncomment to test Stack Underflow

            // stack.pop(); stack.pop(); stack.pop(); stack.pop();


        } catch (StackOverflowException | StackUnderflowException e) {

            System.out.println("Error: " + e.getMessage());

        }

    }

}


Output:




No comments:

Post a Comment