I'm having a little trouble trying to figure out how determine if 2 purses have the same coins in them in the same or different order. The books that i have don't really show an example of this at all and I can't find any other resources elsewhere online. I've bolded where I need help. Also i threw in a collection sort hoping that it would lead me down the right path and make things easier but im not too sure necessary. Thank you in advance I'm sure this is really easy to figure out but im just a straight up noob.

import java.util.ArrayList;
import java.util.Collections;

/**
   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)
   {
	   coins.add(coinName);
   }

   /**
      Returns a string describing the object.
      @return a string in the format "Purse[coinName1,coinName2,...]"
   */
   public String toString()
   {
      String coinName1 = "Quarter";
      String coinName2 = "Dime";
      String coinName3 = "Nickel";
     
      String str = "Actual:" + "Purse[" + (coinName1 + "," + coinName2 + "," + coinName3 + "," + coinName2) + "]";
      
      		return str; 
	
   }

   	/**
   	Counts the number of coins in the purse
   	@return the number of coins
    */
   public int countCoins()
   {
	  return coins.size();
   }
   		
   [B]/**
   Determines if a purse has the same coins in the same or different
   order as another purse.
   @param other the other purse
   @return true if the two purses have the same coins in the
    same or different order, false otherwise
    */
   public boolean sameCoins(Purse other)
   {
	  Collections.sort(coins);
	  for(int i = 0; i < coins.size(); i++)
	  {
	  
	  ....
	  
	  }
	
	 .... 
   }[/B]

    private ArrayList<String> coins;
}

This is the PurseTester

/**
   This class tests the Purse class.
*/
public class PurseTester
{
   public static void main(String[] args)
   {
      Purse a = new Purse();
      a.addCoin("Quarter");
      a.addCoin("Dime");
      a.addCoin("Nickel");
      a.addCoin("Dime");

      Purse b = new Purse();
      b.addCoin("Nickel");
      b.addCoin("Dime");
      b.addCoin("Dime");
      b.addCoin("Quarter");

      System.out.println(a.sameCoins(b));
      System.out.println("Expected: true");

      Purse c = new Purse();
      c.addCoin("Quarter");
      c.addCoin("Penny");
      c.addCoin("Nickel");
      c.addCoin("Dime");

      Purse d = new Purse();
      d.addCoin("Nickel");
      d.addCoin("Dime");
      d.addCoin("Dime");
      d.addCoin("Quarter");

      System.out.println(c.sameCoins(d));
      System.out.println("Expected: false");
   }
}

don't make it harder than needs be

public boolean sameCoins(Purse other)
   {
	  Collections.sort(coins);
	  for(int i = 0; i < coins.size(); i++)
	  {
	  
	  ....
	  
	  }
	
	 .... 
   }

could be:

public boolean sameCoins(Purse other){
ArrayList coins = getCoins();
ArrayList otherCoins = other.getCoins();
coins.trimToSize(); otherCoins.trimToSize(); // just in case

Collections.sort(coins); Collections.sort(otherCoins);

if ( coins.size() == otherCoins.size()){
  if ( coins.equals(otherCoins) 
    return true;
}
return false;
}

to do this, you'll need a 'getCoins()' method in your Purse-class though

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.