public class MixedNumber extends Fraction
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.
Constructor and Description |
---|
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,
int w,
int n,
int 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.
|
Modifier and Type | Method and Description |
---|---|
int |
getProperDenominator()
An accessor to the proper denominator of
this mixed number. |
int |
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. |
int |
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. |
add, cloneMe, compareTo, divide, equals, getDenominator, getNumerator, getSeparator, hashCode, multiply, pow, resembles, setDenominator, setFraction, setFraction, setNumerator, setSeed, setSeparator, subtract, toProperString
public MixedNumber()
public MixedNumber(MixedNumber mixed)
mixed
- the mixed number to copy.public MixedNumber(int s, int w, int n, int d)
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.java.lang.RuntimeException
- if any of the passed parameters is not valid as specified above.public int getSign()
this
mixed number.this
mixed number.
The return is +1 is the mixed number is positive or zero, and
-1 if the mixed number is negative.public int getWhole()
this
mixed number.this
mixed number.
The return is always non-negative.public int getProperNumerator()
this
mixed number.this
mixed number.
The return is always non-negative.public int getProperDenominator()
this
mixed number.this
mixed number.
The return is always non-negative.public java.lang.String toString()
this
mixed number.toString
in class Fraction
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.public boolean resembles(MixedNumber other)
this
mixed number resembles
the passed one. Two mixed numbers are said to resemble
each other if they have the same whole part.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()
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.