Fraction:
- Added multiply, print decimal.
This commit is contained in:
parent
225aa67765
commit
381f5105c6
@ -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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user