Search code examples
javaclassmethodsaggregation

How to aggregate methods from another class in Java?


I have a class Fraction with the arithmetic operations for fractions. Here is an abstract of my class Fraction. (I've included only method of addition.)

package com.company;

import java.util.Scanner;

public class Fraction {
    private int num; // numerator
    private int denom; // denominator

    public Fraction() {
        super();
    }

    public Fraction(int num, int denom) {
        super();
        this.num = num;
        this.denom = denom;
        if (denom == 0) {
            this.denom = 1;
        }
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public int getDenom() {
        return denom;
    }

    public void setDenom(int denom) {
        if (denom > 0) {
            this.denom = denom;
        }
    }

    public void inputFraction() {
        Scanner innum = new Scanner(System.in);
        System.out.println("Input numerator: ");
        num = innum.nextInt();

        Scanner indenom = new Scanner(System.in);
        System.out.println("Input denominator: ");
        denom = indenom.nextInt();
    }

    public String toString() {
        return num + "/" + denom;
    }

    // addition
    public Fraction add(Fraction f2) {
        int num2 = f2.getNum();
        int denom2 = f2.getDenom();
        int num3 = (num * denom2) + (num2 * denom);
        int denom3 = denom * denom2;
        Fraction f3 = new Fraction(num3, denom3);
        f3.simplifyFraction();
        return f3;
    }
}

Now my second task is to make a class Calculator, which aggregates two instances of class Fraction as its attributes and create a complete set of arithmetic operations using instances of the class Fraction as operands. So, if I am correct, I basically have to use those methods from the class Fraction in my Calculator. I've attempted to do that but I do not get any output when I call for method add (from class Calculator) in main().

Here is an abstract of my Calculator class. (I've included only method of addition to give the general idea.)

package com.company;

public class Calculator {
    private Fraction f1 = new Fraction();
    private Fraction f2 = new Fraction();
    private Fraction f;

    public Calculator() {
        super();
    }

    public Calculator(Fraction f) {
        this.f = f;
    }

    public void input() {
        f1.inputFraction();
        f2.inputFraction();
    }

    public void view() {
        f1.toString();
        System.out.println("Fraction = " + f1.toString());
        f2.toString();
        System.out.println("Fraction = " + f2.toString());
    }

    public Calculator add() {
        Calculator f = new Calculator(f1.add(f2));
        return f;
    }
}

And part of my main():

Calculator m = new Calculator();
m.input();
m.view();
System.out.println("Sum = " + m.add());

I'm assuming there are multiple places where I have gone wrong, so I'd be grateful for some advice.


Solution

  • Your add method is the problem here. It is returning a Calculator object and println calls that object's toString method so the toString method for Calculator is being called. The add method should not return a new Calculator but instead a new Fraction that represents your result. Then the code will print the toString method in your Fraction class which is what you want to display.

    public class Calculator {
    .
    .
    .
       public Fraction add() {
              return f1.add(f2);
       }
    }