forked from miafg/fun-with-github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDumbMath.java
71 lines (58 loc) · 1.76 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* This class contains some dumb methods for doing math.
* Hence the name.
* @author chris
*
*/
public class DumbMath {
/**
add(int a, int b)
Adds two integers a, b together and returns the result
*/
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;
}
/**
add(double a, double b)
Adds two doubles a, b together and returns the result
*/
public static double add(double a, double b) {
double sum = a + b;
// check for overflow
if (Double.isInfinite(sum))
throw new ArithmeticException("Overflow when adding " + a + " and " + b);
// check for underflow
if (Double.isInfinite(1/sum))
throw new ArithmeticException("Underflow when adding " + a + " and " + b);
System.out.println(sum);
return sum;
}
/**
multiply(int a, int b)
Multiplies two numbers a, b together and returns the result
*/
public static int multiply(int a, int b) {
int product = a * b;
return product;
}
/**
addFraction(int n1, int d1, int n2, int d2)
Adds two fractions n1/d1 and n2/d2 together
returns the resulting sum which has the value
n1/d1 + n2/d2
*/
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;
}
}