Question: Please show work in java Write a class named Rational for representing rational numbers. Every rational number has an integer numerator and an integer denominator;
Please show work in java






Write a class named Rational for representing rational numbers. Every rational number has an integer numerator and an integer denominator; for example, 7/8,7/8, and 5/1. (Note that 5/1 represents 5. ) Please read until the end of the instructions before beginning to write code. The class should provide the following constructors: - public Rational(int numerator) // denominator should be 1 - public Rational(int numerator, int denominator) - public Rational(String rationalString) // assume rationalString is of the form "numerator/denominator", e.g., "3/ 12" Note: In the constructor that accepts a String, consider using the split method of the String class to split the String parameter into two parts. Here's an example of using the split method: Suppose we have String date ="20220615" " then date.split("-") gives us the array {"2022","06","15"}. Another example: Suppose we have string time ="1:53:45"; then time.split(":") gives us the array {"1","53","45"} The class should provide the following methods: - public int getNumerator() - public int getDenominator( - public String toString0 // Usually returns a String of the form "numerator/denominator", e.g., "1/4". // But if the denominator is 1 , just returns the numerator. - public boolean isEqualTo(Rational other) - public boolean isPositive() - public boolean isNegative( - public Rational plus(Rational other) // returns this + other - public Rational minus(Rational other) // returns this - other - public Rational dividedBy(Rational other) // returns this / other - public Rational getNegation() // returns -this. // E.g, the negation of 1/2 is 1/2; the negation of 1/2 is 1/2 - public Rational getAbsoluteValue() // returns |this|. // E.g., the absolute value of 1/2 is 1/2; the absolute value of 1/2 is 1/2 - public static Rational sum(Rational rational1, Rational rational2) // returns rational1 + rational2 - public static Rational difference(Rational rational1, Rational rational2) // returns rational1 - rational2 - public static Rational product(Rational rational1, Rational rational2) // returns rational1 * rational2 - public static Rational quotient(Rational rational1, Rational rational2) // returns rational1 / rational2 - public static Rational negation(Rational rational) // returns -rational - public static Rational absoluteValue(Rational rational) // returns |rational| If you wish, you many add additional methods, but they must be private. Note: When a comment above says something like "returns this + other", do not take it literally, since you can't use + to add two rational numbers. Rather, "this + other" means the sum of this Rational and other, computed using a for mula. Understand the other comments (like "returns -this" and "returns |this|") similarly. Here are formulas for addition, subtraction, multiplication, and division: ba+dc=bdad+bcbadc=bdadbcbadc=bdac badc=bcad Additionally, the class should provide the following public constants of type Rational: NEGATIVE_ONE, ZERO, ONE (which should represent, respectively, the rational numbers 1,0, and 1 ). Note: This class should be immutable. This means that for every object of this class, the state (contents) of the object can never change. Methods like plus and sum (etc.) should not modify any objects, but rather return a new object. Note: A class invariant is a condition that must always be true for all objects of the class in order for the class code to work properly. The Rational class must abide by the following three class invariants: - A rational number is always in reduced form. - For example, 3/12 should be stored as 1/4. - To find the reduced form, we first find the GCD (greatest common divisor) of the original numerator and d enominator. In the code area below, I have written a method for finding the GCD of any two integers; you can copy my method. We then divide the numerator by the GCD, thereby obtaining the reduced numerat or. We also divide the denominator by the GCD, thereby obtaining the reduced denominator. This should be performed whenever a Rational is constructed, in order to ensure that the class invariant remains true. - The denominator is always positive. - For example, 1/-2 should be stored as 1/2. - Another example: -1/-2 should be stored as 1/2. - For this assignment, you can assume that the user will never provide 0 as the denominator. (Later in the course, we'll learn how to write code to reject an incorrect denominator.) - The only way to represent zero is 0/1. - For example, 0/67 should be stored as 0/1. he headers for the constructors and methods are provided below in Rational.iava. Code for testina the Rational The headers for the constructors and methods are provided below in Rational.java. Code for testing the Rational class is in TestRational.java
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
