Skip to content

Commit 1997b67

Browse files
committed
addd comments to code
1 parent 4758771 commit 1997b67

1 file changed

Lines changed: 42 additions & 22 deletions

File tree

source/ch6_definingclasses.ptx

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -76,37 +76,54 @@
7676
"""
7777
self.num = num
7878
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
8386
return str(retWhole) + " " + str(retNum) + "/" + str(self.den)
8487
else:
8588
return str(self.num) + "/" + str(self.den)
8689
def show(self):
90+
"""
91+
:return: print the fraction
92+
"""
8793
print(self.num, "/", self.den)
8894
def __add__(self, other):
95+
"""
96+
:param other: the fraction to add
97+
:return: the sum of the two fractions
98+
"""
8999
# convert to a fraction
90100
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
93103
common = gcd(newnum, newden)
94104
return Fraction(int(newnum / common), int(newden / common))
95-
__radd__ = __add__
105+
__radd__ = __add__ # allow the fraction to be added to a number
96106
def __lt__(self, other):
107+
"""
108+
:param other: the fraction to compare
109+
:return: whether the fraction is less than the other
110+
"""
97111
num1 = self.num * other.den
98112
num2 = self.den * other.num
99113
return num1 < num2
100114
def toFract(self, n):
115+
"""
116+
:param n: the number to convert to a fraction
117+
:return: the fraction representation of the number
118+
"""
101119
if isinstance(n, int):
102120
other = Fraction(n, 1)
103121
elif isinstance(n, float):
104122
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)
110127
elif isinstance(n, Fraction):
111128
other = n
112129
else:
@@ -115,9 +132,12 @@
115132
return other
116133
def gcd(m, n):
117134
"""
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
119139
"""
120-
while m % n != 0:
140+
while m % n != 0: # keep going until the gcd is found
121141
oldm = m
122142
oldn = n
123143
m = oldn
@@ -140,8 +160,8 @@
140160
<listing xml:id="java-fraction-class-start">
141161
<program interactive="activecode" language="java">
142162
<code>
143-
public class Fraction {
144-
private Integer numerator;
163+
public class Fraction {
164+
private Integer numerator; // notice the private modifier
145165
private Integer denominator;
146166
}
147167
</code>
@@ -157,8 +177,8 @@
157177
<listing xml:id="java-private">
158178
<program interactive="activecode" language="java">
159179
<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
162182
</code>
163183
</program>
164184
</listing>
@@ -174,16 +194,16 @@
174194
<listing xml:id="java-getter-and-setter">
175195
<program interactive="activecode" language="java">
176196
<code>
177-
public Integer getNumerator() {
197+
public Integer getNumerator() { // getter method
178198
return numerator;
179199
}
180-
public void setNumerator(Integer numerator) {
200+
public void setNumerator(Integer numerator) { // setter method
181201
this.numerator = numerator;
182202
}
183-
public Integer getDenominator() {
203+
public Integer getDenominator() { // getter method
184204
return denominator;
185205
}
186-
public void setDenominator(Integer denominator) {
206+
public void setDenominator(Integer denominator) { // setter method
187207
this.denominator = denominator;
188208
}
189209
</code>

0 commit comments

Comments
 (0)