type.lib
Class MixedNumber

java.lang.Object
  extended by type.lib.Fraction
      extended by type.lib.MixedNumber
All Implemented Interfaces:
java.io.Serializable, java.lang.Comparable<Fraction>
Direct Known Subclasses:
Money

public class MixedNumber
extends Fraction

This class encapsulates a mixed number. A mixed number created by this class has four attributes: a sign s, a whole part w, a proper numerator n, and a proper denominator d; where s is +1 or -1; w, n, d are unsigned integers; and n<d (i.e. the fraction is proper). The corresponding rational value of the mixed number is s*(w+n/d) or the fraction s*(wd+n)/d.

The class derives all its features from its superclass, Fraction, because any mixed number is a (possibly improper) fraction. The case of zero denominator is handled the same as in Fraction (see its API for details). In particular, if a string representation of a zero-denominator mixed number is requested, the literal NaMN (Not a Mixed Number) is returned.

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

Field Summary
 
Fields inherited from class type.lib.Fraction
isQuoted, rng, separator
 
Constructor Summary
MixedNumber()
          Construct a default mixed number with sign +1, whole part equal to 0, numerator equal to zero, and denominator equal to 1.
MixedNumber(int s, long w, long n, long d)
          Construct a mixed number having the passed sign, whole part proper numerator, and proper denominator.
MixedNumber(MixedNumber mixed)
          Construct a copy of the passed mixed number.
 
Method Summary
 long getProperDenominator()
          An accessor to the proper denominator of this mixed number.
 long getProperNumerator()
          An accessor to the proper numerator of this mixed number.
static Fraction getRandom()
          Create a random mixed number with a randomly selected sign and with a whole part, proper numerator, and denominator uniformly distributed in [0,1000) (but no zero-denominator mixed number is generated).
 int getSign()
          Determine and return the sign of this mixed number.
 long getWhole()
          An accessor to the whole part of this mixed number.
 boolean resembles(MixedNumber other)
          Determine if this mixed number resembles the passed one.
 java.lang.String toString()
          Determine and return a string representation of this mixed number.
 
Methods inherited from class type.lib.Fraction
add, cloneMe, compareTo, divide, equals, getDenominator, getNumerator, getSeparator, hashCode, multiply, pow, resembles, setDenominator, setFraction, setFraction, setNumerator, setSeed, setSeparator, subtract, toProperString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

MixedNumber

public MixedNumber()
Construct a default mixed number with sign +1, whole part equal to 0, numerator equal to zero, and denominator equal to 1. The rational value of the constructed mixed number is, thus, zero.


MixedNumber

public MixedNumber(MixedNumber mixed)
Construct a copy of the passed mixed number.

Parameters:
mixed - the mixed number to copy.

MixedNumber

public MixedNumber(int s,
                   long w,
                   long n,
                   long d)
Construct a mixed number having the passed sign, whole part proper numerator, and proper denominator.

Parameters:
s - the sign of the mixed number. Must be +1 or -1.
w - the whole part of the mixed number. Must be non-negative.
n - the proper numerator of the mixed number to construct. Must be non-negative.
d - the proper denominator of the mixed number to construct. Must be non-negative.
Throws:
java.lang.RuntimeException - if any of the passed parameters is not valid as specified above.
Method Detail

getSign

public int getSign()
Determine and return the sign of this mixed number.

Returns:
the sign of this mixed number. The return is +1 is the mixed number is positive or zero, and -1 if the mixed number is negative.

getWhole

public long getWhole()
An accessor to the whole part of this mixed number.

Returns:
the whole part of this mixed number. The return is always non-negative.

getProperNumerator

public long getProperNumerator()
An accessor to the proper numerator of this mixed number.

Returns:
the numerator of this mixed number. The return is always non-negative.

getProperDenominator

public long getProperDenominator()
An accessor to the proper denominator of this mixed number.

Returns:
the denominator of this mixed number. The return is always non-negative.

toString

public java.lang.String toString()
Determine and return a string representation of this mixed number.

Overrides:
toString in class Fraction
Returns:
the string "+" or "-" depending on the sign of this mixed number, followed by a space, followed by its whole part, followed by a space, followed by the proper numerator and poper denominator separated by a slash. Note however that if the proper denominator is zero, the literal NaMN is returned.

resembles

public boolean resembles(MixedNumber other)
Determine if this mixed number resembles the passed one. Two mixed numbers are said to resemble each other if they have the same whole part.

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 Fraction getRandom()
Create a random mixed number with a randomly selected sign and with a whole part, proper numerator, and denominator uniformly distributed in [0,1000) (but no zero-denominator mixed number is generated). The return is a MixedNumber instance 50% of the times. In the remaining 50%, the return is supplied by the getRandom method of the Fraction superclass.

This method was added for pedagogical reasons.

Returns:
a randomly chosen fraction.


Java by Abstraction: A Client-View Approach