Question
Here is the UML class diagram for the Money class.
Money
- dollars: int
- cents: int
+ setMoney (int, int)
+ add(Money):void
+subtract(Money): void
+ equals(Money): boolean
+ lessThan(Money): boolean
+ toString():String
Instance data: There are two integer values for the instance data representing the dollars and cents. These must be declared as private.
Instance methods: There are six methods in the class. All methods are public.
1. setMoney(int dollarsIn, int centsIn) –Set the dollars and cents to the values of the parameters. If either input is negative, set the dollars and cents to 0.
2. add(Money moneyIn) – add the dollars and cents of the parameter to the current object. If the cents exceeds 100, adjust the dollars and cents accordingly.
3. subtract(Money moneyIn) – subtract the parameter from the current object If the cents fall below 0, then the dollars and cents must be adjusted. If the number of dollars falls below 0, then set both the dollars and cents to 0
4. boolean equals(Money moneyIn): return true if both the dollars and cents of the parameter match the dollars and cents of the current object, otherwise return false
5. boolean lessThan(Money moneyIn): return true if the current object represents less money than the parameter. otherwise return false
6. String toString(): return the values of the object as a String formatted as $dd.cc
Documentation: Use Javadoc style for all comments.
Solution Preview
This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.
/** Class: Money
* Desc: Class defines money in terms of dollars and cents. It can set a value, add, subtract, check for equivalence, or if it is less than another money value
* It also prints out the amount of money in the correct format of dollars and cents
*/
public class Money {
private int dollars, cents;
/**
* @param dollars
* @param cents
*/
public void setMoney(int dollars, int cents)
{
this.dollars = dollars;
this.cents = cents;
}
/**
* Adds money to current value, based on parameter sent in
* @param money
*/
public void add(Money money)
{
dollars += money.dollars;
dollars += (cents + money.cents)/100;
cents = (cents + money.cents)%100;
}
/**
* Subtracts parameter sent in from current object
* @param money
*/
public void subtract(Money money)
{...