Why Sorting Fractions Matters in Math and Java
Sorting fractions in Java is a valuable skill for junior secondary students who are learning to connect mathematics with coding. Instead of simply converting fractions into decimals, this tutorial shows you how to compare and arrange fractions step by step using the LCM method. By the end, you'll know how to write a Java program to sort fractions in ascending and descending order, making it easier to solve math problems and strengthen your programming logic.
In this guide, we'll start with the basics of comparing fractions, then move on to a clear Java code example for sorting fractions. You'll see how to rationalise fractions, apply the least common multiple (LCM), and use Java's built-in functions to arrange them correctly. This approach not only improves your understanding of fractions but also builds confidence in writing simple algorithms.
Whether you're a student practicing for class, a teacher preparing classroom exercises, or a beginner looking for a Java fractions tutorial, this lesson is designed to be easy to follow. Let's dive in and learn how to sort fractions step by step with Java.
Rationalise (Canonise) the Fractions before Sorting | Maths Explanation for Java Kids
Just like was shown with Adding Fractions in Java and
Subtractinging Fractions in Java,
fractional numbers also have to be rationalised before sorting.
This means they are put in a form where their denominators become the same. This identical denominator is the LCM of the
previous denominators of all the separate fractions.
After this is done, the new numerators can easily be sorted in a preferred order.
Step-by-Step Guide to Sorting Fractions in Java
The following steps will guide us in writing our Java code for sorting fractions.
Let's illustrate the steps to follow with the example case 5/9,
3/7, 1/2
Step 1:
Using the Find LCM in Java
class from the Primary Category, find the LCM of the denominators.
⇒ LCM of 9, 7 & 2 = 126
Step 2:
In a turn by turn fashion, divide the found LCM from Step 1
by each denominator, multiplying the quotient by the corresponding numerator.
⇒
((5 x 14), (3 x 18), (1 x 63))/126
= (70, 54, 63)/126
Step 3:
Go ahead and sort the numerators in our order of choice.
⇒ In ascending order:
54/126, 63/126,
70/126
=
3/7, 1/2,
5/9
Create a new Java class file;
call it SortFraction.
Type out the adjoining Java code for sorting fractions in ascending and descending orders.
Note: You can comment out the SubtractFraction Java object
code in the main class from the previous lesson or simply continue from where it stopped.
So! Java Fun Practice Exercise - Sort 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 sorts those fractions.
Java Code for Sorting Fractions - Class File
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class SortFraction extends AddFraction {
public List<Integer> final_numerators;
public List<Integer> final_denominators;
public SortFraction(List<Integer> num, List<Integer> denom) {
super(num, denom);
final_numerators = new ArrayList<>();
final_denominators = new ArrayList<>();
}
public void sortAscending() {
List<Integer> copy_numerators = new ArrayList<>();
int index;
canonizeFraction();
for (Integer nn : new_numerators) {
copy_numerators.add(nn);
}
Collections.sort(copy_numerators);
// map sorted (transformed) fractions to the original ones
for (Integer sorted : copy_numerators) {
// get index using array value
index = new_numerators.indexOf(sorted);
final_numerators.add(super.numerators.get(index));
final_denominators.add(super.denominators.get(index));
}
}
public void sortDescending() {
List<Integer> copy_numerators = new ArrayList<>();
int index;
canonizeFraction();
for (Integer nn : new_numerators) {
copy_numerators.add(nn);
}
Collections.sort(copy_numerators, Collections.reverseOrder());
// map sorted (transformed) fractions to the original ones
for (int sorted : copy_numerators) {
// get index using array value
index = new_numerators.indexOf(sorted);
final_numerators.add(super.numerators.get(index));
final_denominators.add(super.denominators.get(index));
}
}
}
Java Code for Sorting Fractions - Main Class
import java.util.ArrayList;
import java.util.List;
public class Algebra {
public static void main(String[] args) {
List<Integer> numerators;
List<Integer> denominators;
System.out.println("Welcome to our demonstration sequels");
System.out.println("Hope you enjoy (and follow) the lessons.");
System.out.println("");
/*
* Sorting fractions
*/
numerators = new ArrayList<>();
numerators.add(1);
numerators.add(3);
numerators.add(5);
numerators.add(9);
denominators = new ArrayList<>();
denominators.add(2);
denominators.add(4);
denominators.add(2);
denominators.add(10);
System.out.println("Sorting in ascending order the fractions:");
// Print as fraction
System.out.printf("%35s", " ");
for (int n : numerators) {
System.out.printf("%9d", n);
}
System.out.println();
System.out.printf("%43s", " ");
for (int i = 0; i < numerators.size() - 1; i++) {
System.out.print("― , ");
}
System.out.printf("%2s", "―");
System.out.println();
System.out.printf("%35s", " ");
for (int d : denominators) {
System.out.printf("%9d", d);
}
System.out.println();
// use the SortFraction class
SortFraction sort_fract = new SortFraction(numerators, denominators);
sort_fract.sortAscending();
numerators = sort_fract.final_numerators;
denominators = sort_fract.final_denominators;
System.out.println();
// Print as fraction
System.out.printf("%35s", " ");
for (int n : numerators) {
System.out.printf("%9d", n);
}
System.out.println();
System.out.printf("%43s", "Answer = ");
for (int i = 0; i < numerators.size() - 1; i++) {
System.out.print("― , ");
}
System.out.printf("%2s", "―");
System.out.println();
System.out.printf("%35s", " ");
for (int d : denominators) {
System.out.printf("%9d", d);
}
System.out.println();
System.out.println("\n\n");
}
}