Method of Multiplying Fractions
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.
Algorithm Steps to Multiply Fractions in Java:
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.
Step 3:
Repeat Steps 1 & 2 recursively until there are no
more common factors.
Create a new 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!
Feel free to try out your own set of numerators and denominators for fractional multiplication.
Java code for MultiplyFraction 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_index, d_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() {
while (trial_factor > 1) {
while (n_index < numerators.size()) {
mutual_factor = false;
if ((numerators.get(n_index) % trial_factor) == 0) {
while (d_index < denominators.size()) {
if ((denominators.get(d_index) % trial_factor) == 0) {
mutual_factor = true;
break;
}
d_index++;
}
break;
}
n_index++;
}
if (mutual_factor) {
numerators.set(n_index, numerators.get(n_index) / trial_factor);
denominators.set(d_index, denominators.get(d_index) / trial_factor);
continue;
}
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;
}
}
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("");
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:");
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();
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");
}
}