usingMaths.com
From Theory to Practice - Math You Can Use.







<< Previous Next >>

How to Divide Fractions in Java | Step-by-Step Tutorial with Code Example



Understanding the Math Behind Fraction Division | Maths Explanation for Java Kids

Java makes it easy to perform arithmetic operations like dividing fractions. Whether you're simplifying fractions, multiplying them, or coding a math class project, Java provides clear syntax and powerful tools for beginners and students alike.

Dividing fractions in Java is a great way to combine coding with math skills. In this tutorial, junior secondary students will learn how to use Java to divide fractions step-by-step. We'll explore how to invert and multiply fractions, write a Java class for fraction operations, and understand the logic behind the algorithm.

Division is the inverse operation of multiplication; That is exactly what we'll do with for fractions.
Invert the fractions that come after a division sign, as well as change the division sign to multiplication, and then follow through with multiplication, as already explained in the Multiplying Fractions with Java tutorial.



Algorithm Steps to Divide Fractions in Java

Say we are to implement a Java algorithm to divide the given fractional expression
                  21/8 ÷ 7/2;

Inverting this will yield
                  21/8 ÷ 7/2;

Then we can go ahead and multiply.


Create a new Java class file; Call it DivideFraction.
Type out the adjoining Java code for dividing fractions.


Note: You can comment out the MultiplyFraction Java object code in the main class from the previous lesson or simply continue from where it stopped.


So! Java Fun Practice Exercise - Divide Fractions

As a fun practice exercise, feel free to try out your own fractions with different numerators and denominators, and see how the Java code divides those fractions.







Java Code for Dividing Fractions - Class File

package algebra;

import java.util.List;

public class DivideFraction extends MultiplyFraction {
    
    public DivideFraction(List<Integer> num, List<Integer> denom){
        super(num, denom);
    }
    
    public int[] doDivide(){
        int temp;
        // Invert every other fraction but the first
        for(int i=1; i<numerators.size(); i++){
            temp = numerators.get(i);
            numerators.set(i, denominators.get(i));
            denominators.set(i, temp);
        }
        return doMultiply();
    }
}

Java Code for Dividing Fractions - Main Class

package algebra;

import java.util.ArrayList;
import java.util.List;

public class Algebra {

    public static void main(String[] args) {

        int numerator;
        int denominator;
        List<Integer> numerators;
        List<Integer> denominators;
        int[] solution;

        System.out.println("Welcome to our demonstration sequels");
        System.out.println("Hope you enjoy (and follow) the lessons.");
        System.out.println("");

        /*
        * Dividing fractions
         */

        numerators = new ArrayList<>();
        numerators.add(16);
        numerators.add(9);
        numerators.add(640);
        numerators.add(7);

        denominators = new ArrayList<>();
        denominators.add(9);
        denominators.add(20);
        denominators.add(27);
        denominators.add(20);

        System.out.println("Solving:");
        // Print as fraction
        for (int n : numerators) {
            System.out.printf("%13d", n);
        }
        System.out.println();
        System.out.printf("%12s"" ");
        for (int i = 0; i < numerators.size() - 1; i++) {
            System.out.print("โ€•     รท     ");
        }
        System.out.printf("%2s""โ€•");
        System.out.println();
        for (int d : denominators) {
            System.out.printf("%13d", d);
        }
        System.out.println();

        // use the DivideFraction class
        DivideFraction div_fract = new DivideFraction(numerators, denominators);
        solution = div_fract.doDivide();
        numerator = solution[0];
        denominator = solution[1];

        System.out.println();

        System.out.printf("%25d%n", numerator);
        System.out.printf("%24s%n""Answer = โ€•");
        System.out.printf("%25d%n", denominator);

        System.out.println("\n\n");
    }

}



<< Previous Next >>