-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRational.java
More file actions
63 lines (50 loc) · 1.48 KB
/
Rational.java
File metadata and controls
63 lines (50 loc) · 1.48 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
public class Rational {
long numerator,denominator;
class Illegal extends Exception {
String reason;
Illegal (String reason) {
this.reason = reason;
}
}
Rational() {
super();
}
Rational(long numerator, long denominator) throws Illegal {
/* add your implementation here */
}
// find the reduce form
private void simplestForm() {
long computeGCD;
computeGCD = GCD(Math.abs(numerator), denominator);
numerator /= computeGCD;
denominator /= computeGCD;
}
// find the greatest common denominator
private long GCD(long a, long b) {
if (a%b ==0) return b;
else return GCD(b,a%b);
}
public void add(Rational x) {
numerator = (numerator * x.denominator) + (x.numerator * denominator);
denominator = (denominator * x.denominator);
simplestForm();
}
public void subtract(Rational x) {
/* add your implementation here */
}
public void multiply(Rational x) {
/* add your implementation here */
}
public void divide(Rational x) {
/* add your implementation here */
}
public boolean equals(Object x) {
/* add your implementation here */
}
public long compareTo(Object x) {
/* add your implementation here */
}
public String toString() {
/* add your implementation here */
}
}