|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcox.jmatt.java.MathTools.MFraction
public final class MFraction
This class encapsulates the behavior and manipulation of fractions. It provides static and instance methods to create, add, subtract, multiply, divide, raise, and reduce fractions. It also provides a rich capability to format these fractions many ways. The createMFraction() method can take a fraction in a commonly-typed format, e.g. '-2 4/9', and create an MFraction from it. From there it can be easily manipulated.
Once created, an MFraction is immutable. The only things that change it are methods that do not change the value of the underlying fraction. The raise() and reduce() methods, for instance, multiply or divide both the numerator and denominator by the same (non-zero!) quantity. This may change the appearance of the fraction but it does not change the fraction itself.
Internally the fraction is maintained as a sign multiplier, a numerator, and a denominator. There are methods to test whether the fraction is proper or improper, and methods to produce the numbers needed to format as mixed numbers or improper fractions. MFraction's versatile format() methods allow access to all of these values, raw or 'cooked.'
Field Summary | |
---|---|
static java.lang.String |
DEFAULT_FRACTION_BAR
Default fraction bar symbol: a single slash ('/'). |
static java.lang.String |
FORMAT_DEBUG
Dump ALL replacement tokens. |
static java.lang.String |
FORMAT_LATEX
LaTeX fraction, (signed) numerator over denominator. |
static java.lang.String |
FORMAT_LATEX_MIXED
LaTeX mixed fraction. |
static java.lang.String |
FORMAT_LATEX_TYPICAL
Typical LaTeX format. |
static java.lang.String |
FORMAT_MATHML
MathML presentation standard 'mfrac' tag with just the numerator() and denominator() present. |
static java.lang.String |
FORMAT_MATHML_MIXED
MathML mixed number with explicit whole part and fractional numerator. |
static java.lang.String |
FORMAT_MATHML_TYPICAL
Typical format for MathML. |
static java.lang.String |
FORMAT_OO_MIXED
OpenOffice fraction, per STANDARD_MIXED. |
static java.lang.String |
FORMAT_OO_STANDARD
OpenOffice fraction format, per STANDARD with (plain) numerator and denominator. |
static java.lang.String |
FORMAT_OO_TYPICAL
Typical format for OpenOffice. |
static java.lang.String |
FORMAT_STANDARD
Standard (default) format for a fraction: <0>/<1>. |
static java.lang.String |
FORMAT_STANDARD_MIXED
Standard mixed format: <4> <5>/<1>. |
static java.lang.String |
FORMAT_TYPICAL
Typical format: mixed if improper, single number if whole or unit, proper if proper. |
Constructor Summary | |
---|---|
MFraction()
Zero-argument constructor. |
|
MFraction(int pNumerator,
int pDenominator)
Standard constructor: numerator and denominator specified. |
|
MFraction(int pWhole,
int pNumerator,
int pDenominator)
Mixed-number constructor: whole part, numerator and denominator. |
|
MFraction(MFraction pMFraction)
Copy constructor. |
|
MFraction(java.lang.String pFraction)
Constructor for a String containing a single-line-formatted fraction or mixed number. |
Method Summary | |
---|---|
int |
absNumerator()
Return the unsigned (absolute) value of the numerator. |
MFraction |
add(MFraction pFrac)
Add this MFraction to another. |
int |
compareTo(MFraction pMFrac)
Compare this MFraction to another. |
static MFraction |
createDyadic(int pDen)
Create a random MFraction whose denominator is 2pDen and whose numerator is an odd number between 1 and one less than the denominator. |
static MFraction |
createIrreducible(int pWhole,
int pLow,
int pHigh,
int pDenom)
Create a fraction that cannot be reduced. |
static MFraction |
createMFraction(int pWhole,
int pNum,
int pDen)
Create a MFraction given the whole part, numerator and denominator as type int. |
static MFraction |
createMFraction(java.lang.String pFraction)
Parse a typically-formatted String into an MFraction. |
int |
denominator()
Get the fraction's denominator. |
MFraction |
divide(MFraction pFrac)
Divide this fraction by another MFraction. |
boolean |
equals(MFraction pMFrac)
Test for equality between this MFraction and another. |
java.lang.String |
format_MFraction(MFraction pFraction,
java.lang.String pFormat)
Instance version of formatMFraction(). |
java.lang.String |
format()
Format this per its current furmat. |
java.lang.String |
format(java.lang.String pFormat)
Format this MFraction per the supplied format. |
static java.lang.String |
formatMFraction(MFraction pFraction,
java.lang.String pFormat)
Format an MFraction according to the specified format string. |
java.lang.String |
getBar()
Return the current bar value, just in case it's needed. |
boolean |
isImproper()
Return true if the numerator is (strictly!) greater than the denominator. |
boolean |
isProper()
Return true if the numerator is (strictly) less than the denominator. |
boolean |
isUnit()
Return true if the numerator equals the denominator. |
boolean |
isWhole()
Return true if the numerator is evenly divisible by the denominator. |
int |
mixedNumerator()
Return the NUMERATOR of the fractional part of this fraction expressed as a mixed number. |
MFraction |
multiply(MFraction pFrac)
Multiply this MFraction by another. |
MFraction |
newDyadic(int pDen)
Instance version of createDyadic() . |
MFraction |
newIrreducible(int pWhole,
int pLow,
int pHigh,
int pDenom)
Instance shadow for an irreducible fraction. |
MFraction |
newMFraction(int pWhole,
int pNum,
int pDen)
Instance version of createMFraction(int, int, int) . |
MFraction |
newMFraction(java.lang.String pFraction)
Instance version of createMFraction(). |
MFraction |
normalize()
This method takes care of the MFraction's signs. |
int |
numerator()
Get the fraction's numerator. |
MFraction |
raise(int pNum)
Multiply the numerator and denominator by the same constant. |
MFraction |
reciprocal()
Create and return the reciprocal of this fraction if it exists. |
MFraction |
reduce()
Reduce completely. |
MFraction |
reduce(int pNum)
Reduce the fraction by pNum. |
java.lang.String |
sCompareTo(MFraction pRHFrac)
Compare per compareTo() but returns '<,' '=' or '>' instead of a number. |
void |
setBar(java.lang.String pBar)
Set the fraction bar String for this instance. |
static void |
setDefaultBar(java.lang.String pBar)
Set the String used for the default fraction bar. |
static void |
setDefaultFormat(java.lang.String pFormat)
Set the default format for all MFractions created after this is set. |
MFraction |
setFormat(java.lang.String pFormat)
Set the format for this MFraction. |
void |
setNewDefaultBar(java.lang.String pBar)
Instance default bar set method. |
void |
setNewDefaultFormat(java.lang.String pFormat)
Instance alias for setDefaultFormat(). |
int |
signum()
Return 1 if the fraction is positive, 0 if it is zero, and -1 if it is negative. |
MFraction |
subtract(MFraction pFrac)
Subtract another fraction from this one. |
double |
toDouble()
Return the double value of this MFraction. |
java.lang.String |
toString()
Return a constructor-like representation of this MFraction. |
java.lang.String |
toXML(java.lang.String pID)
Return a simple XML format for this MFraction. |
int |
wholePart()
Return the whole part of the fraction expressed as a mixed number. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public static final java.lang.String DEFAULT_FRACTION_BAR
public static final java.lang.String FORMAT_STANDARD
public static final java.lang.String FORMAT_STANDARD_MIXED
public static final java.lang.String FORMAT_TYPICAL
public static final java.lang.String FORMAT_OO_STANDARD
public static final java.lang.String FORMAT_OO_MIXED
public static final java.lang.String FORMAT_OO_TYPICAL
public static final java.lang.String FORMAT_MATHML
public static final java.lang.String FORMAT_MATHML_MIXED
public static final java.lang.String FORMAT_MATHML_TYPICAL
public static final java.lang.String FORMAT_LATEX
public static final java.lang.String FORMAT_LATEX_MIXED
public static final java.lang.String FORMAT_LATEX_TYPICAL
public static final java.lang.String FORMAT_DEBUG
Constructor Detail |
---|
public MFraction()
public MFraction(int pNumerator, int pDenominator)
pNumerator
- The numerator of the fraction.pDenominator
- The denominator of the fraction.
java.lang.ArithmeticException
- if the denominator is zero. NOT ALLOWED!public MFraction(int pWhole, int pNumerator, int pDenominator)
pWhole
- The whole part of the mixed number.pNumerator
- The numerator of the fractional part.pDenominator
- The denominator of the fractional part.
java.lang.ArithmeticException
- if the denominator is zero.public MFraction(java.lang.String pFraction)
java.lang.ArithmeticException
- if the denominator is zero.public MFraction(MFraction pMFraction)
java.lang.NullPointerException
- if pMFraction is null.Method Detail |
---|
public static final MFraction createMFraction(java.lang.String pFraction)
Parse a typically-formatted String into an MFraction. A typically-formatted String is a string containing the typical typewriter or keyboard formats of fractionnal notation: '4/9' or '3 1/5'. Because of the way this method parses a String, the space delimiter will work in both places. For instance, '3 1/5' could also be 3 1 5'. Slashes also work in both places.
If an error occurs during parsing it is reported at Error level and the zero fraciton is returned. That is, this method guarantees a valid MFraction, just not necessarily the one in the String.
pFraction
- A String consisting of a single-line representation of a fraction, e.g. '3/8 or -12 5/7'.
public MFraction newMFraction(java.lang.String pFraction)
public static MFraction createMFraction(int pWhole, int pNum, int pDen)
pWhole
- The whole part of the fraction. May be zero.pNum
- The numerator of the fraction. May be zero.pDen
- The denominator of the fraction. MUST NOT BE ZERO!!!public MFraction newMFraction(int pWhole, int pNum, int pDen)
createMFraction(int, int, int)
.
public static MFraction createDyadic(int pDen)
pDen
- The power of two for the denominator.public MFraction newDyadic(int pDen)
createDyadic()
.
public static MFraction createIrreducible(int pWhole, int pLow, int pHigh, int pDenom)
Arithmetic.coprime()
method to generate the
numerator so all range and return rules apply. Note: the range/divisor parameters here are in the same order as the 'coprime()' method.
pWhole
- The whole part of the fraction.pDenom
- The denominator of the fraction.pLow
- The low value for the numerator range, inclusive.pHigh
- The high value for the numerator range, exclusive.public MFraction newIrreducible(int pWhole, int pLow, int pHigh, int pDenom)
public static java.lang.String formatMFraction(MFraction pFraction, java.lang.String pFormat)
If something goes horribly wrong the String '0/1' is returned.
public java.lang.String format_MFraction(MFraction pFraction, java.lang.String pFormat)
public static void setDefaultFormat(java.lang.String pFormat)
public void setNewDefaultFormat(java.lang.String pFormat)
public MFraction setFormat(java.lang.String pFormat)
public static void setDefaultBar(java.lang.String pBar)
public void setNewDefaultBar(java.lang.String pBar)
public void setBar(java.lang.String pBar)
public java.lang.String getBar()
public java.lang.String format(java.lang.String pFormat)
public java.lang.String format()
public MFraction normalize()
public int signum()
public int numerator()
public int denominator()
public boolean isImproper()
public boolean isWhole()
public boolean isUnit()
public boolean isProper()
public int wholePart()
public int mixedNumerator()
public int absNumerator()
public double toDouble()
public java.lang.String toString()
toString
in class java.lang.Object
public boolean equals(MFraction pMFrac)
java.lang.NullPointerException
- if pMFrac is null.public int compareTo(MFraction pMFrac)
java.lang.NullPointerException
- if pMFrac is null.public java.lang.String sCompareTo(MFraction pRHFrac)
compareTo()
but returns '<,' '=' or '>' instead of a number. If pRHFrac is null the return is '*ERROR*.'
public MFraction add(MFraction pFrac)
public MFraction subtract(MFraction pFrac)
public MFraction multiply(MFraction pFrac)
public MFraction divide(MFraction pFrac)
java.lang.ArithmeticException
- if pFrac is zero.public MFraction reciprocal()
java.lang.ArithmeticException
- if the reciprocal would have a zero denominator.public MFraction raise(int pNum)
public MFraction reduce(int pNum)
public MFraction reduce()
public java.lang.String toXML(java.lang.String pID)
pID
- The id attribute on the resulting tag. If null or empty no 'id' attribute is set.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |