Class Fraction
- All Implemented Interfaces:
Serializable,Comparable
Fraction is a Number implementation that
stores fractions accurately.
This class is immutable, and interoperable with most methods that accept
a Number.
- Since:
- 2.0
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final FractionDeprecated.Fractionrepresentation of 4/5.static final FractionDeprecated.Fractionrepresentation of 1.static final FractionDeprecated.Fractionrepresentation of 1/5.static final FractionDeprecated.Fractionrepresentation of 1/2.static final FractionDeprecated.Fractionrepresentation of 1/4.static final FractionDeprecated.Fractionrepresentation of 1/3.static final FractionDeprecated.Fractionrepresentation of 3/5.static final FractionDeprecated.Fractionrepresentation of 3/4.static final FractionDeprecated.Fractionrepresentation of 2/5.static final FractionDeprecated.Fractionrepresentation of 2/4.static final FractionDeprecated.Fractionrepresentation of 2/3.static final FractionDeprecated.Fractionrepresentation of 0. -
Method Summary
Modifier and TypeMethodDescriptionabs()Deprecated.Gets a fraction that is the positive equivalent of this one.Deprecated.Adds the value of this fraction to another, returning the result in reduced form.intDeprecated.Compares this object to another based on size.Deprecated.Divide the value of this fraction by another.doubleDeprecated.Gets the fraction as adouble.booleanDeprecated.Compares this fraction to another object to test if they are equal.floatDeprecated.Gets the fraction as afloat.intDeprecated.Gets the denominator part of the fraction.static FractiongetFraction(double value) Deprecated.Creates aFractioninstance from adoublevalue.static FractiongetFraction(int numerator, int denominator) Deprecated.Creates aFractioninstance with the 2 parts of a fraction Y/Z.static FractiongetFraction(int whole, int numerator, int denominator) Deprecated.Creates aFractioninstance with the 3 parts of a fraction X Y/Z.static FractiongetFraction(String str) Deprecated.Creates a Fraction from aString.intDeprecated.Gets the numerator part of the fraction.intDeprecated.Gets the proper numerator, always positive.intDeprecated.Gets the proper whole part of the fraction.static FractiongetReducedFraction(int numerator, int denominator) Deprecated.Creates a reducedFractioninstance with the 2 parts of a fraction Y/Z.inthashCode()Deprecated.Gets a hashCode for the fraction.intintValue()Deprecated.Gets the fraction as anint.invert()Deprecated.Gets a fraction that is the inverse (1/fraction) of this one.longDeprecated.Gets the fraction as along.multiplyBy(Fraction fraction) Deprecated.Multiplies the value of this fraction by another, returning the result in reduced form.negate()Deprecated.Gets a fraction that is the negative (-fraction) of this one.pow(int power) Deprecated.Gets a fraction that is raised to the passed in power.reduce()Deprecated.Reduce the fraction to the smallest values for the numerator and denominator, returning the result.Deprecated.Subtracts the value of another fraction from the value of this one, returning the result in reduced form.Deprecated.Gets the fraction as a properStringin the format X Y/Z.toString()Deprecated.Gets the fraction as aString.Methods inherited from class java.lang.Number
byteValue, shortValue
-
Field Details
-
ZERO
Deprecated.Fractionrepresentation of 0. -
ONE
Deprecated.Fractionrepresentation of 1. -
ONE_HALF
Deprecated.Fractionrepresentation of 1/2. -
ONE_THIRD
Deprecated.Fractionrepresentation of 1/3. -
TWO_THIRDS
Deprecated.Fractionrepresentation of 2/3. -
ONE_QUARTER
Deprecated.Fractionrepresentation of 1/4. -
TWO_QUARTERS
Deprecated.Fractionrepresentation of 2/4. -
THREE_QUARTERS
Deprecated.Fractionrepresentation of 3/4. -
ONE_FIFTH
Deprecated.Fractionrepresentation of 1/5. -
TWO_FIFTHS
Deprecated.Fractionrepresentation of 2/5. -
THREE_FIFTHS
Deprecated.Fractionrepresentation of 3/5. -
FOUR_FIFTHS
Deprecated.Fractionrepresentation of 4/5.
-
-
Method Details
-
getFraction
Deprecated.Creates a
Fractioninstance with the 2 parts of a fraction Y/Z.Any negative signs are resolved to be on the numerator.
- Parameters:
numerator- the numerator, for example the three in 'three sevenths'denominator- the denominator, for example the seven in 'three sevenths'- Returns:
- a new fraction instance
- Throws:
ArithmeticException- if the denomiator iszero
-
getFraction
Deprecated.Creates a
Fractioninstance with the 3 parts of a fraction X Y/Z.The negative sign must be passed in on the whole number part.
- Parameters:
whole- the whole number, for example the one in 'one and three sevenths'numerator- the numerator, for example the three in 'one and three sevenths'denominator- the denominator, for example the seven in 'one and three sevenths'- Returns:
- a new fraction instance
- Throws:
ArithmeticException- if the denomiator iszeroArithmeticException- if the denominator is negativeArithmeticException- if the numerator is negativeArithmeticException- if the resulting numerator exceedsInteger.MAX_VALUE
-
getReducedFraction
Deprecated.Creates a reduced
Fractioninstance with the 2 parts of a fraction Y/Z.For example, if the input parameters represent 2/4, then the created fraction will be 1/2.
Any negative signs are resolved to be on the numerator.
- Parameters:
numerator- the numerator, for example the three in 'three sevenths'denominator- the denominator, for example the seven in 'three sevenths'- Returns:
- a new fraction instance, with the numerator and denominator reduced
- Throws:
ArithmeticException- if the denominator iszero
-
getFraction
Deprecated.Creates a
Fractioninstance from adoublevalue.This method uses the continued fraction algorithm, computing a maximum of 25 convergents and bounding the denominator by 10,000.
- Parameters:
value- the double value to convert- Returns:
- a new fraction instance that is close to the value
- Throws:
ArithmeticException- if|value| > Integer.MAX_VALUEorvalue = NaNArithmeticException- if the calculated denominator iszeroArithmeticException- if the the algorithm does not converge
-
getFraction
Deprecated.Creates a Fraction from a
String.The formats accepted are:
doubleString containing a dot- 'X Y/Z'
- 'Y/Z'
- 'X' (a simple whole number)
- Parameters:
str- the string to parse, must not benull- Returns:
- the new
Fractioninstance - Throws:
IllegalArgumentException- if the string isnullNumberFormatException- if the number format is invalid
-
getNumerator
public int getNumerator()Deprecated.Gets the numerator part of the fraction.
This method may return a value greater than the denominator, an improper fraction, such as the seven in 7/4.
- Returns:
- the numerator fraction part
-
getDenominator
public int getDenominator()Deprecated.Gets the denominator part of the fraction.
- Returns:
- the denominator fraction part
-
getProperNumerator
public int getProperNumerator()Deprecated.Gets the proper numerator, always positive.
An improper fraction 7/4 can be resolved into a proper one, 1 3/4. This method returns the 3 from the proper fraction.
If the fraction is negative such as -7/4, it can be resolved into -1 3/4, so this method returns the positive proper numerator, 3.
- Returns:
- the numerator fraction part of a proper fraction, always positive
-
getProperWhole
public int getProperWhole()Deprecated.Gets the proper whole part of the fraction.
An improper fraction 7/4 can be resolved into a proper one, 1 3/4. This method returns the 1 from the proper fraction.
If the fraction is negative such as -7/4, it can be resolved into -1 3/4, so this method returns the positive whole part -1.
- Returns:
- the whole fraction part of a proper fraction, that includes the sign
-
intValue
public int intValue()Deprecated.Gets the fraction as an
int. This returns the whole number part of the fraction. -
longValue
public long longValue()Deprecated.Gets the fraction as a
long. This returns the whole number part of the fraction. -
floatValue
public float floatValue()Deprecated.Gets the fraction as a
float. This calculates the fraction as the numerator divided by denominator.- Specified by:
floatValuein classNumber- Returns:
- the fraction as a
float
-
doubleValue
public double doubleValue()Deprecated.Gets the fraction as a
double. This calculates the fraction as the numerator divided by denominator.- Specified by:
doubleValuein classNumber- Returns:
- the fraction as a
double
-
reduce
Deprecated.Reduce the fraction to the smallest values for the numerator and denominator, returning the result.
For example, if this fraction represents 2/4, then the result will be 1/2.
- Returns:
- a new reduced fraction instance, or this if no simplification possible
-
invert
Deprecated.Gets a fraction that is the inverse (1/fraction) of this one.
The returned fraction is not reduced.
- Returns:
- a new fraction instance with the numerator and denominator inverted.
- Throws:
ArithmeticException- if the fraction represents zero.
-
negate
Deprecated.Gets a fraction that is the negative (-fraction) of this one.
The returned fraction is not reduced.
- Returns:
- a new fraction instance with the opposite signed numerator
-
abs
Deprecated.Gets a fraction that is the positive equivalent of this one.
More precisely:
(fraction >= 0 ? this : -fraction)The returned fraction is not reduced.
- Returns:
thisif it is positive, or a new positive fraction instance with the opposite signed numerator
-
pow
Deprecated.Gets a fraction that is raised to the passed in power.
The returned fraction is in reduced form.
- Parameters:
power- the power to raise the fraction to- Returns:
thisif the power is one,ONEif the power is zero (even if the fraction equals ZERO) or a new fraction instance raised to the appropriate power- Throws:
ArithmeticException- if the resulting numerator or denominator exceedsInteger.MAX_VALUE
-
add
Deprecated.Adds the value of this fraction to another, returning the result in reduced form. The algorithm follows Knuth, 4.5.1.
- Parameters:
fraction- the fraction to add, must not benull- Returns:
- a
Fractioninstance with the resulting values - Throws:
IllegalArgumentException- if the fraction isnullArithmeticException- if the resulting numerator or denominator exceedsInteger.MAX_VALUE
-
subtract
Deprecated.Subtracts the value of another fraction from the value of this one, returning the result in reduced form.
- Parameters:
fraction- the fraction to subtract, must not benull- Returns:
- a
Fractioninstance with the resulting values - Throws:
IllegalArgumentException- if the fraction isnullArithmeticException- if the resulting numerator or denominator cannot be represented in anint.
-
multiplyBy
Deprecated.Multiplies the value of this fraction by another, returning the result in reduced form.
- Parameters:
fraction- the fraction to multiply by, must not benull- Returns:
- a
Fractioninstance with the resulting values - Throws:
IllegalArgumentException- if the fraction isnullArithmeticException- if the resulting numerator or denominator exceedsInteger.MAX_VALUE
-
divideBy
Deprecated.Divide the value of this fraction by another.
- Parameters:
fraction- the fraction to divide by, must not benull- Returns:
- a
Fractioninstance with the resulting values - Throws:
IllegalArgumentException- if the fraction isnullArithmeticException- if the fraction to divide by is zeroArithmeticException- if the resulting numerator or denominator exceedsInteger.MAX_VALUE
-
equals
Deprecated.Compares this fraction to another object to test if they are equal.
.To be equal, both values must be equal. Thus 2/4 is not equal to 1/2.
-
hashCode
public int hashCode()Deprecated.Gets a hashCode for the fraction.
-
compareTo
Deprecated.Compares this object to another based on size.
Note: this class has a natural ordering that is inconsistent with equals, because, for example, equals treats 1/2 and 2/4 as different, whereas compareTo treats them as equal.
- Specified by:
compareToin interfaceComparable- Parameters:
object- the object to compare to- Returns:
- -1 if this is less, 0 if equal, +1 if greater
- Throws:
ClassCastException- if the object is not aFractionNullPointerException- if the object isnull
-
toString
Deprecated.Gets the fraction as a
String.The format used is 'numerator/denominator' always.
-
toProperString
Deprecated.Gets the fraction as a proper
Stringin the format X Y/Z.The format used in 'wholeNumber numerator/denominator'. If the whole number is zero it will be ommitted. If the numerator is zero, only the whole number is returned.
- Returns:
- a
Stringform of the fraction
-