-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFraction.java
99 lines (82 loc) · 3.15 KB
/
Fraction.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
public class Fraction {
private int numerator = 0;
private int denominator = 0;
//2 parameter constructor
public Fraction(int numerator, int denominator) {
this.numerator = numerator;
if (denominator == 0) { //check 4 zero
throw new IllegalArgumentException(Integer.toString(denominator));
} else if (denominator < 0) {
this.numerator *= -1;
this.denominator = (-1) * denominator;
} else {
this.denominator = denominator;
}
}
//1 parameter constructor
public Fraction(int numerator) {
this(numerator, 1);
}
public int getNumer() {
return this.numerator;
}
public int getDenom() {
return this.denominator;
}
public String toString() {
return this.numerator + "/" + this.denominator;
}
//result of numerator / denominator
public double toDouble() {
return (double) numerator / denominator; //change type to double so that integer division won't happen
}
//Greatest Common Divisor
private static int gcd(int num, int den) {
if (den == 0) {
return num;
}
return gcd(den, num % den);
}
//Lowest Common Multiple
private static int lcm(int den1, int den2) {
int numGCD = gcd(den1, den2);
return (den1 * den2) / numGCD;
}
public Fraction add(Fraction other) {
int fracDenominator = lcm(this.denominator, other.denominator);
int fracNumerator = ((fracDenominator / this.denominator) * this.numerator) + ((fracDenominator / other.denominator) * other.numerator);
Fraction result = new Fraction(fracNumerator, fracDenominator);
return result;
}
public Fraction subtract(Fraction other) {
int fracDenominator = lcm(this.denominator, other.denominator);
int fracNumerator = ((fracDenominator / this.denominator) * this.numerator) - ((fracDenominator / other.denominator) * other.numerator);
Fraction result = new Fraction(fracNumerator, fracDenominator);
return result;
}
public Fraction multiply(Fraction other) {
int fracNumerator = this.numerator * other.numerator;
int fracDenominator = this.denominator * other.denominator;
Fraction result = new Fraction(fracNumerator, fracDenominator);
return result;
}
public Fraction divide(Fraction other) {
if (other.denominator == 0) { //check 4 zero
throw new IllegalArgumentException(Integer.toString(denominator));
}
int fracNumerator = this.numerator * other.denominator;
int fracDenominator = this.denominator * other.numerator;
Fraction result = new Fraction(fracNumerator, fracDenominator);
return result;
}
//lower terms
public void toLowestTerms() {
int numGCD = gcd(this.numerator, this.denominator);
this.numerator /= numGCD;
this.denominator /= numGCD;
if (this.denominator < 0) {
this.numerator *= -1;
this.denominator *= -1;
}
}
}