|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectptolemy.math.Fraction
public class Fraction
A class for representing fractions. Fractions are immutable and maintained in lowest terms, with a positive denominator. Thus, 1/2 and 2/4 are interpreted as different names for the same number. Any fraction with the value zero is expressed as 0/1 in lowest terms.
This class only represents fractions with a determined value, so fractions with a zero in the denominator are not allowed (including 0/0).
Yellow (cxh) |
Green (neuendor) |
Field Summary | |
---|---|
private int |
_denominator
|
private int |
_numerator
|
static Fraction |
ZERO
The value of zero as a Fraction. |
Constructor Summary | |
---|---|
Fraction()
Create a new fraction with the value zero (0/1). |
|
Fraction(Fraction f)
Create a new fraction with the same value as the given fraction. |
|
Fraction(int i)
Create a new fraction with the value i/1. |
|
Fraction(int numerator,
int denominator)
Create a new fraction in lowest terms with the value Numerator/Denominator. |
Method Summary | |
---|---|
protected void |
_simplify()
Reduce the fraction to lowest terms by dividing the Numerator and Denominator by their Greatest Common Divisor. |
Fraction |
add(Fraction b)
Add this fraction to the given fraction. |
Fraction |
divide(Fraction b)
Divide this fraction by the given fraction. |
boolean |
equals(java.lang.Object b)
Compare this fraction with the given object. |
int |
getDenominator()
Return the denominator of this fraction. |
int |
getNumerator()
Return the numerator of this fraction. |
boolean |
greaterThan(Fraction testInput)
Test if this Fraction is greater than the input. |
int |
hashCode()
Return a hash code value for this Fraction. |
Fraction |
inverse()
Find the multiplicative inverse of this fraction. |
static int |
lcm(int u,
int v)
Finds the least common multiple(LCM) of two integers. |
Fraction |
multiply(Fraction b)
Multiply this fraction by the given fraction. |
Fraction |
negate()
Find the additive inverse of this fraction. |
Fraction |
subtract(Fraction b)
Subtract the given fraction from this fraction. |
double |
toDouble()
Convert the fraction to a double. |
float |
toFloat()
Convert the fraction to a float. |
java.lang.String |
toString()
Convert the fraction to a readable string. |
Methods inherited from class java.lang.Object |
---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public static final Fraction ZERO
private int _numerator
private int _denominator
Constructor Detail |
---|
public Fraction()
public Fraction(int i)
i
- The numerator.public Fraction(int numerator, int denominator)
numerator
- The numerator.denominator
- The denominator.
java.lang.ArithmeticException
- If the denominator is specified to be
zero.public Fraction(Fraction f)
f
- The given Fraction.Method Detail |
---|
public Fraction add(Fraction b)
b
- The given Fraction.
public Fraction divide(Fraction b)
b
- The given Fraction.
java.lang.ArithmeticException
- If the fraction in the divisor has
a value of zero.public boolean equals(java.lang.Object b)
equals
in class java.lang.Object
public int getDenominator()
public int getNumerator()
public boolean greaterThan(Fraction testInput)
testInput
- The input to compare against.
public int hashCode()
hashCode
in class java.lang.Object
public Fraction inverse()
java.lang.ArithmeticException
- If this fraction has a value of zero,
in which case the multiplicative inverse cannot be represented.public static int lcm(int u, int v)
public Fraction multiply(Fraction b)
public Fraction negate()
public Fraction subtract(Fraction b)
public double toDouble()
public float toFloat()
public java.lang.String toString()
toString
in class java.lang.Object
protected void _simplify()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |