Why Rationalise or Canonise Fractions before Addition | Maths Explanation for Java Kids
In this Java tutorial for junior secondary students, we explore how to add fractions. Before performing the addition,
we rationalise or canonise the fractions to ensure accuracy. This method uses Finding LCM in Java
class to align denominators, making it ideal for math programming beginners.
This Java tutorial teaches young students how to add fractions with different denominators.
Before fractions are added, they are rationalised; i.e., 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 added together.
Step-by-Step Guide for Addition of Fractions - Java Algorithm
The following steps will guide us in writing our Java code for adding fractions.
Let's illustrate the steps to follow with the example fractional expression
2/5 + 7/4
Step 1:
Using the Find LCM in Java
class from the Primary Category, find the LCM of the denominators.
⇒ LCM of 5 & 4 = 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.
⇒
((2 x 4) + (7 x 5))/20
= (8 + 35)/20
Step 3:
Go ahead and add the numerators.
⇒
43/20
Create a new Java class file;
Call them AddFraction.;
Type out the adjoining Java code for adding fractions.
Note: The code module for Learn how to find LCM in Java
is from the Primary Category.
Create a new Java class file called LCM in your current project and copy the L.C.M. code into it.
You can comment out the DivideFraction 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 adds these fractions.
Java Code for Adding Fractions - Class File
import java.util.ArrayList;
import java.util.List;
public class AddFraction {
protected final List<Integer> numerators;
protected final List<Integer> denominators;
protected List<Integer> new_numerators;
protected int lcm;
protected int answer = 0;
public AddFraction(List<Integer> num, List<Integer> denom) {
numerators = num;
denominators = denom;
new_numerators = new ArrayList<>();
}
protected void canonizeFraction() {
LCM l_c_m = new LCM(denominators);
lcm = l_c_m.getLCM();
for (int i = 0; i < denominators.size(); i++) {
new_numerators.add(lcm / denominators.get(i) * numerators.get(i));
}
}
public int[] doAdd() {
canonizeFraction();
for (int n : new_numerators) {
answer += n;
}
return new int[]{answer, lcm};
}
}
Java Code for Adding 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("");
/*
* Adding fractions
*/
numerators = new ArrayList<>();
numerators.add(1);
numerators.add(1);
numerators.add(1);
numerators.add(1);
denominators = new ArrayList<>();
denominators.add(4);
denominators.add(4);
denominators.add(4);
denominators.add(4);
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 AddFraction class
AddFraction add_fract = new AddFraction(numerators, denominators);
solution = add_fract.doAdd();
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");
}
}