Class MyFractionAlgebra
- java.lang.Object
-
- MyFractionAlgebra
-
- All Implemented Interfaces:
JxnCloneableAlgebra
public class MyFractionAlgebra extends Object implements JxnCloneableAlgebra
Simple implementation of a fraction algebra (example of a user defined algebra).
Stores numerator and denominator with int precision. Allows mixed operations with int and double.
For a may be more elaborate implementation see Commons Math →BigFraction
- See Also:
- user defined algebra
-
-
Constructor Summary
Constructors Constructor Description MyFractionAlgebra(double x)
Finds a fraction that representsx
, using the continued fraction algorithm.MyFractionAlgebra(int numerator)
Equivalent tonew MyFractionAlgebra( numerator, 1 )
.MyFractionAlgebra(int numerator, int denominator)
Constructs a new Fraction object.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description MyFractionAlgebra
add(MyFractionAlgebra opnd)
Addsopnd
to this.JxnCloneableAlgebra
cloneThis()
Note:MyFractionAlgebra
implementsJxnCloneableAlgebra
only for demonstration.double
doubleValue()
Returns this fraction as double.boolean
equals(Object other)
Fractions are equal, if both numerator and denominator are equal.static int
gcd(int m, int n)
Returns the greatest common divisor (gcd) ofm
andn
.int
getDenominator()
Returns the internally stored denominator.int
getNumerator()
Returns the internally stored numerator.MyFractionAlgebra
inv()
Replaces this by its reciprocal.MyFractionAlgebra
mul(MyFractionAlgebra opnd)
(Post)Multiplies this byopnd
.MyFractionAlgebra
pow(int exponent)
Replaces this by its power ofexponent
.protected int
power(int base, int exponent)
power.protected void
reduce()
Reduces the fraction: devides numerator and denominator by their greatest common divisor.String
toString()
Returns a string representation of the fraction.
-
-
-
Constructor Detail
-
MyFractionAlgebra
public MyFractionAlgebra(int numerator, int denominator)
Constructs a new Fraction object.
-
MyFractionAlgebra
public MyFractionAlgebra(int numerator)
Equivalent tonew MyFractionAlgebra( numerator, 1 )
.
Required for mixed operations: int op fraction, fraction op int
-
MyFractionAlgebra
public MyFractionAlgebra(double x)
Finds a fraction that representsx
, 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
implementsJxnCloneableAlgebra
only for demonstration. AsMyFractionAlgebra
has only 2 int membersnumerator
anddenominator
it should better be implemented as immutable likejava.math.
BigInteger
or Commons Math →BigFraction
.
Description copied from interface:JxnCloneableAlgebra
Called internally. Required to avoid overwriting the left side operandopndLeft
inopndLeft op opndRght
, ifop
is implemented asopndLeft.op(opndRght)
and the instance methodop
modifies the instance.- Specified by:
cloneThis
in interfaceJxnCloneableAlgebra
- Returns:
- copy of this instance
-
add
public MyFractionAlgebra add(MyFractionAlgebra opnd)
Addsopnd
to this.- Returns:
- this
-
mul
public MyFractionAlgebra mul(MyFractionAlgebra opnd)
(Post)Multiplies this byopnd
.- Returns:
- this
-
inv
public MyFractionAlgebra inv()
Replaces this by its reciprocal.- Returns:
- this
-
pow
public MyFractionAlgebra pow(int exponent)
Replaces this by its power ofexponent
.- Returns:
- this
-
equals
public boolean equals(Object other)
Fractions are equal, if both numerator and denominator are equal.
-
gcd
public static int gcd(int m, int n)
Returns the greatest common divisor (gcd) ofm
andn
.
-
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
-
-