Date Assigned: November 19, 2007
Date Due: November 26, 2007
Write a Java program that will instantiate rational numbers. Develop instance methods that will perform addition, subtraction, multiplication and division on rational numbers.
Develop corresponding class methods (static) that will perform the above mathematical operations.
Develop toString() method to print rational numbers. The rational numbers must be in their reduced form (by dividing the numerator and denominator by the GCD)
Sample input:
3/7 plus 1/14
Output:
1/2
This algorithm will include the computation of the gcd. Once the gcd is determined the output can be reduced. The following expressions shows the computation for each operation:
n1/d1 + n2/d2 = (n1*d2 + n2*d1)/(d1*d2)
n1/d1 - n2/d2 = (n1*d2 - n2*d1)/(d1*d2)
(n1/d1)*(n2/d2) = (n1*n2)/(d1*d2)
(n1/d1)/(n2/d2) = (n1*d2)/(d1*n2)
Here is the sample code that partially outlines the Rational class.
Submit output that demonstrates the use and correctness of your algorithm. Test your program with the following input.
14/3 + 5/2
33/4 - 6/8
4/9 * 18/24
2/16 / 8/64