Import Java.math.BigInteger;
public class Rational extends number implements comparable {
Private BigInteger numerator;//molecule
Private BigInteger denominator;//Denominator
/**
* @param args
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub
Rational rational1 = new Rational (new BigInteger (1 + ""),
New BigInteger (10 + ""));
Rational rational2 = new Rational (new BigInteger (1 + ""),
New BigInteger (-10 + ""));
System.out.println (Rational1.add (Rational2));
System.out.println (Rational1.subtract (Rational2));
System.out.println (Rational1.multiple (Rational2));
System.out.println (Rational1.divide (Rational2));
}
Public Rational () {
TODO auto-generated Constructor stub
This (Biginteger.zero, biginteger.one);
}
Public Rational (BigInteger numerator, BigInteger denominator) {
BigInteger gcd = gcd (numerator, denominator);
This.numerator = ((Denominator.compareto (biginteger.zero)) > 0? Biginteger.one
: New BigInteger ( -1 + ")"). Multiply (numerator). Divide (GCD);
This.denominator = Denominator.abs (). Divide (GCD);
}
public static BigInteger gcd (BigInteger A, BigInteger b) {
BigInteger n1 = A.abs ();
BigInteger N2 = B.abs ();
BigInteger remainder = N1.remainder (n2);
while (Remainder.compareto (Biginteger.zero) > 0) {
N1 = n2;
N2 = remainder;
remainder = N1.remainder (n2);
}
return n2;
}
Public BigInteger Getnumerator () {
return numerator;
}
Public BigInteger Getdenominator () {
return denominator;
}
Public rational Add (rational secondrational) {
BigInteger n = numerator.multiply (Secondrational.getdenominator ()). Add (
Denominator.multiply (Secondrational.getnumerator ()));
BigInteger d = denominator.multiply (Secondrational.getdenominator ());
return new Rational (n, D);
}
Public rational subtract (rational secondrational) {
BigInteger n = numerator.multiply (Secondrational.getdenominator ())
. Subtract (Denominator.multiply (Secondrational.getnumerator ()));
BigInteger d = denominator.multiply (Secondrational.getdenominator ());
return new Rational (n, D);
}
Public rational multiple (rational secondrational) {
BigInteger n = numerator.multiply (Secondrational.getnumerator ());
BigInteger d = denominator.multiply (Secondrational.getdenominator ());
return new Rational (n, D);
}
Public rational divide (rational secondrational) {
BigInteger n = numerator.multiply (Secondrational.getdenominator ());
BigInteger d = denominator.multiply (Secondrational.getnumerator ());
return new Rational (n, D);
}
@Override
public boolean equals (Object obj) {
TODO auto-generated Method Stub
if (This.getnumerator (). CompareTo ((Rational) obj). Getnumerator ()) = = 0) {
return true;
}
else {
return false;
}
}
@Override
Public String toString () {
TODO auto-generated Method Stub
if (Denominator.compareto (biginteger.one) = = 0) {
return numerator.tostring ();
}
else {
return numerator.tostring () + "/" + denominator.tostring ();
}
}
@Override
public int intvalue () {
TODO auto-generated Method Stub
return Numerator.divide (Denominator). Intvalue ();
}
@Override
Public long Longvalue () {
TODO auto-generated Method Stub
return Numerator.divide (Denominator). Longvalue ();
}
@Override
public float Floatvalue () {
TODO auto-generated Method Stub
return Numerator.divide (Denominator). Floatvalue ();
}
@Override
Public double Doublevalue () {
TODO auto-generated Method Stub
return Numerator.divide (Denominator). Doublevalue ();
}
@Override
public int compareTo (Object o) {
TODO auto-generated Method Stub
if (This.getnumerator (). CompareTo ((Rational) O). Getnumerator ()) > 0) {
return 1;
}
else if (This.getnumerator (). CompareTo ((Rational) O). Getnumerator ()) < 0) {
return-1;
}
else {
return 0;
}
}
}
Implementation of rational number class Java BigInteger