Problem:9.1

 Question 9.1 :

• Write a program that creates a Calculator class. The class contains two variables of

integer type. Design a constructor that accepts two values as parameter and set those

values.

• Design four methods named Add (), Subtract (), multiply (), Division ( ) for performing addition, subtraction, multiplication and division of two numbers.

• For addition and subtraction, two numbers should be positive. If any negative number is entered then throw an exception in respective methods. So design an exception handler

(ArithmeticException) in Add () and Subtract () methods respectively to check whether any number is negative or not.

• For division and multiplication two numbers should not be zero. If zero is entered for any number then throw an exception in respective methods. So design an exception handler

(ArithmeticException) in multiply () and Division () methods respectively to check whether any number is zero or not.

• Write a main class and declare four objects of Calculator class. Perform addition (obj1), subtraction (obj2), multiply (obj3) and division (obj4) operations for these objects. If any non integer values are provided as input; then you should throw an exception (NumberFormatException) and display a message that informs the user of the wrong input before exiting.


Solution: 


package lab9problem1;

import java.io.IOException;
import java.util.Scanner;

public class Calculator {
    int first, second;
    public Calculator(int first, int second) {
        this.first = first;
        this.second = second;
    }
    void Add() {
        if (first >= 0 && second >= 0) {
            System.out.println(first + second);
        } else {
            try {
                int c = first + second;
                System.out.println("Result = " + c);
            } catch (ArithmeticException arithmeticException) {
                System.out.println(arithmeticException);
            }
        }
    

    void Subtract() {
        if (first >= 0 && second >= 0) {
            System.out.println(first - second);
        } else {
            try {
                int c = first - second;
                System.out.println("Result = " + c);
            } catch (ArithmeticException arithmeticException) {
                System.out.println(arithmeticException);

            }
        }
    }
    void Multiply() {
        try {
            int result = first * second;
            System.out.println(result);
        } catch (ArithmeticException arithmeticException) {
            System.out.println(arithmeticException);
        }
    }
    void Division() {
        try {
            int result = first / second;
            System.out.println(result);
        } catch (ArithmeticException arithmeticException) {
            System.out.println(arithmeticException);
        }
    }
}

class ArithmeticException_check {
    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        int a = input.nextInt();
        int b = input.nextInt();
        Calculator obj = new Calculator(a, b);
        obj.Add();
    }
}
}
Previous Post Next Post