public class Money extends MixedNumber
The class derives all its features from its superclass, MixedNumber, because any amount of money is a mixed number having the same sign, a whole part equal to the dollar amount, a proper numerator equal to the cent amount, and a proper denominator of 100.
The case of division by zero is handled the same as in the superclass.
In particular, if a string representation of an amount with a
zero-denominator is requested, the literal
NaM
(Not a Money) is returned.
Constructor and Description |
---|
Money()
Construct a default money amount of +1 dollar and zero cents.
|
Money(double m)
Construct a money amount having the passed real number
value.
|
Money(int s,
int d,
int c)
Construct a money amount having the passed sign, dollar amount,
and cent amount.
|
Money(Money money)
Construct a copy of the passed Money object.
|
Modifier and Type | Method and Description |
---|---|
int |
getCent()
An accessor to the cent amount of
this money amount. |
int |
getDollar()
An accessor to the dollar amount of
this money amount. |
static Fraction |
getRandom()
Create a random fraction.
|
boolean |
resembles(Money other)
Determine if
this money object resembles
the passed one. |
java.lang.String |
toString()
Determine and return a string representation of
this
money amount. |
getProperDenominator, getProperNumerator, getSign, getWhole, resembles
add, cloneMe, compareTo, divide, equals, getDenominator, getNumerator, getSeparator, hashCode, multiply, pow, resembles, setDenominator, setFraction, setFraction, setNumerator, setSeed, setSeparator, subtract, toProperString
public Money()
public Money(Money money)
money
- the Money object to copy.public Money(double m)
IO.format(java.lang.String, java.lang.String)
and the dollar and cent parts are then
extracted.m
- the real value of the money amount to construct.public Money(int s, int d, int c)
s
- the sign of the money amount.
Must be +1 or -1.d
- the dollar amount.
Must be non-negative.c
- the cent amount.
Must be non-negative.java.lang.RuntimeException
- if any of the passed parameters is not valid as specified above.public int getDollar()
this
money amount.this
money amount.public int getCent()
this
money amount.this
money amount.public java.lang.String toString()
this
money amount.toString
in class MixedNumber
(minus) $
",
otherwise it begins with "$
". This is followed by the dollar
amount (with thousand separators) followed by " dollars and
",
and then the cent amount followed by " cents.
".
Note however that if the amount involves division by zero, the literal
NaM
is returned.public boolean resembles(Money other)
this
money object resembles
the passed one. Two money objects are said to resemble
each other if their cent amounts can be expressed using the
same number of quarters (25-cent coin). In other words, the
quotient of dividing the cent amount by 25 should be the
same for both.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()
Money
instance
50% of the times. In the remaining 50%, the return is supplied by the
getRandom
method of the MixedNumber
superclass;
i.e. the return is an instance of Money
with a probability of 0.5, of MixedNumber
with a probability of 0.25, and of Fraction
with a probability of 0.25.
The Money
instances are uniformly distributed in [1000,3000).
This method was added for pedagogical reasons.