Class MyFractionAlgebra

    • 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.
      • 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.
        Overrides:
        equals in class Object
      • 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