Hi All,
I write a program for Arithmetic operation and basic exception handling techniques for avoid run time errors.
It is used in mathematical expressions in the same way that they are used in algebra. The following table lists the arithmetic operators:
Operator | Description | Example |
---|---|---|
+ | Addition - Adds values on either side of the operator | A + B will give 30 |
- | Subtraction - Subtracts right hand operand from left hand operand | A - B will give -10 |
* | Multiplication - Multiplies values on either side of the operator | A * B will give 200 |
/ | Division - Divides left hand operand by right hand operand | B / A will give 2 |
% | Modulus - Divides left hand operand by right hand operand and returns remainder | B % A will give 0 |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Sify
*/
public class arithMetic {
/* Addtion opertion*/
public void additionMethod(int num, int num1) {
System.out.println("Addition operator (+) .");
System.out.println("Addition of two number : " + (num + num1) + "\n");
}
/* Subtraction opertion*/
public void subtractionMethod(int num, int num1) {
System.out.println("Subtraction operator (-).");
System.out.println("Subtraction of two number : " + (num - num1) + "\n");
}
/* Multiplication opertion*/
public void multiplicationMethod(int num, int num1) {
System.out.println("Multiplication operator (*).");
System.out.println("Multiplication of two number : " + (num * num1) + "\n");
}
/* Division opertion*/
public void divisionMethod(int num, int num1) {
System.out.println("Division operator (/).");
try {
if (num < num1) {
System.out.println("Please check the values No " + num + " lessthen No " + num1);
}
if (num1 != 0) {
System.out.println("Division of two number : " + (num / num1) + "\n");
}
} catch (Exception e) {
System.out.println("division by zero not passible :-(");
}
}
/* Modulo opertion*/
public void moduloMethod(int num, int num1) {
System.out.println("Modulo operator (%).");
try{
System.out.println("Modulo of two number : " + (num % num1) + "\n");
}catch (Exception e) {
System.out.println("division by zero not passible :-(");
}
}
public static void main(String[] args) throws IOException {
BufferedReader object = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter the first number: \t");
int a = Integer.parseInt(object.readLine());
System.out.println("\nPlease enter the Second number: \t");
int b = Integer.parseInt(object.readLine());
arithMetic arith = new arithMetic();
if ((a < 32767) && (b <= 32767)) {
/*Methods calling*/
arith.additionMethod(a, b);
arith.subtractionMethod(a, b);
arith.multiplicationMethod(a, b);
arith.divisionMethod(a, b);
arith.moduloMethod(a, b);
} else {
System.out.println("Integer number out of range. Check Again :-(");
}
}
}
OUTPUT
~~~~~~~~~
Please enter the first number:10
Please enter the Second number:
2
Addition operator (+) .
Addition of two number : 12
Subtraction operator (-).
Subtraction of two number : 8
Multiplication operator (*).
Multiplication of two number : 20
Division operator (/).
Division of two number : 5
Modulo operator (%).
Modulo of two number : 0
No comments:
Post a Comment