|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||
java.lang.Objectcom.marringtons.number.Fraction
This is an immutable class representing fractions as 3 longs. Fractions are always maintained in reduced form. Fractions are truly magic things. 1/3 is way more accurate than 0.3333333333333. It is also easier to say and understand. Fractions multiply and divide way more quickly than floating point, but add more slowly (if denominators don't match). Have fun.
| Field Summary | |
int |
after
Set by string constructor to the first non-space character after the number parsed. |
long |
denominator
Denominator for the fractional number (the 3 in 1-2/3) |
boolean |
empty
Set by string constructor if the string did not contain a fractional number. |
long |
numerator
Numerator for the fractional number (the 2 in 2/3) |
| Constructor Summary | |
Fraction(long wholePart,
long numerator,
long denominator)
Constructor must fill final components of the fraction. |
|
Fraction(String string)
Constructor to create fraction from a string. |
|
| Method Summary | |
int |
compareTo(Object other)
|
Fraction |
divide(Fraction by)
Divide one fraction by another. |
boolean |
equals(Object other)
|
static long |
greatestCommonDenominator(long numerator,
long denominator)
|
int |
hashCode()
|
Fraction |
inverse()
Invert the fraction (2/3 becomes 1-1/3). |
Fraction |
minus(Fraction less)
Subtraction ine Fraction from another. |
Fraction |
negative()
Negate a fraction |
Fraction |
plus(Fraction add)
Add two fractions together. |
Fraction |
times(Fraction by)
Multiply two fractions. |
String |
toHTML()
String representation of a fractional number - making fractional part a superscript. |
String |
toString()
String representation of a fractional number is 1, 1-1/3 or 2/5. |
| Methods inherited from class java.lang.Object |
getClass, notify, notifyAll, wait, wait, wait |
| Field Detail |
public final long numerator
public final long denominator
public final transient boolean empty
String myInput = "1 2/3"; Fraction fraction = new Fraction( myInput); if (fraction.empty) Log( "Please enter a valid fraction");
public final transient int after
String myInput = "1 2/3 abc"; Fraction fraction = new Fraction( myInput); check( myInput.substring( fraction.after).equals( "abc");
| Constructor Detail |
public Fraction(long wholePart,
long numerator,
long denominator)
wholePart - The 1 in 1-2/3numerator - The 2 in 1-2/3denominator - The 3 in 1-2/3public Fraction(String string)
string - | Method Detail |
public static long greatestCommonDenominator(long numerator,
long denominator)
numerator - Top part of the fractiondenominator - Lower part of the fraction
public String toString()
Object.toString()public String toHTML()
Object.toString()public int compareTo(Object other)
compareTo in interface ComparableComparable.compareTo(java.lang.Object)public Fraction negative()
public Fraction inverse()
public Fraction plus(Fraction add)
add - Fraction to add.
public Fraction minus(Fraction less)
less - Fraction to subtract from this one.
public Fraction times(Fraction by)
by - Fraction to multiply by.
public Fraction divide(Fraction by)
by - Fraction to divide with.
public boolean equals(Object other)
Object.equals(java.lang.Object)public int hashCode()
Object.hashCode()
|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||