public Rational plus ( final Rational pOther ) { // special cases if ( equals ( ZERO ) ) { return pOther ; } if ( pOther . equals ( ZERO ) ) { return this ; } // Find gcd of numerators and denominators long f = gcd ( numerator , pOther . numerator ) ; long g = gcd ( denominator , pOther . denominator ) ; // add cross-product terms for numerator // multiply back in return new Rational ( ( ( numerator / f ) * ( pOther . denominator / g ) + ( pOther . numerator / f ) * ( denominator / g ) ) * f , lcm ( denominator , pOther . denominator ) ) ; }
return a + b staving off overflow