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







<< Previous Next >>

How to Convert Improper Fraction to Mixed Fraction in Java | Fun Maths Exercise



Understanding Fractional Numbers | Maths Explanation for Java Kids

Welcome to this junior secondary Java math project! In this tutorial, you'll learn how to convert improper fractions to mixed fractions using Java. This beginner-friendly guide is perfect for students exploring math with Java, and includes a simple Java script to help you understand fraction conversion step by step.

Whether you're just starting out with Java or looking for a fun math coding activity, this project will show you how to build a mixed fraction calculator using basic programming concepts.
Coding fractions in Java is a fun way to combine math and programming. This project helps junior secondary students build confidence in both areas, and also teaches how to apply math logic in Java code.

As before, fractional numbers can either be Proper (e.g. 2/5), Improper (e.g. 5/2), or Mixed (e.g. 21/2),


Step-by-Step Explanation of Improper Fraction to Mixed Fraction Conversion in Java

Say we have the fraction 10/3 - which is an improper fraction - we can write a Java algorithm that converts this improper fraction to a mixed fraction by following a number of steps:

Step 1:

Find the largest number smaller than the numerator (10), that can divide the denominator - 3 - without remainder.
         ⇒ 9

Step 2:

Subtract the found number (9) - from Step 1 - from the numerator (10)
         ⇒ 10 - 9 = 1;
This yields the whole number.

Step 3:

Divide the found number (9) - from Step 1 - by the denominator (3)
         ⇒ 9/3 = 3;
This yields the new numerator.

Step 4:

Reuse the denominator (3) to get our mixed fraction
         ⇒ 31/3

Create a new java class file; call it ImproperToMixed
Type out the adjoining Java code for converting improper to mixed fractions.


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


So! Java Fun Practice Exercise - Convert Improper Fraction to Mixed Fraction

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







Java Code for Converting Improper Fraction To Mixed Fraction - Class File

package algebra;

public class ImproperToMixed {

    private final int numerator;
    private final int denominator;
    private int whole_number;
    private int new_numerator;

    public ImproperToMixed(int num, int denom) {
        numerator = num;
        denominator = denom;
    }

    public int[] doConvert() {
        int dividend; // Highest multiple of denominator less than numerator
        for (dividend = numerator - 1; dividend > 1; dividend--) {
            if ((dividend % denominator) == 0) {
                new_numerator = numerator - dividend;
                whole_number = dividend / denominator;
                break;
            }
        }
        return new int[]{whole_numbernew_numerator};
    }
}

Java Code for Converting Improper Fraction To Mixed Fraction - Algebra Main Class

package algebra;

import java.util.List;

public class Algebra {

    public static void main(String[] args) {

        int numerator;
        int denominator;
        int whole_number;
        int[] solution;

        System.out.println("Welcome to our demonstration sequels");
        System.out.println("Hope you enjoy (and follow) the lessons.");
        System.out.println("");
        
        /*
        * Convert fractions from Improper to Mixed
         */

        numerator = 10;
        denominator = 3;

        System.out.println("Converting from Improper to Mixed the fraction:");
        // Print as fraction
        System.out.printf("%55d%n", numerator);
        System.out.printf("%54s%n""―");
        System.out.printf("%55d%n", denominator);

        // use the ImproperToMixed class
        ImproperToMixed imp_mix = new ImproperToMixed(numerator, denominator);
        solution = imp_mix.doConvert();
        whole_number = solution[0];
        numerator = solution[1];

        System.out.println();

        System.out.printf("%52d%n", numerator);
        System.out.printf("%50s%d%s%n""Answer =  ", whole_number, "―");
        System.out.printf("%52d%n", denominator);

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

}




<< Previous Next >>