Repository navigation
Expand file tree
/
Copy pathFraction.java
More file actions
124 lines (106 loc) · 2.94 KB
/
Copy pathFraction.java
File metadata and controls
124 lines (106 loc) · 2.94 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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
*
* @author Michelle Li
* Period 1
*
*/
// class Fraction
public class Fraction {
// instance variables to store the numerator and denominator of the fraction
private int numerator, denominator;
// constructor that takes no paramaters and defaults to 0
public Fraction() {
numerator = 0;
denominator = 1;
}
// constructor that initializes the fraction to the parameter value
public Fraction(int n) {
numerator = n;
denominator = 1;
}
// constructor that initializes the numerator and denominator of fraction to the parameters
public Fraction(int n, int d) {
numerator = n;
denominator = d;
this.reduce();
}
// constructor that initializes the fraction to a given fraction
public Fraction(Fraction other) {
numerator = other.numerator;
denominator = other.denominator;
}
// returns numerator
public int getNumerator() {
return numerator;
}
// returns denominator
public int getDenominator() {
return denominator;
}
// returns value of fraction
public double getValue() {
return numerator / (double) denominator;
}
// set the numerator of the fraction to the parameter value
public void setNumerator(int n) {
numerator = n;
}
// set the denominator of the fraction to the parameter value
public void setDenominator(int d) {
denominator = d;
}
// set the fraction to the parameter
public void setFraction(Fraction other) {
numerator = other.numerator;
denominator = other.denominator;
}
// add a fraction object to the fraction
public Fraction add(Fraction other) {
Fraction f = new Fraction();
f.numerator = this.numerator * other.denominator + this.denominator * other.numerator;
f.denominator = this.denominator * other.denominator;
f.reduce();
return f;
}
// add an integer to the fraction
public Fraction add(int m) {
Fraction f = new Fraction();
f.numerator = this.numerator + this.denominator * m;
f.denominator = this.denominator;
f.reduce();
return f;
}
// multiply the current fraction by another fraction
public Fraction multiply(Fraction other) {
Fraction f = new Fraction();
f.numerator = this.numerator * other.numerator;
f.denominator = this.denominator * other.denominator;
f.reduce();
return f;
}
// multiply the fraction by an integer
public Fraction multiply(int m) {
Fraction f = new Fraction();
f.numerator = this.numerator * m;
f.denominator = this.denominator;
f.reduce();
return f;
}
// reduce the fraction using Java gcf methods
public void reduce() {
// make sure denominator is positive
if (denominator < 0) {
numerator *= -1;
denominator *= -1;
}
// use MyMathLib for gcf
int gcf = MyMathLib.gcf(Math.abs(numerator), Math.abs(denominator));
numerator /= gcf;
denominator /= gcf;
}
// return the fraction in string format
public String toString() {
this.reduce();
return numerator + "/" + denominator;
}
}