Class MyFractionAlgebra

    • Field Detail

      • exact

        protected boolean exact
        Records if the fraction representation with int numerator and denomonator is exact or an approximation. Is internally changed, if during calculations intermedeate results cannot be exactly represented with int numerator and denominator.
        See Also:
        toString()
    • Constructor Detail

      • MyFractionAlgebra

        public MyFractionAlgebra​(int numerator,
                                 int denominator)
        Constructs a new Fraction object.
      • MyFractionAlgebra

        public MyFractionAlgebra​(int numerator)
        Equivalent to new MyFractionAlgebra( numerator, 1 ).
        Required for mixed operations: int op fraction, fraction op int
      • MyFractionAlgebra

        public MyFractionAlgebra​(double x)
        Finds a fraction that represents x, using the continued fraction algorithm.
        Required for mixed operations: double op fraction, fraction op double.
        See Also:
        continued fraction
    • Method Detail

      • getNumerator

        public int getNumerator()
        Returns the internally stored numerator.
      • getDenominator

        public int getDenominator()
        Returns the internally stored denominator.
      • isExact

        public boolean isExact()
        Returns, if the fraction (with int numerator and denominator) is exact (otherwise it is an approximation).
        See Also:
        exact
      • setExact

        public MyFractionAlgebra setExact​(boolean exact)
        Declares that this fraction instance is considered exact.
        Returns:
        this
        See Also:
        exact
      • doubleValue

        public double doubleValue()
        Returns this fraction as double.
      • cloneThis

        public JxnCloneableAlgebra cloneThis()
        Note: MyFractionAlgebra implements JxnCloneableAlgebra only for demonstration. As MyFractionAlgebra has only 2 int members numerator and denominator it should better be implemented as immutable like java.math.BigInteger or Commons MathBigFraction .

        Description copied from interface: JxnCloneableAlgebra
        Called internally. Required to avoid overwriting the left side operand opndLeft in opndLeft op opndRght, if op is implemented as opndLeft.op(opndRght) and the instance method op modifies the instance.
        Specified by:
        cloneThis in interface JxnCloneableAlgebra
        Returns:
        copy of this instance
      • pow

        public MyFractionAlgebra pow​(int exponent)
        Replaces this by its power of exponent.
        Returns:
        this
      • equals

        public boolean equals​(Object other)
        Fractions are equal, if both numerator and denominator are equal.
        (the exactness-information is not required to be equal)
        Overrides:
        equals in class Object
      • toString

        public String toString()
        Returns a string representation of the fraction.

        Shows the fraction as  "numerator / denominator = double-value", if int valued numerator and denominator allow an exact representation of the fraction, e.g. "1 / 8 = 0.125". However, if the fraction is an approximation, it is formatted as "double-value ~ numerator / denominator" e.g. "0.3 ~ 3 / 10" (there is no exact binary representation of the decimal 0.3).
        Note the difference:
            f1 = @MyFractionAlgebra( 3, 10 )  ! exact fraction
             = 3 / 10 = 0.3 (MyFractionAlgebra)
            f2 = @MyFractionAlgebra( 0.3 )  ! approximation of the double 0.3
             = 0.3 ~ 3 / 10 (MyFractionAlgebra)  
        The resulting fraction instances f1 and f2 are equal, but only f1 considers itself exact. A Calculation with fraction instances transfers the exactness-information to its result.
        Overrides:
        toString in class Object
        See Also:
        exact
      • gcd

        public static int gcd​(int m,
                              int n)
        Returns the greatest common divisor (gcd) of m and n.
      • reduce

        protected void reduce()
        Reduces the fraction: devides numerator and denominator by their greatest common divisor.
      • power

        protected int power​(int base,
                            int exponent)
        power.
        Parameters:
        exponent - must be >= 0
      • main

        public static void main​(String[] args)
        Test.