import corejava.Console; import java.lang.Integer; import java.lang.String; //////////////////////////////////////////////////////////////////////////////////////////////////////// //Class Rational-provides functions for performing arithmetic with fractions-rational // //numbers.It provides functions for addition,subtraction,multiplication and comparision of // //two rational numbers.This class provides the following functions. // //Public class Rational{ // // public Rational(int numeratorval,int denominatorval) //Initiallizing Cosntructor // // 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/ // } // //////////////////////////////////////////////////////////////////////////////////////////////////////// public class Rational{ //instance variables private int numerator; //numerator of the Rational number private int denominator; //denominator of the Rational number ///////////////////////////////////////////////////////////////////////////////////////////////// //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 // //Inputs :numeratorval-an integer which specifies the numerator. // // denominatorval-an integer which specifies the denominator // //Outputs :None // //Local Variables:None // //Author :Lakshmi Anumolu // ///////////////////////////////////////////////////////////////////////////////////////////////// public Rational(int numeratorval,int denominatorval){ //Call the gcd function to calculate the greatest common divisor of //the nummerator and the denominator int greatestcommondivisor=gcf(numeratorval,denominatorval); //Reduce the numerator and denominator to the lowest terms. numerator=numeratorval/greatestcommondivisor; denominator=denominatorval/greatestcommondivisor; } ///////////////////////////////////////////////////////////////////////////////////////////////// //Title :Rational() // //Description :Default constructor that sets the private variables to default values // // sets the numerator to zero,and denominator to 1,So number is zero. // //Inputs :None // //Outputs :None // //Local Variables:None // //Author :Lakshmi Anumolu // ///////////////////////////////////////////////////////////////////////////////////////////////// public Rational(){ numerator=0; //initiallise the numerator to zero denominator=1; //initiallise the denominator to one } ///////////////////////////////////////////////////////////////////////////////////////////////// //Title : int gcf(int value1,int value2) // //Description : Private function to find the greatest common divisor of two positive numbers// //Inputs : value1-integer specifies the numerator-value1 > 0 // // value2 -integer specifies the denominator-value2 > 0 // //Outputs : value1-integer-Greatest Common divisor of the two positive numbers // //Local Variables: temp-an integer storing the temporary value while swapping 2 numbers // //Author : Nancy McCracken // ///////////////////////////////////////////////////////////////////////////////////////////////// private int gcf (int value1, int value2){ int temp; //continue till value1 becomes zero while (value1> 0) { if (value1< value2) { //swap both numbers if the first is less then the second temp=value1; //temp holds the temporary value during the swapping process value1=value2; value2=temp; } value1 = value1 - value2; //subtract the second number from the first } return value2; //value2 is the gcd of the 2 numbers } ///////////////////////////////////////////////////////////////////////////////////////////////// //Title :void getnumdenom(int numeratorval,int denominatorval){ // //Description :Calls the gcf function to calculate the greatest common divisor of // // the inputs-nummeratorval and the denominatorval. // // This function sets the numerator of the object to an integer value // // (i.e numeratorval divided by the greatest common divisor) and denominator of // // the object to an integer value(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 // //Inputs :numeratorval-an integer which specifies the numerator. // // denominatorval-an integer which specifies the denominator // //Outputs :None // //Local Variables:None // //Author :Lakshmi Anumolu // ///////////////////////////////////////////////////////////////////////////////////////////////// public void getnumdenom(int numeratorval,int denominatorval){ //Call the gcd function to calculate the greatest common divisor of //the input numerator and the denominator int greatestcommondivisor=gcf(numeratorval,denominatorval); //Reduce the numerator and denominator to the lowest terms. numerator=numeratorval/greatestcommondivisor; denominator=denominatorval/greatestcommondivisor; } //////////////////////////////////////////////////////////////////////////////////////////////// //Title :Rational Addition(Rational r1,Rational r2) // //Description :Public function-Addition which takes in two Rational numbers and provides // // the sum of the two numbers // // Sum of a/b + c/d=(a*d+c*b)/(b*d) // // Call the gcf function to reduce the sum to its lowest terms // //Inputs :r1,r2-Instances of Rational class // //Outputs :sum-Instance of Rational class.This contains the sum of the input numbers // //Local Variables:None // //Author :Lakshmi Anumolu // //////////////////////////////////////////////////////////////////////////////////////////////// public Rational Addition(Rational r1,Rational r2){ //Create an instance of Rational-sum-Contains the sum of the input rational numbers Rational sum=new Rational(); //sum.numerator contains the numerator-(a*b+b*d)- of the resulting addition. sum.numerator=r1.numerator*r2.denominator + r2.numerator*r1.denominator; //sum.denominator contains the denominator-(b*d)- of the resulting addition sum.denominator=r1.denominator*r2.denominator; //Reduce the fraction sum.numerator/sum.denominator to its lowest terms by calling the gcf function //Divide the sum.numerator and sum.denominator by the greatest common divisor to get the lowest fraction int greatestcommondivisor=gcf(sum.numerator,sum.denominator); sum.numerator=sum.numerator/greatestcommondivisor; sum.denominator=sum.denominator/greatestcommondivisor; //return the result-sum- of the addition of 2 rational numbers return sum; } /////////////////////////////////////////////////////////////////////////////////////////////// //Title :Rational Subtraction(Rational r1,Rational r2) // //Description :Public function-Subtraction which takes in two Rational numbers and // // provides the difference between the two numbers // // Difference between a/b -c/d=(a*d-c*b)/(b*d) // // Call the gcf function to reduce the difference fraction to its lowest terms// //Inputs :r1,r2-Instances of Rational class // //Outputs :Difference-Instance of Rational class which holds the difference between // // the two input rational numbers // //Local Variables:None // //Author :Lakshmi Anumolu // /////////////////////////////////////////////////////////////////////////////////////////////// public Rational Subtraction(Rational r1,Rational r2){ //Create an instance of Rational-Difference-Contains the difference between the two input //rational numbers Rational Difference=new Rational(); //Difference.numerator contains the numerator-(a*b-b*d)- of the resulting subtraction. Difference.numerator=r1.numerator*r2.denominator - r2.numerator*r1.denominator; //Difference.denominator contains the denominator-(b*d)- of the resulting subtraction Difference.denominator=r1.denominator*r2.denominator; //Reduce the fraction Difference.numerator/Difference.denominator to its lowest terms by //calling the gcf function.Divide the Difference.numerator and Difference.denominator by the // greatest common divisor to get the lowest fraction int greatestcommondivisor=gcf(Difference.numerator,Difference.denominator); Difference.numerator=Difference.numerator/greatestcommondivisor; Difference.denominator=Difference.denominator/greatestcommondivisor; //return the result-Difference-Contains the difference between the two input //rational numbers return Difference; } /////////////////////////////////////////////////////////////////////////////////////////////// //Title :void Multiplication(Rational r1,Rational r2) // //Description :Public function-Multiplication takes in two Rational numbers and // // provides the result of mulitiplying the two numbers // // Multiplication of a/b *c/d=(a*c)/(b*d) // //Inputs :r1,r2-Instances of Rational classes // //Outputs :Mult-Instance of Rational class-which holds the result of multipication of // // the two input rational numbers // //Local Variables:None // //Author :Lakshmi Anumolu // /////////////////////////////////////////////////////////////////////////////////////////////// public Rational Multiplication(Rational r1,Rational r2){ //Declare an Instance of Rational class-Mult-which holds the result of multipication of //the two input rational numbers Rational Mult=new Rational(); //Mult.multnumerator contains the numerator-(a*c)- of the resulting multiplication. Mult.numerator=r1.numerator*r2.numerator; //Mult.multdenominator contains the denominator-(b*d)- of the resulting multiplication Mult.denominator=r1.denominator*r2.denominator; //Reduce the fraction Mult.numerator/Mult.denominator to its lowest terms by //calling the gcf function.Divide the Mult.numerator and Mult.denominator by the // greatest common divisor to get the lowest fraction int greatestcommondivisor=gcf(Mult.numerator,Mult.denominator); Mult.numerator=Mult.numerator/greatestcommondivisor; Mult.denominator=Mult.denominator/greatestcommondivisor; //return the result-Mult- of the multiplication of 2 rational numbers return Mult; } /////////////////////////////////////////////////////////////////////////////////////////////// //Title :void Comparision(Rational r1,Rational r2) // //Description :Public function-Comparision takes in two Rational numbers and // // provides the result of comparing the two numbers // // Comparision of a/b,c/d ......a/b < c/d if (a*d < (c*b) // //Inputs :r1,r2-Instances of rational class // //Outputs :None // //Local Variables:None // //Author :Lakshmi Anumolu // /////////////////////////////////////////////////////////////////////////////////////////////// public Rational Comparision(Rational r1,Rational r2){ //Check whether the rational number r1 is greater or r2 is greater if ((r1.numerator*r2.denominator)<(r2.numerator*r1.denominator)){ //If the rational number r2 is greater, then return r2 Rational Max=new Rational(r2.numerator,r2.denominator); return Max; } else { //If the rational number r1 is greater, then return r1 Rational Max=new Rational(r1.numerator,r1.denominator); return Max; } } /////////////////////////////////////////////////////////////////////////////////////////////////// //Title :void toString(Rational r1) // //Description :Public function-This forms a string with the numerator and denominator of the // // input rational number // // The string formed is ....numerator + "/" + denominator // //Inputs :r1-instance of Rational class // //Outputs :None // //Local Variables:None // //Author :Lakshmi Anumolu // /////////////////////////////////////////////////////////////////////////////////////////////////// public void toString(Rational r1){ //return a string containing the numerator and denominator with a "/" symbol in-between the //integers to represent the fraction System.out.println(r1.numerator+"/"+r1.denominator); } }