/
RatNum.java
79 lines (69 loc) · 2.11 KB
/
RatNum.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
72
73
74
75
76
77
78
79
/**
* RatNum is an immutable type representing rational numbers.
*/
public class RatNum {
private final int numerator;
private final int denominator;
// Rep invariant:
// denominator > 0
// numerator/denominator is in reduced form, i.e. gcd(|numerator|,denominator) = 1
// Abstraction function:
// AF(numerator, denominator) = numerator/denominator
// Safety from rep exposure:
// All fields are private and immutable.
/**
* Make a new RatNum == n.
* @param n value
*/
public RatNum(int n) {
numerator = n;
denominator = 1;
checkRep();
}
/**
* Make a new RatNum == (n / d).
* @param n numerator
* @param d denominator
* @throws ArithmeticException if d == 0
*/
public RatNum(int n, int d) throws ArithmeticException {
// reduce ratio to lowest terms
int g = gcd(n, d);
n = n / g;
d = d / g;
// make denominator positive
if (d < 0) {
numerator = -n;
denominator = -d;
} else {
numerator = n;
denominator = d;
}
checkRep();
}
/////////////////////////////////////////
// other methods should go here
// producers: add(), subtract(), multiply(), divide(), etc.
// observers: isPositive(), intValue(), etc.
// mutators: none
// Check that the rep invariant is true
// *** Warning: this does nothing unless you turn on assertion checking
// by passing -enableassertions to Java
private void checkRep() {
assert denominator > 0;
assert gcd(Math.abs(numerator), denominator) == 1;
}
/**
* @return a string representation of this rational number
*/
@Override
public String toString() {
checkRep();
return (denominator > 1) ? (numerator + "/" + denominator) : (numerator + "");
}
// compute greatest common divisor of a and b
private static int gcd(int a, int b) {
return (b == 0) ? a : gcd(b, a % b);
}
// TODO: this immutable type needs equals() and hashCode()
}