Write a class for rational numbers. Each object in the class should have two integer values that

Question:

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. For example, the fraction 5/6 would have a numerator of 5 and a denominator of 6. Include a constructor with two arguments that can be used to set the numerator and denominator (forbidding zero in the denominator).

Also provide a no-arguments constructor that has zero for the numerator and 1 for the denominator. Include a method that prints a rational number to System.out in a normal form (so that the denominator is as small as possible). The numerator or denominator (or both) may contain a minus sign, but when a rational number is printed, the denominator should never include a minus sign. So if the numerator is 1 and the denominator is -2, then the printing method should print -1/2.

Include a function to normalize the values stored so that, after normalization, the denominator is positive and as small as possible. For example, after normalization, 4/-8 would be represented as -1/2.

Write static methods for the usual arithmetic operators to provide addition, subtraction, multiplication, and division of two rational numbers. Write static boolean methods to provide the usual comparison operations to allow comparison of two rational numbers.

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

Step by Step Answer:

Related Book For  book-img-for-question
Question Posted: