-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEx06_CalWithException.java
More file actions
93 lines (85 loc) · 2.25 KB
/
Ex06_CalWithException.java
File metadata and controls
93 lines (85 loc) · 2.25 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
package com.company;
class InvalidInputException extends Exception {
@Override
public String toString() {
return "Cannot add 8 & 9";
}
@Override
public String getMessage() {
return super.getMessage();
}
}
class DivideByZero extends Exception {
@Override
public String toString() {
return "Cannot divide by 0";
}
@Override
public String getMessage() {
return super.getMessage();
}
}
class MaxMultiplicationInput extends Exception {
@Override
public String toString() {
return "Cannot multiply over 7000";
}
@Override
public String getMessage() {
return super.getMessage();
}
}
class MaxInputReached extends Exception {
@Override
public String toString() {
return "Max limit of input is 10000";
}
@Override
public String getMessage() {
return super.getMessage();
}
}
class CustomCal {
public int add(int a, int b) throws InvalidInputException, MaxInputReached {
if (a==8 && b==9) {
throw new InvalidInputException();
}
if (a>10000 && b>10000) {
throw new MaxInputReached();
}
return a+b;
}
public int sub(int a, int b) throws MaxInputReached {
if (a>10000 && b>10000) {
throw new MaxInputReached();
}
return a-b;
}
public int mul(int a, int b) throws MaxInputReached, MaxMultiplicationInput {
if (a>10000 && b>10000) {
throw new MaxInputReached();
}
if (a>7000 || b>7000) {
throw new MaxMultiplicationInput();
}
return a*b;
}
public int div(int a, int b) throws MaxInputReached, DivideByZero {
if (a>10000 && b>10000) {
throw new MaxInputReached();
}
if (b==0) {
throw new DivideByZero();
}
return a/b;
}
}
public class Ex06_CalWithException {
public static void main(String[] args) throws InvalidInputException, MaxInputReached, MaxMultiplicationInput, DivideByZero{
CustomCal c = new CustomCal();
System.out.println(c.add(7,9));
System.out.println(c.div(8,4));
System.out.println(c.mul(45,63));
System.out.println(c.sub(45,45));
}
}