Implementation of rational number class Java BigInteger

Source: Internet
Author: User
Tags gcd

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.