简体   繁体   中英

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

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.

Edit: The Error was a compiler error, couldn't find object. I forgot my parentheses after the function call, problem solved. Sorry for the inconvenience of forgetting to detail my error.

Thanks!

I guess your get calls are meant to be methods. Are you getting a compilation error by any chance.

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

Your fraction3 is a local variable, all references to it are lost as soon as the add method completes. You could try returning fraction3 instead of void in order to to hang on to it.

Your method is void , which means it's not returning an answer. A couple of slight modifications to your code results in a functioning solution:

public static Fraction 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.setDenominator(b * d);

    return fraction3;
}

Notice that I changed the method to static since it doesn't modify this , changed the return-type from void to Fraction , and added a return statement. Not that this is a non-destructive add because it returns a new fraction. If you want to use this style then you should probably just get rid of the setter methods since they won't be used, and declare the fields as final , setting the numerator/denominator via the constructor instead.

Your other option would be to define a destructive addition operation, meaning that the receiver is updated rather than returning a new fraction object like you do above.

public void add(Fraction that)
{
    int a = this.getNumerator();
    int b = this.getDenominator();
    int c = that.getNumerator();
    int d = that.getDenominator();

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

This is just my opinion, but if you are going to have add take two arguments, make the method static. If you are going to have it take one argument, it would not be static, as the second argument is implicitly this .

The reasoning for this is if the non-static version takes 2 arguments, then you are using instance a to operate on instance b and instance c? IMHO that doesn't make much sense.

parenthesis are missing in your methods ...

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

Just add the return type and return fraction3 from the method as below:

public Fraction 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);
   return fraction3 ;
}

now you can use this method as :

 Fraction resultFraction = add(fraction1, fraction2);

If you want to add fraction2 in fraction1 itself ie

 fraction1.add(fraction2);

Then update your method as:

public void add(Fraction fraction2)
{
   int a = this.numerator;
   int b = this.denominator;
   int c = fraction2.getNumerator();
   int d = fraction2.getDenominator();

   this.numerator = (a * d) + (b * c);
   this.denominator = (b * d);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM