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







<< Previous Next >>

Multiplying Fractions with Java: A Step-by-Step Tutorial



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

Learning how to multiply fractions in Java is a great way to combine math skills with coding. This tutorial is designed for junior secondary students who want to understand fraction multiplication using simple Java classes and constructors.

Multiplying fractions is pretty straightforward:
Cancel out all common factors between numerators and denominators, then multiply whatever is left numerator to numerator and denominator to denominator.

In this lesson, we'll walk through the step-by-step method of multiplying fractions using Java. You'll learn how to define a class, use constructors, and apply logic to find mutual factors and simplify results.



Step-by-Step Explanation of Algorithm to Multiply Fractions in Java

This Java algorithm for fractions shows how to multiply two fractions and reduce them to their lowest terms. It's a great math coding project for beginners.
Understanding how to multiply multiple fractions in Java helps students build both computational thinking and math fluency. It's a foundational skill for more advanced topics like algebra and data science.

If we have
                  4/9 x 21/8;

Step 1:

Find any common factor between any numerator and any denominator.

Step 2:

Cancel out any such common factor.

= X7
421
98
3 
Step 3:

Repeat Steps 1 & 2 recursively until there are no more common factors.

=1X 
47
38
 2
=7
6

Create a new Java class file; call it MultiplyFraction. Type out the adjoining Java code for multiplying fractions.



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


So! Java Fun Practice Exercise - Multiply 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 multiplies those fractions.







Java Code for Multiplying Fractions - Class File

package algebra;

import java.util.List;

public class MultiplyFraction {

    protected final List<Integer> numerators;
    protected final List<Integer> denominators;
    protected final int[] answer = {1, 1};
    protected int n_indexd_index;
    protected int trial_factor;
    protected boolean mutual_factor;

    public MultiplyFraction(List<Integer> num, List<Integer> denom) {
        numerators = num;
        denominators = denom;
        trial_factor = 0;
        n_index = 0;
        d_index = 0;

        for (int n : num) {
            if (n > trial_factor) {
                trial_factor = n;
            }
        }
        for (int d : denom) {
            if (d > trial_factor) {
                trial_factor = d;
            }
        }
    }

    public int[] doMultiply() {

        // We are counting down to test for mutual factors 
        while (trial_factor > 1) {
            // iterate through numerators and check for factors
            while (n_index < numerators.size()) {
                mutual_factor = false;
                if ((numerators.get(n_index) % trial_factor) == 0) { // do we have a factor
                    // iterate through denominators and check for factors
                    while (d_index < denominators.size()) {
                        if ((denominators.get(d_index) % trial_factor) == 0) { // is this factor mutual?
                            mutual_factor = true;
                            break// stop as soon as we find a mutual factor so preserve the corresponding index
                        }
                        d_index++;
                    }
                    break// stop as soon as we find a mutual factor so as  preserve the corresponding index
                }
                n_index++;
            }
            // where we have a mutual factor
            if (mutual_factor) {
                numerators.set(n_indexnumerators.get(n_index) / trial_factor);
                denominators.set(d_indexdenominators.get(d_index) / trial_factor);
                continue; // continue with next iteration repeating the current value of trial_factor
            }
            n_index = 0;
            d_index = 0;
            trial_factor--;
        }

        for (int i = 0; i < numerators.size(); i++) {
            answer[0] *= numerators.get(i);
            answer[1] *= denominators.get(i);
        }

        return answer;
    }
}

Java Code for Multiplying Fractions - Main Class

package algebra;

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

public class Algebra {

    public static void main(String[] args) {

        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("");

        /*
        * Multiplying fractions
         */

        numerators = new ArrayList<>();
        numerators.add(16);
        numerators.add(20);
        numerators.add(27);
        numerators.add(20);

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

        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("―     X     ");
        }
        System.out.printf("%1s""―");
        System.out.println();
        for (int d : denominators) {
            System.out.printf("%13d", d);
        }
        System.out.println();

        // use the MultiplyFraction class
        MultiplyFraction mul_fract = new MultiplyFraction(numerators, denominators);
        solution = mul_fract.doMultiply();

        System.out.println();

        System.out.printf("%25d%n", solution[0]);
        System.out.printf("%24s%n""Answer = ―");
        System.out.printf("%25d%n", solution[1]);

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

}



<< Previous Next >>