-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathException_example_IMP.java
92 lines (82 loc) · 2.53 KB
/
Exception_example_IMP.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
package com.company;
/*
Exercise 6: 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{
@Override
public String toString() {
return "Cannot add 8 and 9";
}
}
class MaxInputException extends Exception{
@Override
public String toString() {
return "Input can't be greater than 100000";
}
}
class CannotDivideByZeroException extends Exception{
@Override
public String toString() {
return "Cannot divide by 0";
}
}
class MaxMultiplyInputException extends Exception{
@Override
public String toString() {
return "Input cant be greater than 7000 while multiplying";
}
}
class CustomCalculator {
double add(double a, double b) throws InvalidInputException, MaxInputException{
if(a>100000 || b>100000){
throw new MaxInputException();
}
if(a==8 || b==9) {
throw new InvalidInputException();
}
return a + b;
}
double subtract(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 Exception_example_IMP {
public static void main(String[] args) throws InvalidInputException,
CannotDivideByZeroException, MaxInputException, MaxMultiplyInputException {
CustomCalculator c = new CustomCalculator();
// c.add(8, 9);
// c.divide(6, 0);
// c.divide(600000000, 40);
c.multiply(5, 9888);
}
}