Class Money The Money class is used to track a USD amount co
Class Money
The Money class is used to track a USD amount consisting of two integers to manage dollars and cents. All amounts will be positive or 0. We should never return any cents GT 99. The data and methods below will be called from the test driver I’ll use, so be sure to adhere to the names of the functions, as well as the number, order, and type of arguments handed to each function, as indicated below.
If the value of cents is ever greater than 99, you will add to the dollar amount the appropriate value. IE if the user calls setMoney( 3, 550) this is equivalent to a call of setMoney(8,50);
Money Methods:
•Money ( int dol )
Constructor which sets the dollar amount, and sets cents to 0
If the user enters in an amount LT 0, you will throw an IllegalArgumentException
•Money( int dol, int cent)
Constructor which initialized dollars and cents.
If the user enters in an amount LT 0, you will throw an IllegalArgumentException
•Money( Money other)
Copy Constructor
•int getDollars()
•int getCents()
•void setMoney( int dollars, int cents)
If the user enters in an amount LT 0, you will throw an IllegalArgumentException
•double getMoney( )
Gets the money amount as a double
For example it might return 5.75
•void add( int dollars);
If the user enters in an amount LT 0, you will throw an IllegalArgumentException
•void add ( int dollars, int cents);
If the user enters in an amount LT 0, you will throw an IllegalArgumentException
•void add( Money other)
Adds the amounts in other to our money object – reducing cents appropriately.
•Boolean equals ( Object o)
Two money objects are the same if they have the same value for dollars and cents.
•String toString()
Prints out the amount as a string IE “$3.75” or “$4.00” Note the number of digits displayed for cents.
Again for testing and grading purposes use this EXACT output format
------------------DRIVER---------------------
@Test
public void testMoneytDolCentConstructor()
 {
 Money m = new Money(5, 120);
 assertEquals( 6, m.getDollars());
 assertEquals( 20, m.getCents());
 }
   
@Test
 public void testMoneyInvalidInput()
 {
 try {
 Money m = new Money(-1,0);
 // If we get here we did not throw exception
 assertTrue( false);
 }
 catch ( IllegalArgumentException e )
 {
 // Caught the illegal argument
 assertTrue(true);
 }
 }
Solution
Hi, Please find my implementtion.
Please let me know in case of any issue.
I have implemented all the functionality.
public class Money {
// instance variables
private int dollars;
private int cents;
// constructors
public Money ( int dol ){
if( dol < 0){
throw new IllegalArgumentException(\"dollar/cent value can not be < 0\");
}
dollars = dol;
cents = 0;
}
public Money( int dol, int cent){
if( dol < 0 || cent < 0){
throw new IllegalArgumentException(\"dollar/cent value can not be < 0\");
}
dollars = dol;
cents = cent;
normalize();
}
public Money( Money other){
dollars = other.dollars;
cents = other.cents;
}
// this function is used to convert cents to dollars if cents exceeds 100
private void normalize(){
int centDollar = cents/100;
cents = cents%100;
dollars = dollars + centDollar;
}
public int getDollars(){
return dollars;
}
public int getCents(){
return cents;
}
public void setMoney( int dollars, int cents){
if( dollars < 0 || cents < 0){
throw new IllegalArgumentException(\"dollar/cent value can not be < 0\");
}
this.dollars = dollars;
this.cents = cents;
normalize();
}
public double getMoney( ){
return dollars+(cents/100.0);
}
public void add( int dollars){
if( dollars < 0){
throw new IllegalArgumentException(\"dollar/cent value can not be < 0\");
}
this.dollars += dollars;
}
public void add ( int dollars, int cents){
if( dollars < 0 || cents < 0){
throw new IllegalArgumentException(\"dollar/cent value can not be < 0\");
}
this.dollars = this.dollars + dollars;
this.cents = this.cents + cents;
normalize();
}
public void add( Money other){
this.dollars = this.dollars + other.dollars;
this.cents = this.cents + other.cents;
normalize();
}
public boolean equals( Object o){
if( o instanceof Money){
Money m = (Money)o;
if(this.dollars == m.dollars && this.cents == m.cents)
return true;
}
return false;
}
public String toString(){
return \"$\"+(dollars+(cents/100.0));
}
}





