Question: In Java, write a class for rational numbers. Each object in the class should have two integer values that define the rational number: the numerator

In Java, write a class for rational numbers. Each object in the class should have two integer values that define the rational number: the numerator and the denominator.

Include a constructor with two arguments that can be used to set the numerator and denominator (note: zero is an invalid denominator so it should be dealt with appropriately, i.e., throw and exception).

Provide a no?argument constructor that has zero for the numerator and 1 for the denominator. Include a tester class to display the rational number to the screen/console/terminal. If the rational number is negative the display method should always ensure that the negative sign is displayed beside the numerator even if the user entered the negative on the denominator.

Write a normalize function that reduces the denominator to the smallest possible positive value (i.e., 4/?8 should be converted to ?1/2).

Finally write methods to add, subtract, multiply, divide and compare rational numbers. The solutions computed in these methods should always be normalized before being returned. For comparison you should have separate methods for testing equality of two rational numbers and for checking inequality.

Hints: Two rational numbers a/b and c/d are equal if a*d equals c*b. For positive rational numbers, a/b is less than c/d provided a*d is less than c*d.

Write a tester class (console or GUI) showing that your rational numbers class is fully functional. You may hard?code your data.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock 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 Databases Questions!