

PREV CLASS NEXT CLASS  FRAMES NO FRAMES  
SUMMARY: NESTED  FIELD  CONSTR  METHOD  DETAIL: FIELD  CONSTR  METHOD 
java.lang.Object type.lib.Fraction
public class Fraction
This class encapsulates a fraction. It maintains the fraction's state
(numerator and denominator) and provides functionality for manipulating
fractions. The class ensures that the denominators of all its instances are
nonnegative at all times by transferring any negative denominator sign to
the numerator. Furthermore, the fraction is kept in reduced form at all
times, i.e. with no common divisor greater than 1. 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
.
FractionNS
class).
Field Summary  

static boolean 
isQuoted
A flag that determines if the return of the toProperString()
method is surrounded by quotes or not. 
protected static java.util.Random 
rng
The random number generator used by the getRandom() method. 
char 
separator
A character that separates the numerator denominator pair in the return of the toString() method. 
Constructor Summary  

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(long numerator,
long denominator)
Construct a fraction with the passed numerator and denominator and a '/' separator. 

Fraction(long numerator,
long denominator,
char separator)
Construct a fraction with the passed numerator, denominator, and separator. 
Method Summary  

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. 
long 
getDenominator()
An accessor to the denominator of this fraction. 
long 
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(long denominator)
A mutator for the denominator of this fraction. 
void 
setFraction(Fraction other)
A mutator for this fraction. 
void 
setFraction(long numerator,
long denominator)
A mutator for this fraction. 
void 
setNumerator(long numerator)
A mutator of the numerator of this fraction. 
static void 
setSeed(long 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. 
Methods inherited from class java.lang.Object 

clone, finalize, getClass, notify, notifyAll, wait, wait, wait 
Field Detail 

public char separator
toString()
method. The default value is '/'
.
(Bad designshould 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 designshould 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)
.)
protected static java.util.Random rng
getRandom()
method.
Constructor Detail 

public Fraction()
public Fraction(long numerator, long 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(long numerator, long 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.Method Detail 

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
nonnegative).
java.lang.RuntimeException
 if the exponent is negative.public void setNumerator(long numerator)
this
fraction.
numerator
 the new numerator.public long getNumerator()
this
fraction.
this
fraction.public void setDenominator(long denominator)
this
fraction.
denominator
 the new denominator.public long getDenominator()
this
fraction.
this
fraction.public void setFraction(long numerator, long 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
zerodenominator 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(long 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.


PREV CLASS NEXT CLASS  FRAMES NO FRAMES  
SUMMARY: NESTED  FIELD  CONSTR  METHOD  DETAIL: FIELD  CONSTR  METHOD 