forked from miafg/fun-with-github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DumbMath.java
50 lines (36 loc) · 1.04 KB
/
DumbMath.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
/**
* This class contains some dumb methods for doing math.
* Hence the name.
* @author chris
*
*/
public class DumbMath {
public static int add(int a, int b) {
int sum = a + b;
// check for overflow
if (a > 0 && b > 0 && sum < 0)
throw new ArithmeticException("Overflow when adding " + a + " and " + b);
// check for underflow
if (a < 0 && b < 0 && sum > 0)
throw new ArithmeticException("Underflow when adding " + a + " and " + b);
return sum;
}
public static double add(double a, double b) {
double sum = a + b;
// check for overflow
if (a > 0 && b > 0 && sum < 0)
throw new ArithmeticException("Overflow when adding " + a + " and " + b);
return sum;
}
public static int multiply(int a, int b) {
int product = a * b;
return product;
}
public static double addFraction(int n1, int d1, int n2, int d2) {
if (d1 == 0 || d2 == 0)
throw new IllegalArgumentException("Denominator is zero!");
double f1 = ((double)n1)/d1;
double f2 = ((double)n2)/d2;
return f1 + f2;
}
}