Question: Money - dollars: int - cents: int + add(Money):void + subtract(Money): void + compareTo(Money): int + equals(Money):boolean + toString():String + Money (int, int) + Money(Money)
| Money |
| - dollars: int - cents: int |
| + add(Money):void + subtract(Money): void + compareTo(Money): int + equals(Money):boolean + toString():String + Money (int, int) + Money(Money) - adjust () void |
Here is the UML class diagram for the Money class. Convert this UML into code using the following below. Test this out in MoneyTest.java
Class description 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 eight methods in the class. All methods are public accept the adjust method. DO NOT add any additional methods.
Constructors
Money (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. Invoke the adjust method to make sure that the dollars and cents are valid.
Money (Money other) Set the dollars and cents to the values in the parameter.
Mutators
add(Money moneyIn) add the dollars and cents of the parameter to the current object. Invoke the adjust method to make sure that the dollars and cents are valid.
subtract(Money moneyIn) subtract the parameter from the current object. Invoke the adjust method to make sure that the dollars and cents are valid.
adjust() make sure that the cents are from 0..99 borrowing from or adding to the dollars as needed. If both the cents and dollars are negative, set the values to 0.
Accessors
int compareTo (Money moneyIn) return a value < 0 if the current object is less than the parameter, return 0 if the current object equals the parameter and a value > 0 if the current object is greater than the parameter
boolean equals(Money moneyIn): return true if the current matches the parameter. otherwise return false
String toString(): return the values of the object as a String formatted as $d.cc
Testing:
To test your program, you will need to do the following.
1. Create the file MoneyTest.java in the same package 2. Copy the file posted on Blackboard into this file 3. Run the tests by selecting the run button
4. When all the tests are successful, you will get a green bar. A red bar means that the tests have not been successful. You may find it useful to comment out some of the tests and then add them one at a time to help debug your code.
Documentation: Use Javadoc style for all comments.
Document the beginning of the file with your name and program number as well as a description of the class.
Document each method.
Submit your file Money.java
// MoneyTest.java used to test code. package assign2; import static org.junit.Assert.*; import org.junit.Test; public class MoneyTest { @Test public void testMonwy1() { Money moneyObj = new Money(7, 33); assertEquals ("$7.33", moneyObj.toString()); moneyObj = new Money (3, 55); assertEquals ("$3.55", moneyObj.toString ()); } @Test public void testMoney2() { Money moneyObj = new Money(7, 33); Money moneyObj2 = new Money (moneyObj); assertEquals ("$7.33", moneyObj2.toString()); moneyObj2 = new Money (new Money (10, 10)); assertEquals ("$10.10", moneyObj2.toString()); } @Test public void testToString() { Money moneyObj = new Money(7, 0); assertEquals ("$7.00", moneyObj.toString()); moneyObj = new Money(0, 7); assertEquals ("$0.07", moneyObj.toString()); moneyObj = new Money(0, 0); assertEquals ("$0.00", moneyObj.toString()); } @Test public void testEquals () { Money moneyObj = new Money(7, 33); Money moneyObj2 = new Money (moneyObj); assertTrue (moneyObj.equals(moneyObj)); assertTrue (moneyObj.equals(moneyObj2)); assertTrue (moneyObj2.equals(moneyObj)); moneyObj2 = new Money (7, 34); assertFalse (moneyObj.equals(moneyObj2)); moneyObj2 = new Money (8, 33); assertFalse (moneyObj.equals(moneyObj2)); } @Test public void testCompareTo () { Money moneyObj = new Money(7, 33); Money moneyObj2 = new Money (moneyObj); assertEquals (0, moneyObj.compareTo(moneyObj)); assertEquals (0, moneyObj2.compareTo(moneyObj)); assertEquals (0, moneyObj.compareTo(moneyObj2)); moneyObj2 = new Money (8, 33); assertTrue (moneyObj.compareTo(moneyObj2) < 0); assertTrue (moneyObj2.compareTo(moneyObj) > 0); moneyObj2 = new Money (7, 34); assertTrue (moneyObj.compareTo(moneyObj2) < 0); assertTrue (moneyObj2.compareTo(moneyObj) > 0); } @Test public void testAdd1 () { Money money1 = new Money(0, 0); Money money2 = new Money(1, 22); money1.add(money2);; assertEquals ("$1.22", money1.toString()); money1.add(money1);; assertEquals ("$2.44", money1.toString()); } @Test public void testAdd2 () { Money money1 = new Money(10, 50); Money money2 = new Money(10, 60); money1.add(money2);; assertEquals ("$21.10", money1.toString()); money2 = new Money(10, 90); money1.add(money2);; assertEquals ("$32.00", money1.toString()); } @Test public void testSub1 () { Money money1 = new Money(10, 50); Money money2 = new Money(1, 22); money1.subtract(money2); assertEquals ("$9.28", money1.toString()); money1.subtract(new Money (4, 3)); assertEquals ("$5.25", money1.toString()); money1.subtract(money1); assertEquals ("$0.00", money1.toString()); } @Test public void testSub2 () { Money money1 = new Money(10, 50); Money money2 = new Money(11, 22); money2.subtract(money1); assertEquals ("$0.72", money2.toString()); money2.subtract(new Money (4, 3)); assertEquals ("$0.00", money2.toString()); money2.subtract(money1); assertEquals ("$0.00", money2.toString()); } @Test public void testMonwy3() { Money moneyObj = new Money(7, 333); assertEquals ("$10.33", moneyObj.toString()); moneyObj = new Money (3, -255); assertEquals ("$0.45", moneyObj.toString ()); moneyObj = new Money (-3, -155); assertEquals ("$0.00", moneyObj.toString ()); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
