改写课本Listing 13.13的分数类Rational,将分子和分母从类型long改为BigInteger,新版分数类名为MyRational。
写一个测试类,测试MyRational。
import java.math.*;
class MyRational extends Number implements Comparable<MyRational>{
private BigInteger numerator=BigInteger.ZERO;
private BigInteger denominator=BigInteger.ONE;
public MyRational(){
this(BigInteger.ZERO, BigInteger.ONE);
}
public MyRational(BigInteger numerator,BigInteger denominator){
BigInteger gcd=numerator.gcd(denominator);
this.numerator=numerator.divide(gcd);
this.denominator=denominator.divide(gcd);
}
public BigInteger getNumerator(){
return numerator;
}
public BigInteger getDenominator(){
return denominator;
}
public MyR