Home » Java » java – Finish this class method – Do you know how?-Exceptionshub

java – Finish this class method – Do you know how?-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

Could someone help me out finishing this task? I’d be very grateful!

Finish the class method ‘copy(Fraction source, Fraction target)’, so that after calling this method ‘source’ is unchanged and ‘target’ is made identical to ‘source’.

At this line: target = source; // Here I need to put the code

class Fraction {

    Integer nominator;       
    Integer denominator;      

}

class Main
{
	public static void copy(Fraction source, Fraction target)
	{
		target = source; // Here I need to put the code
	}

	public static Fraction createFraction(Integer nominator, Integer denominator)
	{
        Fraction fraction = new Fraction();
        fraction.nominator = nominator;
        fraction.denominator = denominator;
        return fraction;
	}

	public static void main(String[] arguments)
	{
		final Integer ONE = 1;
		final Integer TWO = 2;
		final Integer THREE = 3;
		final Fraction SOURCE = createFraction(ONE, TWO);
		final Fraction TARGET = createFraction(TWO, THREE);
		copy(SOURCE, TARGET);
		System.out.println("SOURCE: " + toString(SOURCE));
		System.out.println("TARGET: " + toString(TARGET));
	}

	public static String toString(Fraction fraction)
	{
                return fraction.nominator + "/" + fraction.denominator;

	}
}
How to&Answers:

Update target (not the reference), specifically copy the source nominator and denominator values to target.nominator and target.denominator respectively. Like,

public static void copy(Fraction source, Fraction target)
{
    // target = source; // Good try, but this just updates a local reference
    target.nominator = source.nominator; // <-- copy the `nominator`
    target.denominator = source.denominator; // <-- copy the `denominator`
}

Alternatively, but along the same lines, add a setFraction function to Fraction like,

public void setFraction(Fraction f) {
    this.nominator = f.nominator;
    this.denominator = f.denominator;
}

Also, you shouldn’t modify toString() like that. Add it to Fraction as well,

@Override
public String toString() {
    return nominator + "/" + denominator;
}

And I would also override Object.equals(Object) in Fraction.

@Override
public boolean equals(Object o) {
    if (this == o) {
        return true;
    }
    if (o instanceof Fraction) {
        Fraction that = (Fraction) o;
        return nominator.equals(that.nominator) && denominator.equals(that.denominator);
    }
    return false;
}

Then you can write

public static void copy(Fraction source, Fraction target)
{
    target.setFraction(source);
}

And if you followed along with toString then

System.out.println("SOURCE: " + toString(SOURCE));
System.out.println("TARGET: " + toString(TARGET));

Can be

System.out.println("SOURCE: " + SOURCE);
System.out.println("TARGET: " + TARGET);
System.out.println("TARGET.equals(SOURCE): " + TARGET.equals(SOURCE));