3

I have a question about using an object as the variable in a constructor. It might be simple but I just really can't think of what to do and my java book isn't really helping. Say i wanted to do this

Fraction f3 = new Fraction(1, 2);
Fraction f5 = new Fraction(f3);

my constructor for the first object is:

public Fraction(int n, int d)

{
    if (d == 0)
    {
        numerator = 0;
        denominator = 1;
        System.err.println("Error: Invalid Denominator (" + d + ")");
    }
    else if (d < 0)
    {
        int nn = Math.abs(n) * (-1);
        numerator = nn;
        denominator = Math.abs(d);
    }
    else
    {
    numerator = n;
    denominator = d;
    }

}

my constructor for the second object is this:

public Fraction(Fraction f) 

{

}

I can't think of how to define the constructor to get it to set a new object as the object given. If anyone could give me a hand or maybe some advice to put me on the path to figuring it out I would greatly appreciate it.

1
  • Is it intentional that if for n=-1, d=-1, numerator is set to -1 and denominator to 1? Note that Math.abs does nothing but test for "argument<0?" and returns -argument if yes, argument if not. Since you already established d<0 where you call Math.abs(d), why not write -d? Commented Nov 27, 2011 at 0:02

4 Answers 4

6
public Fraction(Fraction f){
  this(f.numerator, f.denominator);
}
Sign up to request clarification or add additional context in comments.

Comments

4

I would use "constructor chaining" to ensure the changes made to one are reflected in the other.

// copy constructor
public Fraction(Fraction f) {
    this(f.numerator, f.denominator); // call the regular constructor with f's params
}

Comments

2
public Fraction(Fraction f){
  this.n = f.n;
  this.d = f.d;
}

5 Comments

Of course, this assumes 'numerator' and 'denominator' are defined as public members of Fraction
I wouldn't recommend this - if the logic changes, you're going to find a bug soon enough.
@TravelingTechGuy No, because you're inside the file. You have full access to members of the same class, even if they aren't members of this object.
@Traveling Tech Guy. No because the constructor is inside Fraction so it sees members of all visibilities.
Thank you. It worked perfectly. I had instance variables set up for numerator and denominator already.
1

Do you want something such that

Fraction a = .... ;
Fraction b = new Fraction(a);
System.out.println(a==b);

prints true? That is not possible in Java; new always produces a fresh object that is separate from all previously existing objects.

(This is in contrast to, say, ECMAScript, where a constructor is allowed to return an existing object and discard the fresh object the runtime created and passed as the constructor's this).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.