-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustom Calculator using exception
More file actions
104 lines (88 loc) · 3.07 KB
/
Custom Calculator using exception
File metadata and controls
104 lines (88 loc) · 3.07 KB
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
100
101
102
103
104
package com.company;
/*
Question: You have to create a custom calculator with following operations:
1. + -> Addition
2. - -> Subtraction
3. * -> Multiplication
4. / -> Division
which throws the following exceptions:
1. Invalid input Exception ex: 8 & 9
2. Cannot divide by 0 Exception
3. Max Input Exception if any of the inputs is greater than 100000
4. Max Multiplier Reached Exception - Don't allow any multiplication input to be greater than 7000
*/
class InvalidInputException extends Exception {
public String toString() {
return "Cannot add 8 and 9";
}
public String getMessage() {
return "Getting Message";
}
}
class MaxInputException extends Exception {
public String toString() {
return "Number cannot be greater than 100000";
}
public String getMessage() {
return "Getting Message";
}
}
class CannotDivideByZeroException extends Exception {
public String toString() {
return "Cannot Divide By Zero";
}
public String getMassage() {
return "Getting Message";
}
}
class MaxMultiplyInputException extends Exception {
public String toString() {
return "Input Cannot be Greater than 7000";
}
public String getMassage() {
return "Getting Message";
}
}
class CustomCalculator {
double add(double a, double b) throws InvalidInputException, MaxInputException, MaxMultiplyInputException {
if (a > 100000 || b > 100000) {
throw new MaxInputException();
}
if (a == 8 || b == 9) {
throw new InvalidInputException();
}
return a + b;
}
double subst(double a, double b) throws MaxInputException {
if (a > 100000 || b > 100000) {
throw new MaxInputException();
}
return a - b;
}
double multiply(double a, double b) throws MaxInputException, MaxMultiplyInputException {
if (a > 100000 || b > 100000) {
throw new MaxInputException();
} else if (a > 7000 || b > 7000) {
throw new MaxMultiplyInputException();
}
return a * b;
}
double divide(double a, double b) throws CannotDivideByZeroException, MaxInputException {
if (a > 100000 || b > 100000) {
throw new MaxInputException();
}
if (b == 0) {
throw new CannotDivideByZeroException();
}
return a / b;
}
}
public class Main {
public static void main(String[] args) throws InvalidInputException, CannotDivideByZeroException, MaxInputException, MaxMultiplyInputException {
CustomCalculator cc = new CustomCalculator();
cc.add(8,9);
cc.divide(8, 0);
cc.multiply(3,444444);
cc.multiply(7000,7000);
}
}