Question
MUST USE JAVA(Simple Programming) A rational number is a number that can be represented as the ratio of two integers. For example, 2/3 is a
MUST USE JAVA(Simple Programming)
A rational number is a number that can be represented as the ratio of two integers. For example, 2/3 is a rational number, and you can think of 7 as a rational number with an implicit 1 in the denominator or 7/1.
Define a class called Rational. A Rational object should have two integer instance variables that store the numerator and denominator.
Write a constructor that takes two arguments and that sets the numerator and denominator with these two parameters.
Write an instance method called printRational that displays a Rational in some reasonable format.
Write a main method that creates a new object with the type Rational, sets its instance variables to some values, and displays the object. At this stage, you have a minimal testable program. Test it and, if necessary, debug it.
Write an instance method called negate that reverses the sign of a rational number. This method should be a modifier, so it should be void. Add lines to the main to test the new method.
Write an instance method called invert that inverts the number by swapping the numerator and denominator. It should be a modifier. Add lines to the main to test the new method.
Write an instance method called toDouble that converts the rational number to a double (floating-point number) and returns the result. This method is a pure method; it does not modify the object. As always, test the new method.
Write an instance method named reduce that reduces a rational number to its lowest terms by finding the greatest common divisor (GCD) of the numerator and denominator and dividing through. This method should be a pure method; it should not modify the instance variables of the object on which it is invoked but it should return a new rational number or it should print a reduced format. Hint: Finding the GCD only takes a few lines of code. Search the web for Euclidean algorithm.
Write an instance method called add that takes a Rational number as an argument, adds it to this, and returns a new Rational object. There are several ways to add fractions. You can use anyone you want, but you should make sure that the result of the operation is reduced so that the numerator and denominator have no common divisor (other than 1).
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started