Question
2. Stub out USMoney.java so that all fields and methods correspond to the following UML. Pay close attention to which members are public which are
2. Stub out USMoney.java so that all fields and methods correspond to the following UML. Pay close attention to which members are public which are private, which are static and which are not.
3. Complete the implementation of all methods. You may use USMoneyTest.java to test locally, or you may create your own driver class. Checkstyle will not be run on your submission but you could lose points if there are serious style problems.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
USMoney.java
/** * a class to represent US Money values * * @author - YOUR NAME * @version - 1.0 - DATE */ public class USMoney { // FIELDS HERE /** * Default constructor, sets this USMoney to $0.00 */ // METHOD HERE
/** * Explicit value constructor. If the cents are > 99 adjust the dollars and * cents accordingly (ie. 101 cents would be 1 dollar and 1 cent. * * If either of the values is
/** * the adjust method adjusts this object's dollars and cents based on the * number of cents. If cents is greater than 99, it will increase the dollars * and reduce the cents accordingly. HINT: Work out some examples first. */ // METHOD HERE
/** * getDollars returns only the number of dollars associated with this * USMoney object. For example if the object represents $23.15, this * method should return 23. * * @return The number of dollars */ // METHOD HERE /** * getCents returns only the number of cents associated with this USMoney * object. For example if the object represents $23.15, this method * should return 15. * * @return The number of cents */ // METHOD HERE
/** * getTotalCents gets the total number of cents in this USMoney object. For * example, 3 dollars and 5 cents would return 305 * * @return The number of cents in this money object. */ // METHOD HERE
/** * compare compares this USMoney object to another USMoney object. It returns * 1 if this is > than other, 0 if they are the same, -1 if this is less than * other. * * @param other The USMoney object to compare to this USMoney * @return 1 if this > other, 0 if this == other, -1 if this
/** * add creates and returns a new USMoney object that is equal to the sum * of this USMoney object and the provided USMoney object. * * Neither this object nor the other object should be changed. * * For example if this USMoney represented $3.51 and the other object * represented $.60, the returned object would represent $4.11. * * The returned USMoney object should be properly adjusted. I.e. the number * of cents must be less than 100. * * @param other The other money object to add * * @return A new USMoney object that is the sum of both objects. */ // METHOD HERE
/** * toString returns a String representation of this USMoney object (It is * provided here to use in testing.) * * @return A String representing this USMoney object */ public String toString() { return String.format("$%d.%d", dollars, cents); }
/** * countMoney returns the sum of all USMoney objects in the provided array as * a USMoney object. * * If the array is empty, countMoney should return a USMoney object * representing $0.00. You may assume that the array will not be null. * * * The returned USMoney object should be properly adjusted. I.e. the number * of cents must be less than 100. * * * @param allMoney An array of USMoney objects. * @return A USMoney object representing the sum of all provided money. */ // METHOD HERE }
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
USMoneyTest.java
import static org.junit.Assert.*;
import org.junit.Before; import org.junit.Test;
/** * JUnit tests for the practice coding exam. * * @author * @version */ public class USMoneyTest {
@Before public void setUp() throws Exception { }
@Test public void testZeroArgConstructor() { USMoney money = new USMoney(); assertEquals(0, money.getTotalCents()); } @Test public void testTwoArgConstructorNegativeDollars() { USMoney money = new USMoney(-1, 10); assertEquals(00, money.getDollars()); } @Test public void testTwoArgConstructorNegativeCents() { USMoney money = new USMoney(1, -10); assertEquals(0, money.getCents()); } @Test public void testTwoArgConstructorNoAdjustment() { USMoney money = new USMoney(2, 10); assertEquals(2, money.getDollars()); assertEquals(10, money.getCents()); } @Test public void testTwoArgConstructorNeedsAdjusting() { USMoney money1 = new USMoney(2, 100); USMoney money2 = new USMoney(2, 250); assertEquals(3, money1.getDollars()); assertEquals(0, money1.getCents()); assertEquals(4, money2.getDollars()); assertEquals(50, money2.getCents()); } @Test public void testGetTotalCentsCentsOnly() { USMoney money = new USMoney(0, 10); assertEquals(10, money.getTotalCents()); } @Test public void testGetTotalCentsDollarsOnly() { USMoney money = new USMoney(2, 0); assertEquals(200, money.getTotalCents()); } @Test public void testGetTotalCentsDollarsAndCents() { USMoney money = new USMoney(2, 10); assertEquals(210, money.getTotalCents()); } @Test public void testComareToDollarsOnly() { USMoney money1 = new USMoney(2, 0); USMoney money2 = new USMoney(3, 0); assertEquals(0, money1.compareTo(money1)); assertEquals(-1, money1.compareTo(money2)); assertEquals(1, money2.compareTo(money1)); } @Test public void testComareToCentsOnly() { USMoney money1 = new USMoney(0, 2); USMoney money2 = new USMoney(0, 3); assertEquals(0, money1.compareTo(money1)); assertEquals(-1, money1.compareTo(money2)); assertEquals(1, money2.compareTo(money1)); } @Test public void testComareToDollarsAndCents() { USMoney money1 = new USMoney(2, 10); USMoney money2 = new USMoney(1, 205); assertEquals(0, money1.compareTo(money1)); assertEquals(-1, money1.compareTo(money2)); assertEquals(1, money2.compareTo(money1)); } @Test public void testAddNoAdjustNeeded() { USMoney money1 = new USMoney(2, 10); USMoney money2 = new USMoney(1, 20); USMoney result1 = money1.add(money2); USMoney result2 = money2.add(money1); assertEquals(3, result1.getDollars()); assertEquals(30, result1.getCents()); assertEquals(3, result2.getDollars()); assertEquals(30, result2.getCents()); } @Test public void testAddAdjustNeeded() { USMoney money1 = new USMoney(2, 80); USMoney money2 = new USMoney(1, 30); USMoney result1 = money1.add(money2); USMoney result2 = money2.add(money1); assertEquals(4, result1.getDollars()); assertEquals(10, result1.getCents()); assertEquals(4, result2.getDollars()); assertEquals(10, result2.getCents()); } @Test public void testCountMoneyEmpty() { USMoney[] allMoney = new USMoney[0]; USMoney result = USMoney.countMoney(allMoney); assertEquals(0, result.getDollars()); assertEquals(0, result.getCents()); } @Test public void testCountMoneyNonEmpty() { USMoney[] allMoney = new USMoney[3]; allMoney[0] = new USMoney(1, 80); allMoney[1] = new USMoney(0, 30); allMoney[2] = new USMoney(2, 00); USMoney result = USMoney.countMoney(allMoney); assertEquals(4, result.getDollars()); assertEquals(10, result.getCents()); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started