EX06 custom calculator
package com.company;
import java.util.Scanner;
class invalidInputException extends Exception{
@Override
public String toString() {
return "Invalid input";
}
}
class notDivideByZero extends Exception{
@Override
public String toString() {
return "Cannot divide by zero";
}
}
class maxInputException extends Exception{
@Override
public String toString() {
return "Maximum input reached please enter value less and equal to 100000";
}
}
class maxMultiplierReached extends Exception{
@Override
public String toString() {
return "Stop please input is greater than 7000";
}
}
public class CWH_87_Ex06 {
public static void calculator() throws invalidInputException, notDivideByZero, maxInputException, maxMultiplierReached {
System.out.println("Enter the two number");
double c = 2.55d;
Scanner sc = new Scanner(System.in);
Scanner sc1 = new Scanner(System.in);
double a = sc.nextDouble();
double b = sc1.nextDouble();
if (a==8 || a==9 || b==8 || b==9) {
throw new invalidInputException();
}
if (a > 100000 || b > 100000) {
throw new maxInputException();
}
System.out.println("First number--> " + a);
System.out.println("Second number--> " + b);
Scanner sc2 = new Scanner(System.in);
System.out.println(" 1.Add \n 2.Substract \n 3.Multiply \n 4.Divide");
System.out.println("Choose which operation you want");
int d = sc2.nextInt();
switch (d) {
case 1:
System.out.println("Addition is " + (a + b));
break;
case 2:
System.out.println("Substraction is " +(a - b));
break;
case 3:
if (a>7000 || b>7000) {
throw new maxMultiplierReached();
}
System.out.println("Multiplication is "+(a * b));
break;
case 4:
if (b == 0) {
throw new notDivideByZero();
}
System.out.println("Division is "+(a / b));
break;
default:
System.out.println("Choose appropriate operation!!");
}
}
public static void main(String[] args) {
try {
calculator();
}
catch (invalidInputException e) {
System.out.println(e.toString());
}
catch (notDivideByZero e) {
System.out.println(e.toString());
}
catch (maxInputException e) {
System.out.println(e.toString());
}
catch (maxMultiplierReached e) {
System.out.println(e.toString());
}
}
}
Comments
Post a Comment