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.

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.

Step by Step Solution

3.37 Rating (172 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

public class RationalWithAssert private int numerator private int denominator Creates a new instance of RationalWithAssert public RationalWithAssertin... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Java An Introduction to Problem Solving and Progra Questions!