Revise the class Rational described in the previous exercise to use an assertion instead of an exception

Question:

Revise the class Rational described in the previous exercise to use an assertion instead of an exception to guarantee that the denominator is never zero.


Previous exercise

Create a class Rational that represents a rational number. It should have private attributes for

  • The numerator (an integer)
  • The denominator (an integer) and the following methods:
  • Rational(numerator, denominator)—a constructor for a rational number.
  • Accessor methods getNumerator and getDenominator and mutator methods setNumerator and setDenominator for the numerator and the denominator.

You should use an exception to guarantee that the denominator is never zero.

Fantastic news! We've Found the answer you've been seeking!

Step by Step Answer:

Question Posted: