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|
Construct a default money amount of +1 dollar and zero cents.
Construct a money amount having the passed real number value.
Construct a money amount having the passed sign, dollar amount, and cent amount.
Construct a copy of the passed Money object.
|Modifier and Type||Method and Description|
An accessor to the cent amount of
An accessor to the dollar amount of
Create a random fraction.
Determine and return a string representation of
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(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()
public int getCent()
public java.lang.String toString()
(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
public boolean resembles(Money other)
thismoney 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.
nulland does resemble
thisobject. The return is
public static Fraction getRandom()
Moneyinstance 50% of the times. In the remaining 50%, the return is supplied by the
getRandommethod of the
MixedNumbersuperclass; i.e. the return is an instance of
Moneywith a probability of 0.5, of
MixedNumberwith a probability of 0.25, and of
Fractionwith a probability of 0.25. The
Moneyinstances are uniformly distributed in [1000,3000).
This method was added for pedagogical reasons.