Greatest Common divisor function
This class should provide a function to convert all fractions to the
lowest term.The rational elements enterd by the user are stored
in the lowest terms and the result of all arithmetic operations on
these fractions are converted to their lowest terms.
Other Functionality
This class should include functions to perform arithmetic on rational
numbers.The result of this and all the remaining methods should be in lowest
terms.
The following functionality should be provided:
a/b + c/d = (a*d + c*b) / b*d
a/b - c/d = (a*d - c*b) / b*d
a/b * c/d = a*c / b*d
a/b < c/d iff (a*d < c*b)
The test program creates 2 instances-rationale and rationstr- of the
class Rational.The default constructor Rational() is called to
create both objects. The test program queries the user to enter the
number of Rational elements that will be entered .The Test program
then queries the user to enter the numerator and denominator of each
Rational element. The test program accepts the input Rational
elements entered by the user.The input Rational elements are read in
by the Console.readInt().
The Console.readInt() function verifies if each argument entered
by the user is an integer.It sends an error message to standard output
if an integer is not entered.The Console.readInt() function accepts
all valid arguments.The test program then stores all the arguments in
an array- RationalArray.
The RationalArray contains the numerators and denominators of
all Rational Elements.The test program calls the toString member
function
of the Rational class to display the numerator and denominator of each
number as a string.So a number with numerator 3 and
denominator 4 is displayed as the string 3/4.Each rational element
is displayed in this manner.
The program then calls the getnumdenom function which takes the
numerator and denominator of each rational element and converts the
fraction to the lowest term.The getnumdenom calls the gcf
to find the greatest common divisor of the numerator and the denominator.The
numerator and denominator is then divided by this greatest common divisor.
The SumofRationalElements is an instance of the Rational class.It
represents the sum of all rational elements entered by the user.This is
initiallised to zero.The test program parses through the array of rational
elements and sends each of the elements to the Addition function
of the Rational class which calculates the sum of all Rational elements
in the array.
The MaxofRationalElements is an instance of the Rational class.It
represents the maximum of all rational elements entered by the user.
This is initiallised to zero.The test program parses through the array
of rational elements and sends each of the elements to the Comparision
member function of the Rational class which compares each element with
the latter to find the maximum of all Rational elements in the array.
The test program calls the toString function which takes the
SumofRationalElements instance as an argument and displays this
numerator
and denominator of this fraction as a string. It displays the sum of
all rational elements entered by the user
The test program then calls the toString function which takes
the MaximumofRationalElements instance as an argument and displays
this
numerator and denominator of this fraction as a string. It displays
the maximum of all rational elements entered by the user
Description of class Rational and functions
Public class Rational{ public Rational(int numeratorval,int denominatorval) //Initiallizing Constructor public Rational() //Default Constructor public void getnumdenom(int numeratorval,int denominatorval) //initiallizes the numerator and denominator //of a rational object public Rational Addition(Rational r1,Rational r2) //Addition of 2 Rational numbers public Rational Subtraction(Rational r1,Rational r2) //Subtraction of 2 Rational numbers public Rational Multiplication(Rational r1,Rational r2) //Multiplication of 2 Rational numbers public Rational Comparision(Rational r1,Rational r2) //Comparision of 2 Rational numbers public void toString(Rational r1) //forms a string with the Numerator //and denominator of r1,with "/" in b/w/ private int gcf (int value1, int value2) //obtains the gcd of 2 Rational numbers }
Functions
Title :Rational(int numeratorval,int denominatorval) Description :Calls the gcf function to calculate the greatest common divisor of the inputs-nummeratorval and the denominatorval.This function is an Initiallizing constructor function that initializes an object with an initial numerator(i.e numeratorval divided by the greatest common divisor) and denominator(i.e. denominatorval divided by the greatest common divisor). This division by the greatest common divisor is done to reduce the fraction to lowest terms