Computer Science 221 Program 3

Learning objectives:

  • Write a Java class that takes an object parameter.

  • Write a Java class that modifies "this" object.

  • Write a Java class that creates and returns an object.



Define a class named Money whose objects
represent amounts in U. S. money. The class must have two instance variables of
type int for the dollars
and cents parts of the amount of money. You may not use any float or double variables or values in this


Write two constructors. One has two parameters for the dollars and
cents. The other has no parameters, and set dollars and cents to zero. Write
accessors and mutators (getters and setters) for the dollar amount and the
cents amount.


Write two methods, increment(Money
and decrement(Money
that change the amount in the calling object (this) by the amount in
parameter. So if m1 holds 2 dollars
90 cents and m2 holds 1 dollar
20 cents, the call m1.increment(m2) changes the
amount in m1 to 4 dollars 10
cents. Write two similar methods, add(Money m) and subtract(Money
that add or subtract, respectively, the amounts in the calling object (this) and the parameter and
return a third Money object that
contains the sum or difference. So if m1 holds 2 dollars 90 cents and m2 holds 1 dollar 20 cents,
the call m1.add(m2) does not change
either m1 or m2. Rather a third Money object, containing 4
dollars 10 cents, is created and returned.


You do not have to write a subtract(Money m) method or a decrement(Money m) method that can
subtract a larger amount from a smaller amount (negative result). You will get
extra credit if you do write methods that can correctly produce negative


Write a method, toString(), that returns a String containing the amount of
money in a pleasing format. So if m1 holds 2 dollars 90 cents, toString() will return the String "$2.90".


Write a method, equals(Money
that returns a boolean, true or false as this object is equal to the


Write a program TestMoney to test your Money class. We reserve the right
to replace your TestMoney with our own.


Submission instructions:

Upload the files and to “Program 3”. Do not submit class files or any of the BlueJ
control files. Be sure to press the submit button. 



The policies given on WebCT are in effect
for this and all assignments.


You may submit on WebCT multiple times, so
there is no excuse for not submitting partial solutions.

Powered by