Lab 6

15 points

1. You will create a class Money that will represent a dollar amount to be used for storing bank account balances. There are a number of issues to be considered before you can begin the implementation. these include:

  1. Which class should Money inherit from: Object, Magnitude, Number, Float, or some other class? Look at the implementation of Date and Time. Should Money be implemented in a similarly way?
  2. Should we store the currency amount as one unit, or more than one? for example, should we store a dollar amount as simply cents (an integer), as dollars and cents (as two integers ), ore as dollars (as a float). This means that $364 could be stored as 364 cents, 3 dollars and 64 cents, ore 3.64 dollars respectively. Which is easiest to use when we write arithmetic operations for Money such as addition and subtraction? Which is best if we want to retain and exact number of cents without roundoff errors?

We will implement Money as a subclass of Magnitude which contains an integer number of coins.

2. Define the class Money with appropriate instance variables(s). the first thing to considered is how to create instances of the class. "Money new" is probably not an appropriate message to use. It may be more appropriate to have the following 3 class methods:

Money dollars: 3		(returns a Money object containing 300 cents)
Money cents: 54		(returns a Money object containing 54 cents)
Money dollars: 3 cents: 54	(returns a Money object containing 354 cents)

In addition, it seems reasonable to add a method asMoney to allow numbers to convert themselves to a dollar amount. For example, you may wish to add:

35 asMoney		(should return a Money object containing 3500 cents)
35.25 asMoney	(should return a Money object containing 3525 cents)

3. It may be appropriate to provide methods for accessing the entire amount as a number, for accessing only the dollars portion, and for accessing the cents part. Should there be corresponding modification methods? Before you come up with an answer, consider whether or not you can change other magnitudes such as dates and times. With integers, for example you can't change an integer 200 into 230 by changing the middle digit. You have to get an entirely different integer.

Provide only those accessing and modification methods that are appropriate.

4. Two instances of the class Money should know whether or not they are equal. define a method = which returns true if corresponding amounts are the same, and false otherwise.

Be careful here. Should the following be an error?
(Money cents: 254) = 'hello'
Can you compare different kinds of objects using =?
	5 = 'hi'

5. In addition to using = you should also be able to use relational operations with instances of Money. That is, you should be able to use the methods <, >, <=, >=, min:, max:, between:and:, etc. Try them out. Add any additional methods that are required to make them work.

6. It is about time for us to clean up the way instances of this class display themselves. When you display money, it should look something like "$3.54" - not "aMoney". Write a printOn: method for Money. Try printing each of the following using "Show it" menu item:

Money cents: 154		Money cents: 70
Money cents: 200		Money cents: 3
Money cents: 220		Money cents: 0

7. Implement the operations +, -, *, and /. Consider the semantics of each of them separately. Note that it makes sense to add to money objects. Does it make sense to multiply tow of them; e.g., does $254 * $3.32 make sense; does $2.54 * 3 make sense?