Class BigFraction


  • public class BigFraction
    extends java.lang.Object
    BigFraction is composed of a BigInteger numerator and a BigInteger denominator. Like BigInteger and BigDecimal, BigFraction is immutable. All fractions are simplified during construction by dividing both numerator and denominator by their greatest common divisor, and the denominator will always be positive.
    • Constructor Summary

      Constructors 
      Constructor Description
      BigFraction​(long numerator, long denominator)
      Construct a BigFraction given two long numerator and denominator values, convert and assign them to BigInteger numerator and denominator values.
      BigFraction​(BigFraction f)
      Given a BigFraction, copy its BigInteger numerator and denominator fields to the fields of the new BigFraction object.
      BigFraction​(java.lang.String s)
      Given a String consisting of two integers separated by a single '/', initialize the numerator and denominator fields with BigIntegers constructed with the substrings before and after the '/', respectively.
      BigFraction​(java.math.BigInteger numerator, java.math.BigInteger denominator)
      Given a BigInteger numerator and denominator, simplify the fraction by dividing both numerator and denominator by their greatest common divisor, and change signs such that the denominator is always positive.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      BigFraction add​(BigFraction b)
      Return a BigFraction resulting from the addition of a given BigFraction b to this BigFraction.
      java.math.BigDecimal asBigDecimal​(int scale, java.math.RoundingMode roundingMode)
      Return a BigDecimal formed by converting the BigInteger numerator and denominator to BigDecimals and then dividing the BigDecimal numerator by the BigDecimal denominator using the given scale and rounding mode.
      BigFraction divide​(BigFraction b)
      Return a BigFraction resulting from the division of a given BigFraction b into this BigFraction.
      java.math.BigInteger getDen()
      Return the BigInteger denominator of the BigFraction.
      java.math.BigInteger getNum()
      Return the BigInteger numerator of the BigFraction.
      BigFraction multiply​(BigFraction b)
      Return a BigFraction resulting from the multiplication of a given BigFraction b with this BigFraction.
      BigFraction negate()
      Return a new BigFraction object with the same denominator and the negated numerator.
      BigFraction subtract​(BigFraction b)
      Return a BigFraction resulting from the subtraction of a given BigFraction b from this BigFraction.
      java.lang.String toString()
      Return a String consisting of the numerator and denominator separated by a single '/'.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • BigFraction

        public BigFraction​(java.math.BigInteger numerator,
                           java.math.BigInteger denominator)
        Given a BigInteger numerator and denominator, simplify the fraction by dividing both numerator and denominator by their greatest common divisor, and change signs such that the denominator is always positive. (The numerator may be negative.) Remember that the BigInteger class contains a gcd method.
        Parameters:
        numerator - BigInteger numerator of the BigFraction
        denominator - BigInteger denominator of the BigFraction
      • BigFraction

        public BigFraction​(BigFraction f)
        Given a BigFraction, copy its BigInteger numerator and denominator fields to the fields of the new BigFraction object.
        Parameters:
        f - given BigFraction object to copy in this constructor
      • BigFraction

        public BigFraction​(java.lang.String s)
        Given a String consisting of two integers separated by a single '/', initialize the numerator and denominator fields with BigIntegers constructed with the substrings before and after the '/', respectively.
        Parameters:
        s - a String representation of a fraction consisting of two integers separated by a single '/'
      • BigFraction

        public BigFraction​(long numerator,
                           long denominator)
        Construct a BigFraction given two long numerator and denominator values, convert and assign them to BigInteger numerator and denominator values.
        Parameters:
        numerator - given long numerator value
        denominator - given long denominator value
    • Method Detail

      • getNum

        public java.math.BigInteger getNum()
        Return the BigInteger numerator of the BigFraction.
        Returns:
        the BigInteger numerator of the BigFraction
      • getDen

        public java.math.BigInteger getDen()
        Return the BigInteger denominator of the BigFraction.
        Returns:
        the BigInteger denominator of the BigFraction
      • toString

        public java.lang.String toString()
        Return a String consisting of the numerator and denominator separated by a single '/'.
        Overrides:
        toString in class java.lang.Object
        Returns:
        a String consisting of the numerator and denominator separated by a single '/'
      • asBigDecimal

        public java.math.BigDecimal asBigDecimal​(int scale,
                                                 java.math.RoundingMode roundingMode)
        Return a BigDecimal formed by converting the BigInteger numerator and denominator to BigDecimals and then dividing the BigDecimal numerator by the BigDecimal denominator using the given scale and rounding mode.
        Parameters:
        scale - the number of decimal places for the division computation
        roundingMode - rounding mode for determining the correct final digit of the division result at the given scale
        Returns:
        a BigDecimal formed by converting the BigInteger numerator and denominator to BigDecimals and then dividing the BigDecimal numerator by the BigDecimal denominator using the given scale and rounding mode
      • negate

        public BigFraction negate()
        Return a new BigFraction object with the same denominator and the negated numerator. Note that BigInteger has a negate method.
        Returns:
        a new BigFraction object with the same denominator and the negated numerator
      • add

        public BigFraction add​(BigFraction b)
        Return a BigFraction resulting from the addition of a given BigFraction b to this BigFraction.
        Parameters:
        b - given BigFraction to add to this BigFraction
        Returns:
        a BigFraction resulting from the addition of this BigFraction to given BigFraction b
      • subtract

        public BigFraction subtract​(BigFraction b)
        Return a BigFraction resulting from the subtraction of a given BigFraction b from this BigFraction.
        Parameters:
        b - given BigFraction to subtract from this BigFraction
        Returns:
        a BigFraction resulting from the subtraction of a given BigFraction b from this BigFraction
      • multiply

        public BigFraction multiply​(BigFraction b)
        Return a BigFraction resulting from the multiplication of a given BigFraction b with this BigFraction.
        Parameters:
        b - given BigFraction to multiply with this BigFraction
        Returns:
        a BigFraction resulting from the multiplication of a given BigFraction b with this BigFraction
      • divide

        public BigFraction divide​(BigFraction b)
        Return a BigFraction resulting from the division of a given BigFraction b into this BigFraction.
        Parameters:
        b - given BigFraction to divide into this BigFraction
        Returns:
        a BigFraction resulting from the division of a given BigFraction b into this BigFraction