-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator_with_error_excecption.java
More file actions
115 lines (99 loc) · 3.87 KB
/
calculator_with_error_excecption.java
File metadata and controls
115 lines (99 loc) · 3.87 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
106
107
108
109
110
111
112
113
114
115
import java.util.*;
// Error and Exception***
class CanNotDivideByZeroException extends Exception{
@Override
public String toString(){
return "Can not Devide by zero(0)";
}
}
class MaxInputException extends Exception{
@Override
public String toString(){
return "Max input Exception because fix length is 100000 number()";
}
}
class MaxMultiplyException extends Exception{
@Override
public String toString(){
return "Max input Exception because fix length is 100000 number()";
}
}
// Methods of custom calculator***
class custom_calculator{
double Add(double a, double b) throws MaxInputException {
if (a >= 100000 || b >= 100000){
throw new MaxInputException();
}
return a + b;
}
double Sub(double a, double b) throws MaxInputException {
if (a >= 100000 || b >= 100000){
throw new MaxInputException();
}
return a - b;
}
double Mul(double a, double b) throws MaxInputException {
if (a >= 100000 || b >= 100000 && a*b >= 100000){
throw new MaxInputException();
}
return a * b;
}
double Div(double a, double b) throws MaxInputException, CanNotDivideByZeroException {
if(b == 0){
throw new CanNotDivideByZeroException();
}
if (a >= 100000 || b >= 100000){
throw new MaxInputException();
}
return a / b;
}
double Mod(double a, double b) throws MaxInputException {
if (a >= 100000 || b >= 100000){
throw new MaxInputException();
}
return a % b;
}
}
// Methods calling***
public class calculator_with_error_excecption {
public static void main(String[] args) throws MaxInputException, CanNotDivideByZeroException{
Scanner sc = new Scanner(System.in);
// System.out.print("Select only one option.\n1. Addition\n2. Subtraction\n3.
// Multiplication\n4. Divition\n");
while (true) {
System.out.print(
"1. Press 1 for Addition.\n2. Press 2 for Subtration.\n3. Press 3 for Multiplication.\n4. Press 4 for Divition.\n5. Press 5 for Modulus.\nChoose your choice : ");
int choice = sc.nextInt();
if (choice >= 6) {
System.out.println("Sorry! your choice is greater than 4 so you're exit the loop.....");
break;
}
System.out.print("Entetr number first : ");
double num1 = sc.nextDouble();
System.out.print("Entetr number second : ");
double num2 = sc.nextDouble();
if (choice == 1) {
custom_calculator t1 = new custom_calculator();
System.out.println("The Sum of two number : " + t1.Add(num1, num2));
} else if (choice == 2) {
custom_calculator t2 = new custom_calculator();
System.out.println("The Subtarct of two number : " + t2.Sub(num1, num2));
} else if (choice == 3) {
custom_calculator t3 = new custom_calculator();
System.out.println("The Mulitilication of two number : " + t3.Mul(num1, num2));
} else if (choice == 4) {
custom_calculator t4 = new custom_calculator();
System.out.println("The Divition of two number : " + t4.Div(num1, num2));
} else if (choice == 5) {
custom_calculator t5 = new custom_calculator();
System.out.println("The Modulus of two number : " + t5.Mod(num1, num2));
}
System.out.print("Let's do next calculation? [YES(1)/NO(0)] : ");
int Next_cal = sc.nextInt();
if (Next_cal == 0) {
break;
}
}
sc.close();
}
}