Creating the divide method

To go along with multiplication, we will also create a method for fraction division. Like our multiply method, we will create a divide method which takes in another Fraction and returns the quotient of itself and the incoming Fraction.

To divide two fractions, we multiply the dividend (the calling Fraction or this) by the reciprocal of the divisor (the argument Fraction or parameter). Since Fraction already has reciprocal and multiply methods, the divide method will be simple to code.

/**
 * Divides this Fraction by a given Fraction
 * @param otherFraction the Fraction to divide by
 * @return A Fraction that is the result of multiplying this Fraction by the given Fraction
 */
public Fraction divide( Fraction otherFraction )
{
	return multiply( otherFraction.reciprocal() );
}

Testing

  1. Un-comment the code in main that prints the quotient of the two fractions.