Results 1 to 2 of 2
  1. #1
    cpguy is offline Member
    Join Date
    Sep 2011
    Rep Power

    Default 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:

    Java 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:
    Java 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));
    Error in testAdd: Expected 3/4, found: Fraction@4e82701e

  2. #2
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Rep Power

    Default 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.

Similar Threads

  1. help returning a min value
    By Meta in forum New To Java
    Replies: 7
    Last Post: 04-11-2011, 12:28 AM
  2. Help with returning
    By Da1dmoney in forum Eclipse
    Replies: 3
    Last Post: 12-15-2010, 02:38 AM
  3. Fixed location
    By Dipke in forum New To Java
    Replies: 4
    Last Post: 08-13-2010, 10:25 AM
  4. need to copy file from one location to another location
    By naveen.dpt2007 in forum New To Java
    Replies: 4
    Last Post: 02-28-2010, 03:53 PM
  5. location provider returns no location
    By sandeeprao.techno in forum CLDC and MIDP
    Replies: 0
    Last Post: 09-24-2009, 09:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts