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;
}

Here is the ArrayList documentation.
Here is a quick example on how to use ArrayLists.

The toString method is derived from the Object class in Java. It simply returns a string that is printed out when you do a System.out.println(object).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.