Fraction:

- Added subtract and divide methods
This commit is contained in:
HeshamTB 2020-08-30 17:24:40 +03:00
parent 34e2031f4c
commit bce5d75ba8
Signed by: Hesham
GPG Key ID: 74876157D199B09E

View File

@ -98,11 +98,29 @@ public class Fraction {
return new Fraction(mNumeratorResult, mDenominatorResult);
}
/**
* subtract two fractions
* @param fraction1
* @param fraction2
* @return subtracted fractions reduced to min form
*/
public static Fraction subtract(Fraction fraction1, Fraction fraction2){
//new denominator
int mDenominatorResult = fraction1.denominator * fraction2.denominator;
int mNumerator1 = fraction1.numerator * fraction2.denominator;
int mNumerator2 = fraction2.numerator * fraction1.denominator;
//new num
int mNumeratorResult = mNumerator1 - mNumerator2;
return new Fraction(mNumeratorResult, mDenominatorResult);
}
/**
* Multiply two fractions and reduce
* @param fraction1
* @param fraction2
* @return
* @return fractions
*/
public static Fraction multiply(Fraction fraction1, Fraction fraction2) {
@ -112,6 +130,19 @@ public class Fraction {
}
/**
* Divide two fractions and reduce
* @param fraction1
* @param fraction2
* @return fractions
*/
public static Fraction divide(Fraction fraction1, Fraction fraction2){
return new Fraction(fraction1.numerator * fraction2.denominator,
fraction1.denominator * fraction2.numerator);
}
/**
* string of the fraction in num/den form
* overrides global toString()