-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArithmetic.java
More file actions
226 lines (183 loc) · 7.66 KB
/
Arithmetic.java
File metadata and controls
226 lines (183 loc) · 7.66 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package calculator;
import java.util.Scanner;
/**
*
* @author al
*/
public class Arithmetic {
public static Scanner kb = new Scanner(System.in);
public static byte choice;
public static String YesOrNo;
public static void Choice() {
try {
do {
System.out.println(" Press:");
System.out.println(" 1 for Addition");
System.out.println(" 2 for Subtraction");
System.out.println(" 3 for Multiplication");
System.out.println(" 4 for Division");
System.out.println(" 5 to go back");
System.out.println("");
choice = kb.nextByte();
switch (choice) {
case 1:
Addition();
break;
case 2:
Subtraction();
break;
case 3:
Multiplication();
break;
case 4:
Division();
break;
case 5:
break;
default:
System.out.println("");
System.out.println("");
System.out.println("Please enter only between 1 - 5");
Choice();
break;
}
} while (choice != 5);
} catch (java.util.InputMismatchException ex) {
System.out.println("");
System.out.println("I N P U T E R R O R");
System.out.println(ex.toString());
System.out.println("T R Y A G A I N");
System.out.println("");
} catch (Exception ex) {
System.out.println(ex.toString());
}
}
public static void Addition() {
try {
do {
System.out.println("A D D I T I O N");
System.out.println("");
String input = kb.next();
String[] inputArr = input.split("\\+");
double[] dblArr = new double[inputArr.length];
for (int i = 0; i < inputArr.length; i++) {
String numberAsString = inputArr[i];
dblArr[i] = Double.parseDouble(numberAsString);
}
double sum = 0;
for (double number : dblArr) {
sum = sum + number;
}
System.out.print("The sum is: ");
System.out.printf(Option.getFormat() + "\n", sum);
System.out.println("Do you want to do it again? Y/N or y/n");
YesOrNo = kb.next();
} while (!YesOrNo.equalsIgnoreCase("n"));
} catch (Exception ex) {
// System.out.println(ex.toString());
}
}
public static void Subtraction() {
try {
do {
YesOrNo = "y";
System.out.println("S U B T R A C T I O N");
System.out.println("");
String input = kb.next();
String[] inputArr = input.split("\\-");
double[] dblArr = new double[inputArr.length];
for (int i = 0; i < inputArr.length; i++) {
String numberAsString = inputArr[i];
dblArr[i] = Double.parseDouble(numberAsString);
}
double difference = 0;
if (dblArr.length == 2) {
difference = dblArr[0] - dblArr[1];
} else if (dblArr.length >= 3) {
difference = dblArr[0] - dblArr[1];
for (int i = 2; i < inputArr.length; i++) {
difference -= dblArr[i];
}
} else {
System.out.println("You need at least 2 numbers to subtract");
}
System.out.print("The difference is: ");
System.out.printf(Option.getFormat() + "\n", difference);
System.out.println("Do you want to do it again? Y/N or y/n");
YesOrNo = kb.next();
} while (!YesOrNo.equalsIgnoreCase("n"));
} catch (Exception ex) {
// System.out.println(ex.toString());
}
}
public static void Multiplication() {
try {
do {
System.out.println("M U L T I P L I C A T I O N");
System.out.println("");
String input = kb.next();
String[] inputArr = input.split("\\*");
double[] dbleArr = new double[inputArr.length];
for (int i = 0; i < inputArr.length; i++) {
String numberAsString = inputArr[i];
dbleArr[i] = Double.parseDouble(numberAsString);
}
double product = 0;
if (dbleArr.length == 2) {
product = dbleArr[0] * dbleArr[1];
} else if (dbleArr.length >= 3) {
product = dbleArr[0] * dbleArr[1];
for (int i = 2; i < inputArr.length; i++) {
product *= dbleArr[i];
}
} else {
System.out.println("You need at least 2 numbers to product");
}
System.out.print("The product is: ");
System.out.printf(Option.getFormat() + "\n", product);
System.out.println("Do you want to do it again? Y/N or y/n");
YesOrNo = kb.next();
} while (!YesOrNo.equalsIgnoreCase("n"));
} catch (Exception ex) {
// System.out.println(ex.toString());
}
}
public static void Division() {
try {
do {
System.out.println("D I V I S I O N");
System.out.println("");
String input = kb.next();
String[] inputArr = input.split("\\/");
double[] dblArr = new double[inputArr.length];
for (int i = 0; i < inputArr.length; i++) {
String numberAsString = inputArr[i];
dblArr[i] = Double.parseDouble(numberAsString);
}
double quotient = 0;
if (dblArr.length == 2) {
quotient = dblArr[0] / dblArr[1];
} else if (dblArr.length >= 3) {
quotient = dblArr[0] / dblArr[1];
for (int i = 2; i < inputArr.length; i++) {
quotient /= dblArr[i];
}
} else {
System.out.println("You need at least 2 numbers to divide");
}
System.out.print("The quotient is: ");
System.out.printf(Option.getFormat() + "\n", quotient);
System.out.println("");
System.out.println("Do you want to do it again? Y/N or y/n");
YesOrNo = kb.next();
} while (!YesOrNo.equalsIgnoreCase("n"));
} catch (Exception ex) {
// System.out.println(ex.toString());
}
}
}