Skip to content

Commit 3b6a1e3

Browse files
committed
addd comments to code blocks
1 parent 2a9411e commit 3b6a1e3

1 file changed

Lines changed: 14 additions & 13 deletions

File tree

source/ch6_definingclasses.ptx

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -303,11 +303,12 @@ public Fraction(Integer num, Integer den) {
303303

304304
<program xml:id="java-fraction-class-addition" language="java">
305305
<code>
306-
public Fraction add(Fraction otherFrac) {
306+
public Fraction add(Fraction otherFrac) {
307307
Integer newNum = otherFrac.getDenominator() * this.numerator +
308-
this.denominator * otherFrac.getNumerator();
308+
this.denominator * otherFrac.getNumerator(); // notice the use of this.
309+
Integer newDen = this.denominator * otherFrac.getDenominator(); // find the new denominator
309310
Integer newDen = this.denominator * otherFrac.getDenominator();
310-
Integer common = gcd(newNum, newDen);
311+
Integer common = gcd(newNum, newDen); // find the greatest common divisor
311312
return new Fraction(newNum/common, newDen/common);
312313
}
313314
</code>
@@ -329,7 +330,7 @@ public Fraction add(Fraction otherFrac) {
329330
<code>
330331
public Fraction add(Fraction otherFrac) {
331332
Integer newNum = otherFrac.getDenominator() * numerator +
332-
denominator * otherFrac.getNumerator();
333+
denominator * otherFrac.getNumerator(); // notice the absence of this.
333334
Integer newDen = denominator * otherFrac.getDenominator();
334335
Integer common = gcd(newNum, newDen);
335336
return new Fraction(newNum/common, newDen/common);
@@ -388,12 +389,12 @@ public Fraction add(Fraction otherFrac) {
388389

389390
<program xml:id="java-method-overloading" language="java">
390391
<code>
391-
public Fraction(Integer num) {
392-
this.numerator = num;
392+
public Fraction(Integer num) {
393+
this.numerator = num; // set the numerator to the Integer
393394
this.denominator = 1;
394395
}
395-
public Fraction add(Integer other) {
396-
return add(new Fraction(other));
396+
public Fraction add(Integer other) { // overload the add method when the parameter is an Integer
397+
return add(new Fraction(other));
397398
}
398399
</code>
399400
</program>
@@ -415,11 +416,11 @@ public Fraction add(Integer other) {
415416
public class Fraction {
416417
private Integer numerator;
417418
private Integer denominator;
418-
public Fraction(Integer num, Integer den) {
419+
public Fraction(Integer num, Integer den) { // constructor that takes two Integers
419420
this.numerator = num;
420421
this.denominator = den;
421422
}
422-
public Fraction(Integer num) {
423+
public Fraction(Integer num) { // constructor that takes a single Integer, sets the denominator to 1
423424
this.numerator = num;
424425
this.denominator = 1;
425426
}
@@ -435,10 +436,10 @@ public class Fraction {
435436
Integer common = gcd(newNum,newDen);
436437
return new Fraction(newNum/common, newDen/common );
437438
}
438-
public Fraction add(Integer other) {
439+
public Fraction add(Integer other) { // overload the add method when the parameter is an Integer
439440
return add(new Fraction(other));
440441
}
441-
private static Integer gcd(Integer m, Integer n) {
442+
private static Integer gcd(Integer m, Integer n) { // a helper method for the add method
442443
while (m % n != 0) {
443444
Integer oldm = m;
444445
Integer oldn = n;
@@ -447,7 +448,7 @@ public class Fraction {
447448
}
448449
return n;
449450
}
450-
public static void main(String[] args) {
451+
public static void main(String[] args) { // a main method
451452
Fraction f1 = new Fraction(1,2);
452453
System.out.println(f1.add(1));
453454
}

0 commit comments

Comments
 (0)