Calling a class method within a method definition in the same class

Posted by user1786288 on Stack Overflow See other posts from Stack Overflow or by user1786288
Published on 2012-10-30T16:55:12Z Indexed on 2012/10/30 17:00 UTC
Read the original article Hit count: 152

Filed under:

So I'm writing a program for an intro Java class. I'm defining a method to add two fractions together within the class Fraction. Fraction has 2 variables, numerator and denominator. Each of these variables has a setter and a getter. Anyway, I have a problem with my addition method, which is shown below. I've just sort of tried to implement it in a way that made sense, but I'm sure I did something with calling methods/using objects that doesn't work.

public void add(Fraction fraction1, Fraction fraction2)
{
    Fraction fraction3 = new Fraction();

    int a = fraction1.getNumerator;
    int b = fraction1.getDenominator;
    int c = fraction2.getNumerator;
    int d = fraction2.getDenominator;

    fraction3.setNumerator((a * d) + (b * c));
    fraction3.setDenominator(b * d);
}

I don't know if the problem is in the method definition, if I can use the object type that way, or if there's something else wrong. any help would be appreciated, and if I need to provide any information, I'll do so ASAP.

© Stack Overflow or respective owner

Related posts about java