Fraction:

- Added multiply, print decimal.
This commit is contained in:
HeshamTB 2020-08-30 15:25:08 +03:00
parent 225aa67765
commit 381f5105c6
Signed by: Hesham
GPG Key ID: 74876157D199B09E

View File

@ -70,11 +70,36 @@ public class Fraction {
} }
public static Fraction add(Fraction fraction1, Fraction fraction2){ public static Fraction add(Fraction fraction1, Fraction fraction2){
return null; return null;
}
/**
* Multiply two fractions and reduce
* @param fraction1
* @param fraction2
* @return
*/
public static Fraction multiply(Fraction fraction1, Fraction fraction2) {
//The constructor already reduces.
return new Fraction(fraction1.numerator * fraction2.numerator,
fraction1.denominator * fraction2.denominator);
} }
public String toString(){ public String toString(){
return String.format("%d/%d", this.numerator, this.denominator); return String.format("%d/%d", this.numerator, this.denominator);
}
public String decimal(int digits){
double decimal = this.numerator / this.denominator;
return String.format("%."+digits, decimal);
} }
} }