Week 5 of OOP in JAVA

Write a Java program to divide two numbers entered by the user and handle the exception that occurs when the denominator is zero.


import java.util.Scanner;
public class DivisionException {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        try {
            System.out.print("Enter numerator: ");
            int a = sc.nextInt();
            System.out.print("Enter denominator: ");
            int b = sc.nextInt();
            int result = a / b;
            System.out.println("Result = " + result);
        }
        catch (ArithmeticException e) {
            System.out.println("Error: Division by zero is not allowed.");
        }
     System.out.println("Program continues normally.");
    }
}


Write a Java program to create an array of 5 elements and intentionally access an invalid index to demonstrate handling of ArrayIndexOutOfBoundsException.


public class ArrayException {
    public static void main(String[] args) {
        int arr[] = {10, 20, 30, 40, 50};
        try {
            System.out.println(arr[7]);
        }
        catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Error: Invalid array index.");
        }

        System.out.println("Program executed successfully.");
    }
}


Write a Java program that demonstrates multiple catch blocks by handling both ArithmeticException and ArrayIndexOutOfBoundsException.


public class MultipleCatch {
    public static void main(String[] args) {

        try {
            int a = 10, b = 0;
            int result = a / b;

            int arr[] = new int[5];
            arr[10] = 50;
        }

        catch (ArithmeticException e) {
            System.out.println("Arithmetic Exception occurred.");
        }

        catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Array Index Exception occurred.");
        }

        catch (Exception e) {
            System.out.println("General Exception occurred.");
        }
    }
}


Write a Java program to demonstrate the use of the finally block which executes regardless of whether an exception occurs or not.


public class FinallyDemo {
    public static void main(String[] args) {

        try {
            int a = 20;
            int b = 0;
            int c = a / b;
        }

        catch (ArithmeticException e) {
            System.out.println("Exception caught: Division by zero.");
        }

        finally {
            System.out.println("Finally block executed.");
        }

        System.out.println("End of program.");
    }
}


Write a Java program to accept an integer from the user and handle the exception if the user enters non-numeric input.


import java.util.Scanner;
import java.util.InputMismatchException;

public class InputException {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        try {
            System.out.print("Enter an integer: ");
            int num = sc.nextInt();

            System.out.println("You entered: " + num);
        }

        catch (InputMismatchException e) {
            System.out.println("Error: Please enter a valid integer.");
        }
    }
}


Write a Java program to create a user-defined exception that is thrown when the age entered is less than 18.


import java.util.Scanner;

class AgeException extends Exception {
    AgeException(String message) {
        super(message);
    }
}

public class CustomExceptionDemo {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        try {
            System.out.print("Enter age: ");
            int age = sc.nextInt();

            if(age < 18)
                throw new AgeException("Age must be 18 or above.");

            System.out.println("Eligible.");
        }

        catch (AgeException e) {
            System.out.println(e.getMessage());
        }
    }
}


Write a Java program to demonstrate nested try-catch blocks.


public class NestedTry {
    public static void main(String[] args) {

        try {
            int arr[] = {10,20,30};

            try {
                System.out.println(arr[5]);
            }

            catch(ArrayIndexOutOfBoundsException e) {
                System.out.println("Inner catch: Invalid array index.");
            }

            int a = 10/0;
        }

        catch(ArithmeticException e) {
            System.out.println("Outer catch: Division by zero.");
        }
    }
}


Write a Java program to implement a stack using an array. Perform push and pop operations and handle the exceptions Stack Overflow (when the stack is full) and Stack Underflow (when the stack is empty).


import java.util.Scanner;

class Stack {
    int top = -1;
    int max = 5;
    int stack[] = new int[max];

    void push(int item) throws Exception {
        if (top == max - 1) {
            throw new Exception("Stack Overflow");
        }
        else {
            stack[++top] = item;
            System.out.println(item + " pushed into stack");
        }
    }

    void pop() throws Exception {
        if (top == -1) {
            throw new Exception("Stack Underflow");
        }
        else {
            System.out.println(stack[top--] + " popped from stack");
        }
    }

    void display() {
        if (top == -1) {
            System.out.println("Stack is empty");
        }
        else {
            System.out.println("Stack elements:");
            for (int i = top; i >= 0; i--) {
                System.out.println(stack[i]);
            }
        }
    }
}

public class StackExceptionDemo {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Stack s = new Stack();
        int choice;

        while (true) {
            System.out.println("\n1.Push 2.Pop 3.Display 4.Exit");
            System.out.print("Enter choice: ");
            choice = sc.nextInt();

            try {
                switch (choice) {
                    case 1:
                        System.out.print("Enter element: ");
                        int x = sc.nextInt();
                        s.push(x);
                        break;

                    case 2:
                        s.pop();
                        break;

                    case 3:
                        s.display();
                        break;

                    case 4:
                        System.exit(0);
                }
            }

            catch (Exception e) {
                System.out.println("Exception: " + e.getMessage());
            }
        }
    }
}


Comments

Popular Posts