- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise_6_sol.java
More file actions
Latest commit
105 lines (97 loc) · 2.88 KB
/
Copy pathExercise_6_sol.java
File metadata and controls
105 lines (97 loc) · 2.88 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
105
classInvalidInputExceptionextendsException{
@Override
publicStringtoString() {
return"Cannot add 8 and 9";
}
@Override
publicStringgetMessage() {
return"I am getMessage()";
}
}
classMaxInputExceptionextendsException{
@Override
publicStringtoString() {
return"Input cant be greater than 100000";
}
@Override
publicStringgetMessage() {
return"I am getMessage()";
}
}
classCannotDivideByZeroExceptionextendsException{
@Override
publicStringtoString() {
return"Cannot divide by 0";
}
@Override
publicStringgetMessage() {
return"I am getMessage()";
}
}
classMaxMultiplyInputExceptionextendsException{
@Override
publicStringtoString() {
return"Input cant be greater than 7000 while multiplying";
}
@Override
publicStringgetMessage() {
return"I am getMessage()";
}
}
classCustomCalculator {
doubleadd(doublea, doubleb) throwsInvalidInputException, MaxInputException{
if(a>100000 || b>100000){
thrownewMaxInputException();
}
if(a==8 || b==9) {
thrownewInvalidInputException();
}
returna + b;
}
doublesubtract(doublea, doubleb) throwsMaxInputException{
if(a>100000 || b>100000){
thrownewMaxInputException();
}
returna - b;
}
doublemultiply(doublea, doubleb)throwsMaxInputException, MaxMultiplyInputException{
if(a>100000 || b>100000){
thrownewMaxInputException();
}
elseif(a>7000 || b>7000){
thrownewMaxMultiplyInputException();
}
returna * b;
}
doubledivide(doublea, doubleb) throwsCannotDivideByZeroException, MaxInputException{
if(a>100000 || b>100000){
thrownewMaxInputException();
}
if(b==0){
thrownewCannotDivideByZeroException();
}
returna / b;
}
}
publicclassExercise_6_sol {
publicstaticvoidmain(String[] args) throwsInvalidInputException,
CannotDivideByZeroException, MaxInputException, MaxMultiplyInputException {
CustomCalculatorc = newCustomCalculator();
// c.add(8, 9);
// c.divide(6, 0);
// c.divide(600000000, 40);
c.multiply(5, 9888);
/*
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
*/
}
}