public class Money extends Object
The Money
class keeps track of two values:
Each Money
object keeps track of two values:
this
money objectthis
money objectConstructor and Description |
---|
Money()
Construct a money object having 0 dollars and 0 cents.
|
Money(int dollars,
int cents)
Construct a money object given the amount of dollars and amount of cents.
|
Modifier and Type | Method and Description |
---|---|
int |
getCents()
Get the amount of cents represented by this object
(ignoring the number of dollars).
|
static int |
getCentsInCirculation()
Get the total amount of cents in circulation.
|
int |
getDollars()
Get the amount of dollars represented by this object.
|
static int |
getDollarsInCirculation()
Get the total amount of dollars in circulation.
|
boolean |
setCents(int cents)
Set the amount of cents.
|
boolean |
setDollars(int dollars)
Set the amount of dollars.
|
String |
toString()
Creates a string representation of a money object.
|
public Money()
public Money(int dollars, int cents)
IllegalArgumentException
- if dollars < 0 or cents < 0.dollars
- The amount of dollars.cents
- The amount of cents.public boolean setDollars(int dollars)
dollars < 0
then the
state of the object is unchanged from its current value and false
is returned. If dollars >= 0
then the state of the object is
updated, the total amount of money in circulation is updated,
and true is returned.
The total amount of money in circulation is updated with the
change in the value of dollars for this money object. For example,
suppose the number of dollars for a money object m
is 10; then m.setDollars(15)
will increase the total amount of
money in circulation by 5 dollars.
dollars
- The amount of dollars.public boolean setCents(int cents)
cents < 0
then the
state of the object is unchanged from its current value and false
is returned. If cents >= 0
then the state of the object is
updated, the total amount of money in circulation is updated,
and true is returned.
The total amount of money in circulation is updated with the
change in the value of cents for this money object. For example,
suppose the number of cents for a money object m
is 50; then m.setCents(30)
will decrease the total amount of
money in circulation by 20 cents.
cents
- The amount of cents.public int getDollars()
public int getCents()
public static int getDollarsInCirculation()
public static int getCentsInCirculation()