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.
Math.absdoes nothing but test for "argument<0?" and returns -argument if yes, argument if not. Since you already establishedd<0where you callMath.abs(d), why not write-d?