-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcwh_103_custom_calc.java
132 lines (98 loc) · 2.65 KB
/
cwh_103_custom_calc.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package javatutorial;
class InvalidInputException extends Exception{
@Override
public String toString() {
return "Cannot add 8 and 9";
}
@Override
public String getMessage() {
return "I am getMessage()" ;
}
}
class CannotDivideByZeroException extends Exception{
@Override
public String toString() {
return "Cannot divide by 0";
}
@Override
public String getMessage() {
return "I am getMessage()" ;
}
}
class MaxInputException extends Exception{
@Override
public String toString() {
return "number cannot be greater than 100000";
}
@Override
public String getMessage() {
return "I am getMessage()" ;
}
}
class MaxMultiplyInputException extends Exception{
@Override
public String toString() {
return "Input cant be greater than 7000 while multiplying";
}
@Override
public String getMessage() {
return "I am getMessage()" ;
}
}
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();
}
else if(b==0) {
throw new CannotDivideByZeroException();
}
return a/b;
}
}
public class cwh_103_custom_calc {
public static void main(String[] args) throws InvalidInputException, CannotDivideByZeroException, MaxInputException , MaxMultiplyInputException {
CustomCalculator c = new CustomCalculator();
//c.add(8, 9); // 1.
//c.divide(6, 0); // 2.
//c.divide(6000000, 40);// 3.
c.multiply(5, 9000); // 4.
/*
/*
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
*/
}
}