Returning location instead of value?

I created a Function class, with a method Add that can add two fractions together. The problem is that instead of getting a value when added together, I get the reference location(I think). I know this is probably something simple, but please let me know what I'm doing wrong!

Here is my Fraction class:

Code:

`public class Fraction`

{

private int n, d;

public Fraction(int n, int d) {

}

public Fraction add(Fraction other)

{

int n,d;

n = (this.n*other.d) + (this.d+other.n);

d = (this.d*other.d);

return new Fraction(n,d);

}

}

Here is my test method:

Code:

`public static void testAdd()`

{

Fraction f1, f2, f3;

f1 = new Fraction(1,4);

f2 = new Fraction(1,2);

f3 = new Fraction(3,4);

if (f1.add(f2) != f3)

{

System.out.println("Error in testAdd: Expected 3/4, found: " + f2.add(f1));

}

OUTPUT:

Quote:

Error in testAdd: Expected 3/4, found: Fraction@4e82701e

Re: Returning location instead of value?

The reason you're getting the reference location is that you haven't overridden toString(), which by default returns a string representing the location.

Also, consider that it's trying to calculate the denominator by multiplying 4 by 2, which isn't what you intend.

Edit: You need to define an equals() method instead of using == and != to compare equality. Again, that compares by reference.