Created
May 19, 2019 00:32
-
-
Save ahmednasserpro/9ceb2d973b9fa1ea52b1c2cff5d52dc8 to your computer and use it in GitHub Desktop.
The Rational class with unlimited numbers to process
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Rational extends Number implements Comparable<Rational> { | |
// Data fields for numerator and denominator | |
private BigInteger numerator = BigInteger.ZERO; | |
private BigInteger denominator = BigInteger.ONE; | |
/** Construct a rational with default properties */ | |
public Rational() { | |
this(BigInteger.ZERO, BigInteger.ONE); | |
} | |
/** Construct a rational with specified numerator and denominator | |
* @param numerator the numerator | |
* @param denominator the denominator | |
*/ | |
public Rational (BigInteger numerator, BigInteger denominator) { | |
BigInteger gcd = gcd(numerator, denominator); | |
this.numerator = (denominator.compareTo(BigInteger.ZERO) == 1? | |
BigInteger.ONE : new BigInteger("-1")) | |
.multiply(numerator).divide(gcd); | |
this.denominator = denominator.abs().divide(gcd); | |
} | |
/** Find two GCD for two numbers */ | |
private static BigInteger gcd(BigInteger n, BigInteger d) { | |
BigInteger n1 = n.abs(); | |
BigInteger n2 = d.abs(); | |
BigInteger gcd = BigInteger.ONE; | |
for ( | |
BigInteger k = BigInteger.ONE; | |
n1.compareTo(k) < 1 && n2.compareTo(k) < 1; | |
k.add(BigInteger.ONE)) { | |
if (n1.remainder(k).equals(BigInteger.ZERO) && | |
n2.remainder(k).equals(BigInteger.ZERO)) | |
gcd = k; | |
} | |
return gcd; | |
} | |
/** | |
* Get the numerator | |
* @return the numerator | |
*/ | |
public BigInteger getNumerator() { | |
return numerator; | |
} | |
/** | |
* Get the denominator | |
* @return the denominator | |
*/ | |
public BigInteger getDenominator() { | |
return denominator; | |
} | |
/** | |
* Add a rational number to this rational | |
* @param sec the added rational | |
* @return the new rational after the adding | |
*/ | |
public Rational add(Rational sec) { | |
BigInteger newNumerator = | |
numerator.multiply(sec.denominator). | |
add(sec.getNumerator().multiply(denominator)); | |
BigInteger newDenomenator = denominator.multiply(sec.denominator); | |
return new Rational(newNumerator, newDenomenator); | |
} | |
/** | |
* Subtract a rational from this rational | |
* @param sec the subtracted rational | |
* @return the new rational after the subtraction | |
*/ | |
public Rational subtract(Rational sec) { | |
BigInteger newNumerator = | |
numerator.multiply(sec.denominator). | |
subtract(sec.numerator.multiply(denominator)); | |
BigInteger newDenomenator = denominator.multiply(sec.denominator); | |
return new Rational(newNumerator, newDenomenator); | |
} | |
/** | |
* Multiply a rational number by this rational | |
* @param sec the multiplied rational | |
* @return the new rational after the multiplying | |
*/ | |
public Rational multiply(Rational sec) { | |
BigInteger newNumerator = numerator.multiply(sec.numerator); | |
BigInteger newDenomenator = denominator.multiply(sec.denominator); | |
return new Rational(newNumerator, newDenomenator); | |
} | |
/** | |
* Divide a rational number by this rational | |
* @param secondRational the divided rational | |
* @return the new rational after the dividing | |
*/ | |
public Rational divide(Rational sec) { | |
BigInteger newNumerator = numerator.multiply(sec.denominator); | |
BigInteger newDenomenator = denominator.multiply(sec.numerator); | |
return new Rational(newNumerator, newDenomenator); | |
} | |
@Override | |
public String toString() { | |
if (denominator.equals(BigInteger.ONE)) | |
return numerator.toString(); | |
else | |
return numerator + "/" + denominator; | |
} | |
@Override | |
public boolean equals(Object o) { | |
return this.subtract((Rational) o).getNumerator().equals(BigInteger.ONE); | |
} | |
// Implement the intValue method in Number | |
@Override | |
public int intValue() { | |
return (int) doubleValue(); | |
} | |
// Implement the floatValue method in Number | |
@Override | |
public float floatValue() { | |
return (float) doubleValue(); | |
} | |
// Implement the doubleValue method in Number | |
@Override | |
public double doubleValue() { | |
return numerator.doubleValue() * 1.0 / denominator.doubleValue(); | |
} | |
// Implement the loneValue method in Number | |
@Override | |
public long longValue() { | |
return (long) doubleValue(); | |
} | |
@Override | |
public int compareTo(Rational sec) { | |
BigInteger n = this.subtract((Rational) sec).getNumerator(); | |
if (n.compareTo(BigInteger.ZERO) > 0) { | |
return 1; | |
} else if (n.compareTo(BigInteger.ZERO) < 0) { | |
return -1; | |
} else { | |
return 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment