Start New Discussion within our Software Development Community

Please help! Im behind in my homework and dont really even understand it anyways. I cluelessly added the addCoin("...");, which i believe is correct. but I dont know what to add to the return statement for public String toString( )

If you are willing to help, plz provide steps as to how yo figured out how to solve.

Below is the original problem and requirements:

REQUIREMENTS:

  • Implement a class Purse. A purse contains a collection of coins.
  • For simplicity, we will only store the coin names in an ArrayList<String>.
  • Supply a method void addCoin(String coinName)
  • Add a method toString to the Purse class that prints the coins in the purse in the format Purse[Quarter,Dime,Nickel,Dime]

Use the following class in your solution:

import java.util.ArrayList;

/**
   A purse holds a collection of coins.
*/
public class Purse
{
   /**
      Constructs an empty purse.
   */
   public Purse()
   {
      coins = new ArrayList<String>();
   }

   /**
      Adds a coin to the purse.
      @param coinName the coin to add
   */
   public void addCoin(String coinName)
   {
      . . .
   }

   /**
      Returns a string describing the object.
      @return a string in the format "Purse[coinName1,coinName2,...]"
   */
   public String toString()
   {
      . . .
   }

   private ArrayList<String> coins;
}
This question has already been answered. Start a new discussion instead.