-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathCalculator_in_java.java
100 lines (93 loc) · 2.72 KB
/
Calculator_in_java.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package com.company;
import java.util.Objects;
import java.util.Scanner;
class CannotDivideZeroException extends Exception{
@Override
public String toString() {
return "Cannot Divide by Zero!";
}
}
class MaxInputException extends Exception {
@Override
public String toString() {
return "Max Input Exceed!";
}
}
class MaxMultiplierReachedException extends Exception{
@Override
public String toString() {
return "Max Multiplication Inout Reached!";
}
}
public class Calculator_in_java {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
float a;
float b;
String c;
System.out.print("Enter first number: ");
a = sc.nextFloat();
System.out.print("Enter operation: ");
c = sc.next();
System.out.print("Enter second number: ");
b = sc.nextFloat();
if(Objects.equals(c, "+")){
try{
if((a>100000 || b>100000)){
throw new MaxInputException();
}
else{
System.out.println("The sum of two number is: " + (a + b));
}
}
catch (Exception e){
System.out.println(e.toString());
}
}
else if(Objects.equals(c, "-")){
try{
if((a>100000 || b>100000)){
throw new MaxInputException();
}
else{
System.out.println("The subtraction of two number is: " + (a - b));
}
}
catch (Exception e){
System.out.println(e.toString());
}
}
else if(Objects.equals(c, "*")){
try{
if((a>7000 || b>7000)){
throw new MaxMultiplierReachedException();
}
else{
System.out.println("The product of two numbers is: " + (a*b));
}
}
catch (Exception e){
System.out.println(e.toString());
}
}
else if(Objects.equals(c, "/")){
try{
if((a>100000 || b>100000)){
throw new MaxInputException();
}
else if((b == 0)){
throw new CannotDivideZeroException();
}
else{
System.out.println("The division of two numbers is: " + (a/b));
}
}
catch (Exception e){
System.out.println(e.toString());
}
}
else{
System.out.println("Invalid Input");
}
}
}