Complete the constructor deposit and toString methods of the
Complete the constructor, deposit, and toString methods of the SavingsAccount class for the Money
class given below. (12 pts.)
public class Money
{
private int dollars; // 0 <= dollars
private int cents; // 0 <= cents < 100
public Money(int dollars, int cents)
{ // assume implemented }
public Money add(Money other)
{ // assume implemented to return the sum of the addition }
public String toString()
{ // assume implemented to return string of the form $12.45 }
}
public class SavingsAccount
{
private Money amount;
private String acct_num;
public SavingsAccount(String acct_num)
{ // creates new account with initial balance of $0.0 }
}
public void deposit(Money amt)
{
}
// toString method to add here to return string of form \"Acct 10405: $345.64\".
}
Solution
Hi
I have updated the code and highlighted the code changes below
public class Money
{
private int dollars; // 0 <= dollars
private int cents; // 0 <= cents < 100
public Money(int dollars, int cents)
{ // assume implemented
this.dollars = dollars;
this.cents = cents;
}
public Money add(Money other)
{ // assume implemented to return the sum of the addition
other.cents = other.cents + cents;
other.dollars = other.dollars + dollars;
return other;
}
public String toString()
{ // assume implemented to return string of the form $12.45
return \"$\"+dollars+\".\"+cents;
}
}
public class SavingsAccount
{
private Money amount;
private String acct_num;
public SavingsAccount(String acct_num)
{ // creates new account with initial balance of $0.0 }
this.acct_num = acct_num;
amount = new Money(0,0);
}
public void deposit(Money amt)
{
amount.add(amt);
}
// toString method to add here to return string of form \"Acct 10405: $345.64\".
public String toString(){
return \"Acct \"+acct_num+\": $\"+amount.toString()+\".\";
}
}

