How to work with Fractions in java / How to do mathematical operations with Fractions in java

A fraction is a quantity which is not a whole number rather it is formed by using two numbers. A fraction has two parts : numerator and denominator separated by a “/”. Examples of fraction are : 15/2, 35/6
A fraction should always be represented in reduced(or simplest) form where there are no common multiples of numerator and denominator.

Example, 56/12 is not in its reduced from since its numerator and denominator still have common multiples. It reduced form will be 14/3(dividing both numerator and denominator by 4).
For reducing a fraction to its simplest form, Greatest Common Divisor(GCD) of its numerator and denominator is calculated. GCD is the largest number from the common multiples of numerator and denominator.
Both the numerator and denominator are divided by GCD to get the reduced form of fraction. In above case GCD of 56 and 12 is 4, dividing numerator and denominator by 4 gives 14/3 which is the simplest form of fraction.

Operations of Fractions
All mathematical operations are performed on fractions as well but have a different method. Common operations are:

Addition : For adding two fractions, numerator of first fraction is multiplied by the denominator of second and numerator of second fraction is multiplied by the denominator of second fraction and both are added to form the numerator of the resulting fraction. Denominator of resulting fraction is determined by multiplying the denominators of both the fractions. Finally the resulting fraction is reduced to its lowest form.
Example, 2/7 + 7/4 = (2 * 4) + (7 * 7) / (7 * 4) = 57/28

Subtraction : Similar to addition, numerator of first fraction is multiplied by the denominator of second and numerator of second fraction is multiplied by the denominator of second fraction and both are subtracted to form the numerator of the resulting fraction. Denominator of resulting fraction is determined by multiplying the denominators of both the fractions. Example, 11/3 – 2/5 = (11 * 5) – (2 * 3) / (3 * 5) = 49/15

Multiplication : This is straight-forward. Numerator of first fraction is multiplied by the numerator of second and denominator of second fraction is multiplied by the denominator of second fraction. Finally, the resulting fraction is reduced to its simplest form.  Example, 14/5 * 2/7 = (14 * 2) / (5 * 7) = 28/35 = 4/5(reduced to simplest form)

Division : Numerator of first fraction is multiplied with denominator of second and denominator of first fraction is multiplied with numerator of second and the resulting fraction is reduced to its simplest form. Example, 12/7 ÷ 4/3 = 36/28 = 9/7(reduced to simplest form)

Operations using java program

Java program to perform all mathematical operations listed above on fractions is given below.

public class Fraction {

    int numerator;
    int denominator;

    /**
    * Constructor
    * 
    * @param numr
    * @param denr
    */
    public Fraction(int numr, int denr) {
	numerator = numr;
	denominator = denr;
	reduce();
    }

    public int getNumerator() {
	return numerator;
    }

    public void setNumerator(int numerator) {
	this.numerator = numerator;
    }

    public int getDenominator() {
	return denominator;
    }

    public void setDenominator(int denominator) {
	this.denominator = denominator;
    }

    /**
    * Calculates gcd of two numbers
    * 
    * @param numerator
    * @param denominator
    * @return
    */
    public int calculateGCD(int numerator, int denominator) {
	if (numerator % denominator == 0) {
             return denominator;
        }
	return calculateGCD(denominator, numerator % denominator);
	}

    /**
    * Reduce the fraction to lowest form
    */
    void reduce() {
	int gcd = calculateGCD(numerator, denominator);
	numerator /= gcd;
	denominator /= gcd;
    }

    /**
    * Adds two fractions
    * 
    * @param fractionTwo
    * @return
    */
    public Fraction add(Fraction fractionTwo) {
	int numer = (numerator * fractionTwo.getDenominator()) + 
                            (fractionTwo.getNumerator() * denominator);
	int denr = denominator * fractionTwo.getDenominator();
	return new Fraction(numer, denr);
    }

    /**
    * Subtracts two fractions
    * 
    * @param fractionTwo
    * @return
    */
    public Fraction subtract(Fraction fractionTwo) {
        int newNumerator = (numerator * fractionTwo.denominator) - 
                                 (fractionTwo.numerator * denominator);
	int newDenominator = denominator * fractionTwo.denominator;
	Fraction result = new Fraction(newNumerator, newDenominator);
	return result;
    }

    /**
    * Multiples two functions
    * 
    * @param fractionTwo
    * @return
    */
    public Fraction multiply(Fraction fractionTwo) {
	int newNumerator = numerator * fractionTwo.numerator;
	int newDenominator = denominator * fractionTwo.denominator;
	Fraction result = new Fraction(newNumerator, newDenominator);
	return result;
    }

    /**
    * Divides two fractions
    * 
    * @param fractionTwo
    * @return
    */
    public Fraction divide(Fraction fractionTwo) {
	int newNumerator = numerator * fractionTwo.getDenominator();
	int newDenominator = denominator * fractionTwo.numerator;
	Fraction result = new Fraction(newNumerator, newDenominator);
	return result;
    }

    /**
    * Returns string representation of the fraction
    */
    @Override
    public String toString() {
	return this.numerator + "/" + this.denominator;
    }

    public static void main(String[] args) {
	Fraction f1 = new Fraction(25, 35);
	System.out.println(f1.toString());
	Fraction f2 = new Fraction(2, 7);
	Fraction f3 = f1.add(f2);
	System.out.println("Result of addition of " 
                      + f1 + " and " + f2 + " is : " + f3);
	f3 = f1.subtract(f2);
	System.out.println("Result of subtraction of " 
                      + f1 + " and " + f2 + " is : " + f3);
	f3 = f1.divide(f2);
	System.out.println("Result of division of " 
                      + f1 + " and " + f2 + " is : " + f3);
	f3 = f1.multiply(f2);
	System.out.println("Result of multiplication of " 
                      + f1 + " and " + f2 + " is : " + f3);
    }
}

Output

Result of addition of 7/4 and 2/7 is : 57/28
Result of subtraction of 7/4 and 2/7 is : 41/28
Result of division of 7/4 and 2/7 is : 49/8
Result of multiplication of 7/4 and 2/7 is : 1/2

Explanation
The above class depicts a Fraction with two fields representing numerator and denominator of fraction. The class has a constructor for initializing the fields of fraction.
It also contains a method reduce which converts a fraction to its simplified form. Thus, this method called on a fraction 25/35 converts it to 5/7. Internally it calculates the gcd of the fraction, which is the greatest multiple of both numerator and denominator of a fraction and then divides the numerator and denominator by this gcd.
Method to calculate gcd is a recursive method which keeps on calling itself till the remainder of division of numerator by denominator is 0. Thus for a fraction with numerator as 25 and denominator as 35, the greatest common divisor is 5 and the simplified fraction becomes (25 ÷ 5) / (35 ÷ 5) = 5/7.
The class contains methods to add, subtract, multiply and divide fractions and work on similar algorithm as explained above.
All these methods accepts a Fraction object as argument which acts as the second fraction. First fraction is the object on which these methods are called.
The class also contains a toString() method which returns the String version of the Fraction in the form of “numerator / denominator” of fraction.
Hope this post helped you to resolve a problem and saved your time. Hit the clap below to appreciate the article.

Leave a Reply