public class Fraction extends java.lang.Object implements java.io.Serializable, java.lang.Comparable<Fraction>
int
type,
and hence, they maintain closure by wrapping the int
range.
Note also that a Fraction
instance can have a zero denominator either upon creation or after an
operation. Such an instance is treated like any other (it can, for example,
participate in the operations supported by the class without leading to
exceptions) except for two differences:
toString
method
returns the literal NaF
, which stands for "Not a Fraction".
compareTo
and equals
.
Modifier and Type | Field and Description |
---|---|
static boolean |
isQuoted
A flag that determines if the return of the
toProperString()
method is surrounded by quotes or not. |
char |
separator
A character that separates the numerator denominator pair in the return
of the
toString() method. |
Constructor and Description |
---|
Fraction()
Construct a default fraction with numerator equal to 0, denominator equal
to 1, and separator equal to '/'.
|
Fraction(Fraction fraction)
Construct a copy of the passed Fraction.
|
Fraction(int numerator,
int denominator)
Construct a fraction with the passed numerator and denominator and a '/'
separator.
|
Fraction(int numerator,
int denominator,
char separator)
Construct a fraction with the passed numerator, denominator, and
separator.
|
Modifier and Type | Method and Description |
---|---|
void |
add(Fraction other)
Add the passed fraction to the fraction on which it was called.
|
Fraction |
cloneMe()
Copy the state of
this object. |
int |
compareTo(Fraction other)
Compare this object with the specified object for order.
|
void |
divide(Fraction other)
Divide the fraction on which the method was called by the passed
fraction.
|
boolean |
equals(java.lang.Object other)
Determine if
this fraction is the same as the passed one. |
int |
getDenominator()
An accessor to the denominator of
this fraction. |
int |
getNumerator()
An accessor to the numerator of
this fraction. |
static Fraction |
getRandom()
Create a random fraction.
|
char |
getSeparator()
An accessor to the separator of
this fraction. |
int |
hashCode()
Compute a hash code for
this Fraction. |
void |
multiply(Fraction other)
Multiply the fraction on which the method was called by the passed
fraction.
|
void |
pow(int exponent)
Raise the fraction on which the method was called to the passed exponent.
|
boolean |
resembles(Fraction other)
Determine if
this fraction resembles the passed one. |
void |
setDenominator(int denominator)
A mutator for the denominator of
this fraction. |
void |
setFraction(Fraction other)
A mutator for
this fraction. |
void |
setFraction(int numerator,
int denominator)
A mutator for
this fraction. |
void |
setNumerator(int numerator)
A mutator of the numerator of
this fraction. |
static void |
setSeed(int seed)
Change the seed of the random sequence returned by
getRandom() . |
boolean |
setSeparator(char newSeparator)
A mutator of the separator of
this fraction. |
void |
subtract(Fraction other)
Subtract the passed fraction from the fraction on which it was called.
|
java.lang.String |
toProperString()
Return
this fraction as a proper fraction. |
java.lang.String |
toString()
Return a string representation of
this fraction. |
public char separator
toString()
method. The default value is '/'
.
(Bad design--should have been kept private. Access would have still been possible via the existing public accessor and mutator.)
public static boolean isQuoted
toProperString()
method is surrounded by quotes or not. The default value is
true
.
(Bad design--should have been kept private. Access would have still
been possible via a public static accessor, e.g. isQuoted()
and a public static mutator, e.g. setIsQuoted(boolean)
.)
public Fraction()
public Fraction(int numerator, int denominator)
numerator
- the numerator of the fraction to construct.denominator
- the denominator of the fraction to construct.public Fraction(Fraction fraction)
fraction
- the Fraction to copy.java.lang.RuntimeException
- if fraction = null
public Fraction(int numerator, int denominator, char separator)
numerator
- the numerator of the fraction to construct.denominator
- the denominator of the fraction to construct.separator
- the separator of the fraction to construct.public void add(Fraction other)
other
- the fraction to add to this
fraction.public void subtract(Fraction other)
other
- the fraction to subtract from this
fraction.public void multiply(Fraction other)
other
- the fraction to multiply this
fraction bypublic void divide(Fraction other)
other
- the fraction to divide this
fraction bypublic void pow(int exponent)
exponent
- the exponent to raise this
fraction to (must be
non-negative).java.lang.RuntimeException
- if the exponent is negative.public void setNumerator(int numerator)
this
fraction.numerator
- the new numerator.public int getNumerator()
this
fraction.this
fraction.public void setDenominator(int denominator)
this
fraction.denominator
- the new denominator.public int getDenominator()
this
fraction.this
fraction.public void setFraction(int numerator, int denominator)
this
fraction.numerator
- the new numerator of the fraction.denominator
- the new denominator of the fraction.public void setFraction(Fraction other)
this
fraction.other
- the fraction whose numerator and denominator will become those
of this
fraction.public boolean setSeparator(char newSeparator)
this
fraction. The separator
must not be a letter or a digit.newSeparator
- the new separator.true
if the change was made (i.e. if the passed
parameter is neither a letter nor a digit), and return
false
otherwise.public char getSeparator()
this
fraction.this
fraction.public java.lang.String toString()
this
fraction. Note that
the literal "NaF" (Not A Fraction) is returned if the denominator is zero
to indicate that this is not a valid fraction.toString
in class java.lang.Object
separator
.public java.lang.String toProperString()
this
fraction as a proper fraction. Note that the
literal "NaF" (Not A Fraction) is returned if the denominator is zero to
indicate that this is not a valid fraction.this
fraction in the
form: w n/d
w
is a whole number and n
<
d
. The n/d
part and the space that
precedes it are not present if the numerator of this
fraction is divisible by its denominator. The return is
surrounded by double quotes if isQuoted
is
true
.public boolean equals(java.lang.Object other)
this
fraction is the same as the passed one.
Two fractions are considered equal if they have the same numerator and
denominator (in reduced form).equals
in class java.lang.Object
other
- a reference to the object to test equality with.true
if other
is not null
and points to a fraction that is equal to this
one.
The return is false
otherwise. Note that all
zero-denominator fractions are considered equal.public int hashCode()
this
Fraction.hashCode
in class java.lang.Object
public boolean resembles(Fraction other)
this
fraction resembles the passed one. Two
fractions are said to resemble each other if they have a common
denominator.other
- a reference to the object to test resemblance with.true
if other
is not null
and does resemble this
object. The return is
false
otherwise.public static Fraction getRandom()
This method was added for pedagogical reasons.
public static void setSeed(int seed)
getRandom()
.
This method is meant to be used by a test harness so that the same
sequence can be generated for the app being tested and its oracle.seed
- the initial seed for random number generator.public Fraction cloneMe()
this
object.Fraction
object having exactly
the same state as this
one.public int compareTo(Fraction other)
compareTo
in interface java.lang.Comparable<Fraction>
other
- the Object to compare to.