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 intdenominatorprotected booleanexactRecords if the fraction representation with int numerator and denomonator is exact or an approximation.protected intnumerator
-
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 MyFractionAlgebraadd(MyFractionAlgebra opnd)Addsopndto this.JxnCloneableAlgebracloneThis()Note:MyFractionAlgebraimplementsJxnCloneableAlgebraonly for demonstration.intcompareTo(MyFractionAlgebra other)Returns -1 ifthis < other, +1 ifother > this, else 0.doubledoubleValue()Returns this fraction as double.booleanequals(Object other)Fractions are equal, if both numerator and denominator are equal.static intgcd(int m, int n)Returns the greatest common divisor (gcd) ofmandn.intgetDenominator()Returns the internally stored denominator.intgetNumerator()Returns the internally stored numerator.MyFractionAlgebrainv()Replaces this by its reciprocal.booleanisExact()Returns, if the fraction (with int numerator and denominator) is exact (otherwise it is an approximation).static voidmain(String[] args)Test.MyFractionAlgebramul(MyFractionAlgebra opnd)(Post)Multiplies this byopnd.MyFractionAlgebrapow(int exponent)Replaces this by its power ofexponent.protected intpower(int base, int exponent)power.protected voidreduce()Reduces the fraction: devides numerator and denominator by their greatest common divisor.MyFractionAlgebrasetExact(boolean exact)Declares that this fraction instance is considered exact.StringtoString()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:MyFractionAlgebraimplementsJxnCloneableAlgebraonly for demonstration. AsMyFractionAlgebrahas only 2 int membersnumeratoranddenominatorit should better be implemented as immutable likejava.math.BigIntegeror Commons Math →BigFraction.
Description copied from interface:JxnCloneableAlgebra
Called internally. Required to avoid overwriting the left side operandopndLeftinopndLeft op opndRght, ifopis implemented asopndLeft.op(opndRght)and the instance methodopmodifies the instance.- Specified by:
cloneThisin interfaceJxnCloneableAlgebra- Returns:
- copy of this instance
-
add
public MyFractionAlgebra add(MyFractionAlgebra opnd)
Addsopndto 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:
compareToin 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)f1andf2are equal, but onlyf1considers 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) ofmandn.
-
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
-
-