|
76 | 76 | """ |
77 | 77 | self.num = num |
78 | 78 | self.den = den |
79 | | - def __repr__(self): |
80 | | - if self.num > self.den: |
81 | | - retWhole = int(self.num / self.den) |
82 | | - retNum = self.num - (retWhole * self.den) |
| 79 | + def __repr__(self): |
| 80 | + """ |
| 81 | + :return: a string representation of the fraction |
| 82 | + """ |
| 83 | + if self.num > self.den: |
| 84 | + retWhole = int(self.num / self.den) # find the whole number part |
| 85 | + retNum = self.num - (retWhole * self.den) # find the numerator part |
83 | 86 | return str(retWhole) + " " + str(retNum) + "/" + str(self.den) |
84 | 87 | else: |
85 | 88 | return str(self.num) + "/" + str(self.den) |
86 | 89 | def show(self): |
| 90 | + """ |
| 91 | + :return: print the fraction |
| 92 | + """ |
87 | 93 | print(self.num, "/", self.den) |
88 | 94 | def __add__(self, other): |
| 95 | + """ |
| 96 | + :param other: the fraction to add |
| 97 | + :return: the sum of the two fractions |
| 98 | + """ |
89 | 99 | # convert to a fraction |
90 | 100 | other = self.toFract(other) |
91 | | - newnum = self.num * other.den + self.den * other.num |
92 | | - newden = self.den * other.den |
| 101 | + newnum = self.num * other.den + self.den * other.num # find the new numerator |
| 102 | + newden = self.den * other.den # find the new denominator |
93 | 103 | common = gcd(newnum, newden) |
94 | 104 | return Fraction(int(newnum / common), int(newden / common)) |
95 | | - __radd__ = __add__ |
| 105 | + __radd__ = __add__ # allow the fraction to be added to a number |
96 | 106 | def __lt__(self, other): |
| 107 | + """ |
| 108 | + :param other: the fraction to compare |
| 109 | + :return: whether the fraction is less than the other |
| 110 | + """ |
97 | 111 | num1 = self.num * other.den |
98 | 112 | num2 = self.den * other.num |
99 | 113 | return num1 < num2 |
100 | 114 | def toFract(self, n): |
| 115 | + """ |
| 116 | + :param n: the number to convert to a fraction |
| 117 | + :return: the fraction representation of the number |
| 118 | + """ |
101 | 119 | if isinstance(n, int): |
102 | 120 | other = Fraction(n, 1) |
103 | 121 | elif isinstance(n, float): |
104 | 122 | wholePart = int(n) |
105 | | - fracPart = n - wholePart |
106 | | - # convert to 100ths??? |
107 | | - fracNum = int(fracPart * 100) |
108 | | - newNum = wholePart * 100 + fracNum |
109 | | - other = Fraction(newNum, 100) |
| 123 | + fracPart = n - wholePart |
| 124 | + fracNum = int(fracPart * 100) # convert to 100ths |
| 125 | + newNum = wholePart * 100 + fracNum # combine the whole and fractional parts |
| 126 | + other = Fraction(newNum, 100) |
110 | 127 | elif isinstance(n, Fraction): |
111 | 128 | other = n |
112 | 129 | else: |
|
115 | 132 | return other |
116 | 133 | def gcd(m, n): |
117 | 134 | """ |
118 | | - A helper function for Fraction |
| 135 | + A helper function for Fraction. |
| 136 | + :param m: the first number |
| 137 | + :param n: the second number |
| 138 | + :return: the greatest common divisor |
119 | 139 | """ |
120 | | - while m % n != 0: |
| 140 | + while m % n != 0: # keep going until the gcd is found |
121 | 141 | oldm = m |
122 | 142 | oldn = n |
123 | 143 | m = oldn |
|
140 | 160 | <listing xml:id="java-fraction-class-start"> |
141 | 161 | <program interactive="activecode" language="java"> |
142 | 162 | <code> |
143 | | - public class Fraction { |
144 | | - private Integer numerator; |
| 163 | + public class Fraction { |
| 164 | + private Integer numerator; // notice the private modifier |
145 | 165 | private Integer denominator; |
146 | 166 | } |
147 | 167 | </code> |
|
157 | 177 | <listing xml:id="java-private"> |
158 | 178 | <program interactive="activecode" language="java"> |
159 | 179 | <code> |
160 | | - Fraction f = new Fraction(1,2); |
161 | | - Integer y = f.numerator * 10; |
| 180 | + Fraction f = new Fraction(1,2); |
| 181 | + Integer y = f.numerator * 10; // trying to access the numerator |
162 | 182 | </code> |
163 | 183 | </program> |
164 | 184 | </listing> |
|
174 | 194 | <listing xml:id="java-getter-and-setter"> |
175 | 195 | <program interactive="activecode" language="java"> |
176 | 196 | <code> |
177 | | -public Integer getNumerator() { |
| 197 | +public Integer getNumerator() { // getter method |
178 | 198 | return numerator; |
179 | 199 | } |
180 | | -public void setNumerator(Integer numerator) { |
| 200 | +public void setNumerator(Integer numerator) { // setter method |
181 | 201 | this.numerator = numerator; |
182 | 202 | } |
183 | | -public Integer getDenominator() { |
| 203 | +public Integer getDenominator() { // getter method |
184 | 204 | return denominator; |
185 | 205 | } |
186 | | -public void setDenominator(Integer denominator) { |
| 206 | +public void setDenominator(Integer denominator) { // setter method |
187 | 207 | this.denominator = denominator; |
188 | 208 | } |
189 | 209 | </code> |
|
0 commit comments