My assignment is as follows.

Create a class called PursePile that can hold any number of Purse objects. The class should have the following methods:

public void add(Purse p) //adds a purse to the pile

public Purse findAPursesWith(String coinName) //searches the pile until a purse with the given coin is found

public Purse findWithMostCoins() //returns the purse in the pile that has the most coins in it. Does not consider the value of the coins

Create another class called PileTester that, when run, creates a PursePile, adds some purses, and tests all of the methods above.

Use the Purse class you created for the previous assignment. You can modify this class as needed, including adding methods to it.

Here is what I have:
*The bolded section is where Im getting an error.
Please help me figure out how to correct it.*

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

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

   public String getCoinName() 
   {
   	return coinName;
   }
   
   /**
      Adds a coin to the purse.
      @param coinName the coin to add
   */
   public void addCoin(String coinName)
   {
	   coins.add(coinName);
   }
   
//Returns how many coins a purse has.

   public int count()
   {
   return coins.size();
   }

   
   /**
      Returns a string describing the object.
      @return a string in the format "Purse[coinName1,coinName2,...]"
   */
   public String toString()
   {
	   String returnString = "Purse[";
	   if(coins.size() == 0)
	   {
		   return returnString + "]";
	   }
	   
	   returnString += coins.get(0) + "";
	   for(int i = 1; i < coins.size(); i++)
	   {
		   returnString += "," + coins.get(i);
	   }
	   return returnString + "]";
   }

   		
   /**
   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){
	   
	   if(this.coins.size() != other.coins.size())
	   {
		   return false;
	   }
	   
	   //Must make a copy before sorting
	   ArrayList<String> thisOne = new ArrayList<String>(this.coins);
	   ArrayList<String> otherOne = new ArrayList<String>(other.coins);
	   
	   //Sort the collection
	   Collections.sort(thisOne);
	   Collections.sort(otherOne);
	   
	   for(int i = 0; i < this.coins.size(); i++)
	   {
		   if(!thisOne.get(i).equals(otherOne.get(i)))
				   {
			   return false;
				   }
	   }
	   return true;
   }
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;


public class PursePile {
	
	private ArrayList<Purse> purses;
	private ArrayList<String> coins;
	public PursePile()
	{
		purses = new ArrayList<Purse>();
		coins = new ArrayList<String>();
	}

	//adds a purse to the pile
	public void add(Purse p)
	{
		purses.add(p);
	}
	
	//searches the pile until a purse with the given coin is found
	public Purse findAPursesWith(String coinName)
	{
		
		for(Purse p: purses)
		{
			[B]if(p.getCoinName().equalsIgnoreCase(coinName))[/B]
				return p;
		}
		return null;
	}
		

//		
//		String x;
//		
//		for (int i = 0; i < coins.size(); i++)
//		{
//			
//			x = coins.get(i);
//			if(x.equals(coinName))
//			{
//				return x;
//			}
//				
//		}
//		return null;
	
	
	//returns the purse in the pile that has the most coins in it.  Does not consider the value of the coins
	public Purse findWithMostCoins()
	{
		if(purses.size() == 0) return null;
		Purse mostCoins = purses.get(0);
		for(int i = 1; i < purses.size(); i++)
		{
			Purse p = purses.get(i);
			if (p.count() > mostCoins.count())
				mostCoins = p;
		}
		return mostCoins;
	}
			
}
import java.util.Scanner;


public class PileTester {

	/**
	   This class tests the PursePile class.
	*/
	
	
	
	
	public static void main(String[] args) 
	{
		
		String input;
		
		
		//Brings all of the purses together.
		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");
		
		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");
		
		//Creates a PursePile
		PursePile pile = new PursePile();
		pile.add(a);
		pile.add(b);
		pile.add(c);
		pile.add(d);
		
		//Creates a Scanner class for user input
		Scanner keyboard = new Scanner(System.in);
	
		System.out.println("Which coin would you like to find?: \nPlease enter either Quarter, Dime, Nickel, or Penny.");
		input = keyboard.nextLine();
		
		[B]System.out.println(pile.findAPursesWith(input));[/B]
		
		System.out.println(pile.findWithMostCoins());

	}

}

Recommended Answers

All 3 Replies

What errors? You didn't specify what they are. Have you read the stack trace of the error? It indicates the nature of the problem and the line number.

I'm interested to see what goes on with this one, why wouldn't you need his line in bold to be this?

if(p.getCoinName(coinName).equalsIgnoreCase(coinName))

I'm very new to Java so please be kind if I'm completely lost, just trying to follow what's going on even though I don't know the error, I'm trying to follow the logic of the program.
Thanks

@rapture: No, because that method signature does not have a parameter. He is calling it correctly, but it's not doing what he thinks it might be doing.

That method is not actually doing anything but returning a null string variable that was never initialized, which in turn is causing a null pointer exception I would imagine (I haven't run any of the code and he didn't specify the error).

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.