package com.bean; public class Fraction { private Integer molecule; private Integer denominator;//!=0 public Fraction(Integer molecule, Integer denominator){ this.denominator = denominator; this.molecule = molecule; } public Integer getMolecule() { return molecule; } public void setMolecule(Integer molecule) { this.molecule = molecule; } public Integer getDenominator() { return denominator; } public void setDenominator(Integer denominator) { this.denominator = denominator; } @Override public String toString() { return "Fraction [molecule=" + molecule + ", denominator=" + denominator + "]. " + molecule + "/" + denominator; } }
package com.acm; import com.bean.Fraction; public class FractionOperation { public static Fraction add(Fraction value1, Fraction value2) { Fraction ret = new Fraction(value1.getMolecule() * value2.getDenominator() + value1.getDenominator() * value2.getMolecule(), value1.getDenominator() * value2.getDenominator()); Integer miniDivisor = miniDivisor(ret.getDenominator(), ret.getMolecule()); ret.setDenominator(ret.getDenominator() / miniDivisor); ret.setMolecule(ret.getMolecule() / miniDivisor); return ret; } public static Fraction subtrac(Fraction value1, Fraction value2) { Fraction ret = new Fraction(value1.getMolecule() * value2.getDenominator() - value1.getDenominator() * value2.getMolecule(), value1.getDenominator() * value2.getDenominator()); Integer miniDivisor = miniDivisor(ret.getDenominator(), ret.getMolecule()); ret.setDenominator(ret.getDenominator() / miniDivisor); ret.setMolecule(ret.getMolecule() / miniDivisor); return ret; } public static Fraction multil(Fraction value1, Fraction value2) { Fraction ret = new Fraction(value1.getMolecule() * value2.getMolecule(), value1.getDenominator() * value2.getDenominator()); Integer miniDivisor = miniDivisor(ret.getDenominator(), ret.getMolecule()); ret.setDenominator(ret.getDenominator() / miniDivisor); ret.setMolecule(ret.getMolecule() / miniDivisor); return ret; } public static Fraction dividen(Fraction value1, Fraction value2) { Fraction ret = new Fraction(value1.getMolecule() * value2.getDenominator(), value1.getDenominator() * value2.getMolecule()); Integer miniDivisor = miniDivisor(ret.getDenominator(), ret.getMolecule()); ret.setDenominator(ret.getDenominator() / miniDivisor); ret.setMolecule(ret.getMolecule() / miniDivisor); return ret; } public static Integer miniDivisor(Integer value1, Integer value2) { Integer temp = new Integer(1); Integer total = temp; for (int i = 2, j = value1 / 2 + 1; i < j;) { temp = new Integer(i); if (value1 % i != 0) { i++; } else { value1 = value1 / i; if (value2 % i == 0) { total = total * i; value2 = value2 / i; } } } return total; } public static Integer leastCommonMultiple(Integer value1, Integer value2) { if(miniDivisor(value1, value2) == 1){ return value1 * value2; } Integer temp = value1; if (value1 < value2) { temp = value2; } for (Integer i = temp; i <= value1 * value2; i++) { if (i % value1 == 0 && i % value2 == 0) { temp = i; break; } } return temp; } public static void main(String[] args) { System.out.println(miniDivisor(new Integer(9), new Integer(6))); System.out.println(miniDivisor(new Integer(9), new Integer(15))); System.out.println(miniDivisor(new Integer(24), new Integer(6))); System.out.println(miniDivisor(new Integer(24), new Integer(21))); System.out .println(leastCommonMultiple(new Integer(24), new Integer(32))); System.out .println(leastCommonMultiple(new Integer(24), new Integer(35))); Fraction value1 = new Fraction(4, 15); Fraction value2 = new Fraction(2, 9); System.out.println(add(value1, value2)); System.out.println(subtrac(value1, value2)); System.out.println(multil(value1, value2)); System.out.println(dividen(value1, value2)); } }