@@ -303,11 +303,11 @@ 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();
309- Integer newDen = this.denominator * otherFrac.getDenominator();
310- Integer common = gcd(newNum, newDen);
308+ this.denominator * otherFrac.getNumerator(); // notice the use of this.
309+ Integer newDen = this.denominator * otherFrac.getDenominator(); // find the new denominator
310+ Integer common = gcd(newNum, newDen); // find the greatest common divisor
311311 return new Fraction(newNum/common, newDen/common);
312312}
313313 </code >
@@ -329,7 +329,7 @@ public Fraction add(Fraction otherFrac) {
329329 <code >
330330public Fraction add(Fraction otherFrac) {
331331 Integer newNum = otherFrac.getDenominator() * numerator +
332- denominator * otherFrac.getNumerator();
332+ denominator * otherFrac.getNumerator(); // notice the absence of this.
333333 Integer newDen = denominator * otherFrac.getDenominator();
334334 Integer common = gcd(newNum, newDen);
335335 return new Fraction(newNum/common, newDen/common);
@@ -388,12 +388,12 @@ public Fraction add(Fraction otherFrac) {
388388
389389 <program xml : id =" java-method-overloading" language =" java" >
390390 <code >
391- public Fraction(Integer num) {
392- this.numerator = num;
391+ public Fraction(Integer num) {
392+ this.numerator = num; // set the numerator to the Integer
393393 this.denominator = 1;
394394}
395- public Fraction add(Integer other) {
396- return add(new Fraction(other));
395+ public Fraction add(Integer other) { // overload the add method when the parameter is an Integer
396+ return add(new Fraction(other));
397397}
398398 </code >
399399 </program >
@@ -415,11 +415,11 @@ public Fraction add(Integer other) {
415415public class Fraction {
416416 private Integer numerator;
417417 private Integer denominator;
418- public Fraction(Integer num, Integer den) {
418+ public Fraction(Integer num, Integer den) { // constructor that takes two Integers
419419 this.numerator = num;
420420 this.denominator = den;
421421 }
422- public Fraction(Integer num) {
422+ public Fraction(Integer num) { // constructor that takes a single Integer, sets the denominator to 1
423423 this.numerator = num;
424424 this.denominator = 1;
425425 }
@@ -435,10 +435,10 @@ public class Fraction {
435435 Integer common = gcd(newNum,newDen);
436436 return new Fraction(newNum/common, newDen/common );
437437 }
438- public Fraction add(Integer other) {
438+ public Fraction add(Integer other) { // overload the add method when the parameter is an Integer
439439 return add(new Fraction(other));
440440 }
441- private static Integer gcd(Integer m, Integer n) {
441+ private static Integer gcd(Integer m, Integer n) { // a helper method for the add method
442442 while (m % n != 0) {
443443 Integer oldm = m;
444444 Integer oldn = n;
@@ -447,7 +447,7 @@ public class Fraction {
447447 }
448448 return n;
449449 }
450- public static void main(String[] args) {
450+ public static void main(String[] args) { // a main method
451451 Fraction f1 = new Fraction(1,2);
452452 System.out.println(f1.add(1));
453453 }
0 commit comments