type.lib
Class FractionNS

java.lang.Object
  extended by type.lib.FractionNS
All Implemented Interfaces:
java.io.Serializable, java.lang.Comparable<FractionNS>

public class FractionNS
extends java.lang.Object
implements java.io.Serializable, java.lang.Comparable<FractionNS>

This class is identical to the Fraction class except for three non-standard (NS) features. For pedagogical reasons, neither toString nor equals nor hashCode is present.

Version:
7.0 - Summer 2010
Author:
H. Roumani, roumani@cse.yorku.ca
See Also:
Serialized Form

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 Object.toString() method.
 
Constructor Summary
FractionNS()
          Construct a default fraction with numerator equal to 0, denominator equal to 1, and separator equal to '/'.
FractionNS(FractionNS fraction)
          Construct a copy of the passed Fraction.
FractionNS(long numerator, long denominator)
          Construct a fraction with the passed numerator and denominator and a '/' separator.
FractionNS(long numerator, long denominator, char separator)
          Construct a fraction with the passed numerator, denominator, and separator.
 
Method Summary
 void add(FractionNS other)
          Add the passed fraction to the fraction on which it was called.
 FractionNS cloneMe()
          Copy the state of this object.
 int compareTo(FractionNS other)
          Compare this object with the specified object for order.
 void divide(FractionNS other)
          Divide the fraction on which the method was called by the passed fraction.
 long getDenominator()
          An accessor to the denominator of this fraction.
 long getNumerator()
          An accessor to the numerator of this fraction.
static FractionNS getRandom()
          Create a random fraction.
 char getSeparator()
          An accessor to the separator of this fraction.
 void multiply(FractionNS 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(FractionNS other)
          Determine if this fraction resembles the passed one.
 void setDenominator(long denominator)
          A mutator for the denominator of this fraction.
 void setFraction(FractionNS 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(FractionNS other)
          Subtract the passed fraction from the fraction on which it was called.
 java.lang.String toProperString()
          Return this fraction as a proper fraction.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

separator

public char separator
A character that separates the numerator denominator pair in the return of the Object.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.)


isQuoted

public static boolean isQuoted
A flag that determines if the return of the 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 accessor, e.g. isQuoted and a public mutator, e.g. setQuoted.)


rng

protected static java.util.Random rng
The random number generator used by the getRandom() method.

Constructor Detail

FractionNS

public FractionNS()
Construct a default fraction with numerator equal to 0, denominator equal to 1, and separator equal to '/'. The rational value of the constructed fraction is, thus, zero.


FractionNS

public FractionNS(long numerator,
                  long denominator)
Construct a fraction with the passed numerator and denominator and a '/' separator. If the passed denominator is negative, the sign of the numerator is reversed in order to keep the denominator positive.

Parameters:
numerator - the numerator of the fraction to construct.
denominator - the denominator of the fraction to construct.

FractionNS

public FractionNS(FractionNS fraction)
Construct a copy of the passed Fraction.

Parameters:
fraction - the Fraction to copy.

FractionNS

public FractionNS(long numerator,
                  long denominator,
                  char separator)
Construct a fraction with the passed numerator, denominator, and separator. If the passed denominator is negative, the sign of the numerator is reversed in order to keep the denominator positive.

Parameters:
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

add

public void add(FractionNS other)
Add the passed fraction to the fraction on which it was called. This method is, in effect, a mutator because it changes the state of the encapsulated object.

Parameters:
other - the fraction to add to this fraction.

subtract

public void subtract(FractionNS other)
Subtract the passed fraction from the fraction on which it was called. This method is, in effect, a mutator because it changes the state of the encapsulated object.

Parameters:
other - the fraction to subtract from this fraction.

multiply

public void multiply(FractionNS other)
Multiply the fraction on which the method was called by the passed fraction. This method is, in effect, a mutator because it changes the state of the encapsulated object.

Parameters:
other - the fraction to multiply this fraction by

divide

public void divide(FractionNS other)
Divide the fraction on which the method was called by the passed fraction. This method is, in effect, a mutator because it changes the state of the encapsulated object.

Parameters:
other - the fraction to divide this fraction by

pow

public void pow(int exponent)
Raise the fraction on which the method was called to the passed exponent. This method is, in effect, a mutator because it changes the state of the encapsulated object.

Parameters:
exponent - the exponent to raise this fraction to (must be non-negative).
Throws:
java.lang.RuntimeException - if the exponent is negative.

setNumerator

public void setNumerator(long numerator)
A mutator of the numerator of this fraction.

Parameters:
numerator - the new numerator.

getNumerator

public long getNumerator()
An accessor to the numerator of this fraction.

Returns:
the numerator of this fraction.

setDenominator

public void setDenominator(long denominator)
A mutator for the denominator of this fraction.

Parameters:
denominator - the new denominator.

getDenominator

public long getDenominator()
An accessor to the denominator of this fraction.

Returns:
the denominator of this fraction.

setFraction

public void setFraction(long numerator,
                        long denominator)
A mutator for this fraction.

Parameters:
numerator - the new numerator of the fraction.
denominator - the new denominator of the fraction.

setFraction

public void setFraction(FractionNS other)
A mutator for this fraction.

Parameters:
other - the fraction whose numerator and denominator will become those of this fraction.

setSeparator

public boolean setSeparator(char newSeparator)
A mutator of the separator of this fraction. The separator must not be a letter or a digit.

Parameters:
newSeparator - the new separator.
Returns:
true if the change was made (i.e. if the passed parameter is neither a letter nor a digit), and return false otherwise.

getSeparator

public char getSeparator()
An accessor to the separator of this fraction.

Returns:
the separator of this fraction.

toProperString

public java.lang.String toProperString()
Return 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.

Returns:
a string representation of of this fraction in the form:
w n/d
where w is a whole number and n < d. The return is surrounded by double quotes if isQuoted is true.

resembles

public boolean resembles(FractionNS other)
Determine if this fraction resembles the passed one. Two fractions are said to resemble each other if they have a common denominator.

Parameters:
other - a reference to the object to test resemblance with.
Returns:
true if other is not null and does resemble this object. The return is false otherwise.

getRandom

public static FractionNS getRandom()
Create a random fraction. The numerators and denominators of the created fractions are uniformly distributed in [0,1000) but no zero-denominator fraction is generated.

This method was added for pedagogical reasons.

Returns:
a randomly chosen fraction.

setSeed

public static void setSeed(long seed)
Change the seed of the random sequence returned by 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.

Parameters:
seed - the initial seed for random number generator.

cloneMe

public FractionNS cloneMe()
Copy the state of this object.

Returns:
a reference to a new Fraction object having exactly the same state as this one.

compareTo

public int compareTo(FractionNS other)
Compare this object with the specified object for order. Return a negative integer, zero, or a positive integer if this object is less than, equal to, or greater than the specified object.

Specified by:
compareTo in interface java.lang.Comparable<FractionNS>
Parameters:
other - the Object to compare to.
Returns:
-1 if this fraction is less than the passed one, +1 if it is greater, and 0 if they are equal. Note that all zero-denominator fractions are considered equal and greater than any other fraction.


Java by Abstraction: A Client-View Approach