subtracting fractions with Different Denominators | Rationalising Fractions in Java
Learning how to subtract fractions in Java is an important step for junior secondary students who are beginning to combine mathematics with coding. In this tutorial, we'll walk through the process of subtracting fractions with different denominators, explain how to use the LCM (Lowest Common Multiple) method, and show you how to rationalise or canonise fractions before subtraction. By the end, you'll be able to write a simple Java program to subtract fractions step by step, giving you both a solid maths foundation and practical coding skills to implement libraries like the built-in Java fractions module.
Like with Adding Fractions in Java, fractional numbers are also rationalised before subtraction.
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 then be subtracted.
Steps for Subtraction of Fractions with Different Denominators - Java Algorithm
The following steps will guide us in writing our Java code for subtracting fractions.
Let's illustrate these steps with the example fractional expression
7/4 - 2/5
Step 1:
Using the Find LCM in Java
class from the Primary Category, find the LCM of the denominators.
⇒ LCM of 4 & 5 = 20
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.
⇒
((7 x 5) - (2 x 4))/20
= (35 - 8)/20
Step 3:
Go ahead and subtract the numerators.
⇒
27/20
Create a new Java class file;
call it SubtractFraction
Type out the adjoining Java code for subtracting fractions.
Note: You can comment out the AddFraction Java object code in the main class from the previous lesson or simply continue from where it stopped.
So! Java Fun Practice Exercise - Add 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 Subtracting Fractions - Class File
import java.util.List;
public class SubtractFraction extends AddFraction {
public SubtractFraction(List<Integer> num, List<Integer> denom){
super(num, denom);
}
public int[] doSubtract() {
canonizeFraction();
answer = new_numerators.get(0);
for (int i=1; i< new_numerators.size(); i++) {
answer -= new_numerators.get(i);
}
return new int[]{answer, lcm};
}
}
Java Code for Subtracting Fractions - Main Class
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("");
/*
* Subtracting fractions
*/
numerators = new ArrayList<>();
numerators.add(9);
numerators.add(3);
numerators.add(5);
numerators.add(7);
denominators = new ArrayList<>();
denominators.add(2);
denominators.add(4);
denominators.add(12);
denominators.add(18);
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 SubtractFraction class
SubtractFraction sub_fract = new SubtractFraction(numerators, denominators);
solution = sub_fract.doSubtract();
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");
}
}