-3

I'm trying to add three instance methods to the public interface of class 'Fraction' that all return a 'Fraction' as a result:

add, subtraction and multiplication. is it possible to change it from my current code into instance methods?

I just can't get it to work

Here is my code:

class Fraction {
    private Integer numerator;
    private Integer denumerator;

    public Fraction(Integer numerator, Integer denumerator) {

        int gcd = 1;

        for (int i = 1; i <= numerator && i <= denumerator; i++) {
            if (numerator % i == 0 && denumerator % i == 0)
                gcd = i;
        }
        this.numerator = numerator / gcd;
        this.denumerator = denumerator / gcd;
    }

    public Fraction(Integer numerator) {
        this.numerator = numerator;
        this.denumerator = 1;
    }

    public String toString() {

        return numerator + "/" + denumerator;
    }



    public static Fraction add(Fraction f1,Fraction f2){
        return new Fraction(f1.numerator*f2.denumerator+f2.numerator*f1.denumerator,f1.denumerator*f2.denumerator);
    }
    public static Fraction subtract(Fraction f1,Fraction f2){
        return new Fraction(f1.numerator*f2.denumerator-f2.numerator*f1.denumerator,f1.denumerator*f2.denumerator);
    }
    public static Fraction mul(Fraction f1,Fraction f2){
        return new Fraction(f1.numerator*f2.numerator,f1.denumerator*f2.denumerator);
    }
}

class Main
{
    public static void main(String[] arguments)
    {
        final Fraction HALF = new Fraction(1, 2);
        final Fraction THREE_FIFTH = new Fraction(3, 5);
        System.out.println(HALF.add(HALF, THREE_FIFTH).toString());
        System.out.println(THREE_FIFTH.subtract(HALF, THREE_FIFTH).toString());
        System.out.println(HALF.mul(HALF, THREE_FIFTH).toString());
    }
}

1 Answers1

1
public static Fraction add(Fraction f1,Fraction f2){
  return new Fraction(f1.numerator*f2.denumerator+f2.numerator*f1.denumerator,
                      f1.denumerator*f2.denumerator);
}

is a class method (because of the static it does not need an instance to call "on").

Making it instance method would look like

public Fraction add(Fraction other){
  return new Fraction(this.numerator*other.denumerator+other.numerator*this.denumerator,
                      this.denumerator*other.denumerator);
}

of course you do not actually need to write the thiss there, just they emphasize that f1 became the current object, and f2 became the single argument.

Then you could use it as

Fraction HALF = new Fraction(1, 2);
Fraction THREE_FIFTH = new Fraction(3, 5);
System.out.println(HALF.add(THREE_FIFTH));

without repeating HALF (like HALF.add(HALF,THREE_FIFTH) in the original code).

Side comment: class methods (static stuff) can be referred via the name of the class, your original code would be more conventionally called in the form Fraction.add(...):

System.out.println(Fraction.add(HALF,THREE_FIFTH));

(System.out.println() knows that it should call toString() so you do not actually need to do that yourself)

tevemadar
  • 12,389
  • 3
  • 21
  • 49
  • @UMLtotallydistilled yes, `subtract()` and `mul()` could be made into instance methods in the same way as shown with `add()` – tevemadar Feb 15 '20 at 18:32