Class MyFractionAlgebra
- java.lang.Object
-
- MyFractionAlgebra
-
- All Implemented Interfaces:
Comparable<MyFractionAlgebra>
,JxnCloneableAlgebra
public class MyFractionAlgebra extends Object implements JxnCloneableAlgebra, Comparable<MyFractionAlgebra>
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
-
-
Field Summary
Fields Modifier and Type Field Description protected int
denominator
protected boolean
exact
Records if the fraction representation with int numerator and denomonator is exact or an approximation.protected int
numerator
-
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.int
compareTo(MyFractionAlgebra other)
Returns -1 ifthis < other
, +1 ifother > this
, else 0.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.boolean
isExact()
Returns, if the fraction (with int numerator and denominator) is exact (otherwise it is an approximation).static void
main(String[] args)
Test.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.MyFractionAlgebra
setExact(boolean exact)
Declares that this fraction instance is considered exact.String
toString()
Returns a string representation of the fraction.
-
-
-
Field Detail
-
numerator
protected int numerator
-
denominator
protected int denominator
-
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 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.
-
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
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.
(the exactness-information is not required to be equal)
-
compareTo
public int compareTo(MyFractionAlgebra other)
Returns -1 ifthis < other
, +1 ifother > this
, else 0.- Specified by:
compareTo
in interfaceComparable<MyFractionAlgebra>
-
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 decimal0.3
).
Note the difference:f1 = @
The resulting fraction instancesMyFractionAlgebra
( 3, 10 ) ! exact fraction = 3 / 10 = 0.3 (MyFractionAlgebra) f2 = @MyFractionAlgebra
( 0.3 ) ! approximation of the double0.3
= 0.3 ~ 3 / 10 (MyFractionAlgebra)f1
andf2
are equal, but onlyf1
considers itself exact. A Calculation with fraction instances transfers the exactness-information to its result.
-
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
-
-