Hello! Can smb explain why is there a new object(Rational x = new Rational(...)) created in the main class inside the for-loop??
public class MainClass
public static void main (String args )
Rational sum = new Rational (0,0);
Random gen = new Random ();
for (int i = 1; i <= 10; i++)
Rational x = new Rational (gen.nextInt (10), gen.nextInt (10)+1);
System.out.println (x.toString ());
sum = sum.add(x);
System.out.println ("sum = " + sum);
Hi. Not sure what you're really asking about, but it appears that new Rational objects are created so that they can be added up to 'sum'...
Do elaborate on what you really want to know.
Numbers are immuatable; probably Rational objects are immuatable too (as they should be); b.t.w. the initial value of 'sum' is suspicious, check the Rational class and ask the author of it for details.