public class Fraction
extends java.lang.Object
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) |
Modifier and Type | Field and Description |
---|---|
static Fraction |
ZERO
The value of zero as a Fraction.
|
Constructor and Description |
---|
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.
|
Modifier and Type | Method and Description |
---|---|
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.
|
public static final Fraction ZERO
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.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()