# problem1.txt # Dan R. K. Ports # 6.170 PS2-1, 2004/02/12 # $Id: problem1.txt,v 1.1 2004/02/19 23:09:40 drkp Exp $ 1. The one-line comments clarify the math being used by the computations that generate the return values of these functions. 2. A NaN is represented by a denominator of zero. RatNum.add and RatNum.mul generate new RatNums that have denominator (this.denom * arg.denom), so if either number is NaN, then the denominator of the returned value will be zero, i.e. NaN. This is not true of a div call, so it is necessary to explicitly check whether the argument is NaN. 3. RatNum.parse is static because it generates a new object from only a string; it does not depend on the previous state of a RatNum object. This could also have been implemented as a constructor. 4. At the beginning of the constructor, the fields have not been initialized, so they should not be expected to satisfy the runtime invariant. 5. The two-argument constructor would be simpler: it wouldn't need to reduce the input. The checkRep method would be simplified to meet the weaker invariant. unparse() would be more complicated because it would need to reduce the number before outputting it. equals() and hashCode() would need to be made more complex: they would need to take into account that two rationals may be equal but in different unreduced forms. 6. The add/sub/mult/div calls need to return a new RatNum object. They are not allowed to modify the existing (immutable) object.