The programming
assignment is to implement a class Purse. A purse
contains a collection of coins. Each coin object must contain its
name. You should not put a limit on the number of coins that a purse
can hold. This program should provide the user with the ability to
display the contents of the purse, add coins, count coins, calculate
the amount of money in purse and for extra credit, spend coins. You
will need 2 Java Object Classes: one to define the Coin objects, and
one for the Purse object.

There is a sample PurseTester class and its output. You are required
to use this class to test your code but you may make modifications to
the method calls to match your method names. The code below is the sample tester that I have to use and the code below that is what I have so far. I have no clue what I am doing and I didn't even start the purse class yet. If you could help me, that would be amazing.


public class PurseTester {
public static void main(String[] args)
{
Purse myPurse = new Purse();
System.out.println("Created my purse!");
System.out.println("My Purse = " + myPurse.toString());
System.out.println(String.format("Tota... value = $%.2f\n",
myPurse.getTotalValue()));

System.out.println("Trying to add an invalid coin called
Dollar...");
myPurse.addCoin(new Coin("Dollar"));
System.out.println("My Purse = " + myPurse.toString());
System.out.println(String.format("Tota... value = $%.2f\n",
myPurse.getTotalValue()));

System.out.println("Adding coins to purse");
myPurse.addCoin(new Coin(Coin.PENNY));
myPurse.addCoin(new Coin(Coin.NICKEL));
myPurse.addCoin(new Coin(Coin.PENNY));
myPurse.addCoin(new Coin(Coin.PENNY));
myPurse.addCoin(new Coin(Coin.QUARTER));
myPurse.addCoin(new Coin(Coin.QUARTER));
System.out.println("My Purse = " + myPurse.toString());
System.out.println("I have " + myPurse.countCoin(Coin.PENNY) +
" pennies, " +
myPurse.countCoin(Coin.NICKEL)
+ " nickels, " +
myPurse.countCoin(Coin.DIME) +
" dimes, and " +
myPurse.countCoin(Coin.QUARTER)
+ "quarters.");
System.out.println(String.format("Tota... value = $%.2f\n",
myPurse.getTotalValue()));

/* extra credit from here on down */
System.out.println("Attempting to spend dime that you don't have.");
if (myPurse.spendCoin(Coin.DIME))
System.out.println(Coin.DIME + " was spent!");
else
System.out.println("No " + Coin.DIME + " was found in
purse!");

System.out.println("\nAdding a dime.");
myPurse.addCoin(new Coin(Coin.DIME));
System.out.println("My Purse = " + myPurse.toString());
System.out.println("I have " + myPurse.countCoin(Coin.PENNY) +
" pennies, " +
myPurse.countCoin(Coin.NICKEL)
+ " nickels, " +
myPurse.countCoin(Coin.DIME) +
" dimes, and " +
myPurse.countCoin(Coin.QUARTER)
+ "quarters.");
System.out.println(String.format("Tota... value = $%.2f\n",
myPurse.getTotalValue()));

System.out.println("Spending all my money...");
myPurse.spendCoin(Coin.DIME);
myPurse.spendCoin(Coin.QUARTER);
myPurse.spendCoin(Coin.QUARTER);
myPurse.spendCoin(Coin.PENNY);
myPurse.spendCoin(Coin.PENNY);
myPurse.spendCoin(Coin.NICKEL);
myPurse.spendCoin(Coin.PENNY);
System.out.println("My Purse = " + myPurse.toString());
System.out.println(String.format("Tota... value = $%.2f\n",
myPurse.getTotalValue()));
}
}
Output from PurseTester

Created my purse!
My Purse = Purse[]
Total value = $0.00

Trying to add an invalid coin called Dollar...
My Purse = Purse[]
Total value = $0.00

Adding coins to purse
My Purse = Purse[Penny,Nickel,Penny,Penny,Quarter,Q...
I have 3 pennies, 1 nickels, 0 dimes, and 2 quarters.
Total value = $0.58

Attempting to spend dime that you don't have.
No Dime was found in purse!

Adding a dime.
My Purse = Purse[Penny,Nickel,Penny,Penny,Quarter,Q...
I have 3 pennies, 1 nickels, 1 dimes, and 2 quarters.
Total value = $0.68

Spending all my money...
My Purse = Purse[]
Total value = $0.00

________________________________________________________________________________
__
THIS IS WHAT I HAVE SO FAR!!!!!!! I ONLY STARTED THE COIN CLASS!!!!

public class Coin
{

private final double PENNY = 0.01;
private final double NICKEL = 0.05;
private final double DIME = 0.10;
private final double QUARTER = 0.25;
private double Value;
private String Name;

public double getValue()
{
return Value;
}

public String getName()
{
return Name;
}

public Coin(double aValue)
{
Value = aValue;
Name = "";
}

public Coin(String aName)
{
Value = 0;
Name = aName;
}

public String toString()
{
return "Name:"+ Name +" Value:" + Value;
}

public class PurseTester {
public static void main(String[] args)
{
Purse myPurse = new Purse();
System.out.println("Created my purse!");
System.out.println("My Purse = " + myPurse.toString());
System.out.println(String.format("Tota... value = $%.2f\n",
myPurse.getTotalValue()));

System.out.println("Trying to add an invalid coin called
Dollar...");
myPurse.addCoin(new Coin("Dollar"));
System.out.println("My Purse = " + myPurse.toString());
System.out.println(String.format("Tota... value = $%.2f\n",
myPurse.getTotalValue()));

System.out.println("Adding coins to purse");
myPurse.addCoin(new Coin(Coin.PENNY));
myPurse.addCoin(new Coin(Coin.NICKEL));
myPurse.addCoin(new Coin(Coin.PENNY));
myPurse.addCoin(new Coin(Coin.PENNY));
myPurse.addCoin(new Coin(Coin.QUARTER));
myPurse.addCoin(new Coin(Coin.QUARTER));
System.out.println("My Purse = " + myPurse.toString());
System.out.println("I have " + myPurse.countCoin(Coin.PENNY) +
" pennies, " +
myPurse.countCoin(Coin.NICKEL)
+ " nickels, " +
myPurse.countCoin(Coin.DIME) +
" dimes, and " +
myPurse.countCoin(Coin.QUARTER)
+ "quarters.");
System.out.println(String.format("Tota... value = $%.2f\n",
myPurse.getTotalValue()));

/* extra credit from here on down */
System.out.println("Attempting to spend dime that you don't have.");
if (myPurse.spendCoin(Coin.DIME))
System.out.println(Coin.DIME + " was spent!");
else
System.out.println("No " + Coin.DIME + " was found in
purse!");

System.out.println("\nAdding a dime.");
myPurse.addCoin(new Coin(Coin.DIME));
System.out.println("My Purse = " + myPurse.toString());
System.out.println("I have " + myPurse.countCoin(Coin.PENNY) +
" pennies, " +
myPurse.countCoin(Coin.NICKEL)
+ " nickels, " +
myPurse.countCoin(Coin.DIME) +
" dimes, and " +
myPurse.countCoin(Coin.QUARTER)
+ "quarters.");
System.out.println(String.format("Tota... value = $%.2f\n",
myPurse.getTotalValue()));

System.out.println("Spending all my money...");
myPurse.spendCoin(Coin.DIME);
myPurse.spendCoin(Coin.QUARTER);
myPurse.spendCoin(Coin.QUARTER);
myPurse.spendCoin(Coin.PENNY);
myPurse.spendCoin(Coin.PENNY);
myPurse.spendCoin(Coin.NICKEL);
myPurse.spendCoin(Coin.PENNY);
System.out.println("My Purse = " + myPurse.toString());
System.out.println(String.format("Tota... value = $%.2f\n",
myPurse.getTotalValue()));
}
}

Comments
You actually didn't read my suggestion at all :angry: !!

Read the links above. Use code tags like this:

[code=JAVA] // paste code here

[/code]


Here is your program in code tags. The code tags help a little, but the code is still unreadable since it's not formatted/indented.

public class PurseTester {
public static void main(String[] args)
{
Purse myPurse = new Purse();
System.out.println("Created my purse!");
System.out.println("My Purse = " + myPurse.toString());
System.out.println(String.format("Tota... value = $%.2f\n",
myPurse.getTotalValue()));

System.out.println("Trying to add an invalid coin called
Dollar...");
myPurse.addCoin(new Coin("Dollar"));
System.out.println("My Purse = " + myPurse.toString());
System.out.println(String.format("Tota... value = $%.2f\n",
myPurse.getTotalValue()));

System.out.println("Adding coins to purse");
myPurse.addCoin(new Coin(Coin.PENNY));
myPurse.addCoin(new Coin(Coin.NICKEL));
myPurse.addCoin(new Coin(Coin.PENNY));
myPurse.addCoin(new Coin(Coin.PENNY));
myPurse.addCoin(new Coin(Coin.QUARTER));
myPurse.addCoin(new Coin(Coin.QUARTER));
System.out.println("My Purse = " + myPurse.toString());
System.out.println("I have " + myPurse.countCoin(Coin.PENNY) +
" pennies, " +
myPurse.countCoin(Coin.NICKEL)
+ " nickels, " +
myPurse.countCoin(Coin.DIME) +
" dimes, and " +
myPurse.countCoin(Coin.QUARTER)
+ "quarters.");
System.out.println(String.format("Tota... value = $%.2f\n",
myPurse.getTotalValue()));

/* extra credit from here on down */
System.out.println("Attempting to spend dime that you don't have.");
if (myPurse.spendCoin(Coin.DIME))
System.out.println(Coin.DIME + " was spent!");
else
System.out.println("No " + Coin.DIME + " was found in
purse!");

System.out.println("\nAdding a dime.");
myPurse.addCoin(new Coin(Coin.DIME));
System.out.println("My Purse = " + myPurse.toString());
System.out.println("I have " + myPurse.countCoin(Coin.PENNY) +
" pennies, " +
myPurse.countCoin(Coin.NICKEL)
+ " nickels, " +
myPurse.countCoin(Coin.DIME) +
" dimes, and " +
myPurse.countCoin(Coin.QUARTER)
+ "quarters.");
System.out.println(String.format("Tota... value = $%.2f\n",
myPurse.getTotalValue()));

System.out.println("Spending all my money...");
myPurse.spendCoin(Coin.DIME);
myPurse.spendCoin(Coin.QUARTER);
myPurse.spendCoin(Coin.QUARTER);
myPurse.spendCoin(Coin.PENNY);
myPurse.spendCoin(Coin.PENNY);
myPurse.spendCoin(Coin.NICKEL);
myPurse.spendCoin(Coin.PENNY);
System.out.println("My Purse = " + myPurse.toString());
System.out.println(String.format("Tota... value = $%.2f\n",
myPurse.getTotalValue()));
}
}

You didn't post the Purse class. Please post it as well as the class above with code tags/formatting/indentation. Also, please ask a more detailed question. Thank you.

Thanks for your help. The problem I am having is just trying to create a purse class. I attempted to create a Coin class, but I think I did that completely wrong. Here is the actual assignment:

Your programming assignment is to implement a class Purse. A purse contains a collection of coins. Each coin object must contain its name. You should not put a limit on the number of coins that a purse can hold. This program should provide the user with the ability to display the contents of the purse, add coins, count coins, calculate the amount of money in purse and for extra credit, spend coins. You will need 2 Java Object Classes: one to define the Coin objects, and one for the Purse object.

In addition, you will need to create a PurseTester class to test your Purse and Coin classes. I have included a sample PurseTester class and its output. You are highly encouraged to use this class to test your code but you may create a similar one if you wish


Here is the tester given:

// paste code here

public class PurseTester {
public static void main(String[] args)
{
Purse myPurse = new Purse();
System.out.println("Created my purse!");
System.out.println("My Purse = " + myPurse.toString());
System.out.println(String.format("Total value = $%.2f\n",
myPurse.getTotalValue()));

System.out.println("Trying to add an invalid coin called Dollar...");
myPurse.addCoin(new Coin("Dollar"));
System.out.println("My Purse = " + myPurse.toString());
System.out.println(String.format("Total value = $%.2f\n",
myPurse.getTotalValue()));

System.out.println("Adding coins to purse");
myPurse.addCoin(new Coin(Coin.PENNY));
myPurse.addCoin(new Coin(Coin.NICKEL));
myPurse.addCoin(new Coin(Coin.PENNY));
myPurse.addCoin(new Coin(Coin.PENNY));
myPurse.addCoin(new Coin(Coin.QUARTER));
myPurse.addCoin(new Coin(Coin.QUARTER));
System.out.println("My Purse = " + myPurse.toString());
System.out.println("I have " + myPurse.countCoin(Coin.PENNY) + " pennies, " +
myPurse.countCoin(Coin.NICKEL) + " nickels, " +
myPurse.countCoin(Coin.DIME) + " dimes, and " +
myPurse.countCoin(Coin.QUARTER) + "quarters.");
System.out.println(String.format("Total value = $%.2f\n",
myPurse.getTotalValue()));

/* extra credit from here on down */
System.out.println("Attempting to spend dime that you don't have.");
if (myPurse.spendCoin(Coin.DIME))
System.out.println(Coin.DIME + " was spent!");
else
System.out.println("No " + Coin.DIME + " was found in purse!");

System.out.println("\nAdding a dime.");
myPurse.addCoin(new Coin(Coin.DIME));
System.out.println("My Purse = " + myPurse.toString());
System.out.println("I have " + myPurse.countCoin(Coin.PENNY) + " pennies, " +
myPurse.countCoin(Coin.NICKEL) + "nickels, " +
myPurse.countCoin(Coin.DIME) + " dimes, and " +
myPurse.countCoin(Coin.QUARTER) + "quarters.");

System.out.println(String.format("Total value = $%.2f\n",
myPurse.getTotalValue()));

System.out.println("Spending all my money...");
myPurse.spendCoin(Coin.DIME);
myPurse.spendCoin(Coin.QUARTER);
myPurse.spendCoin(Coin.QUARTER);
myPurse.spendCoin(Coin.PENNY);
myPurse.spendCoin(Coin.PENNY);
myPurse.spendCoin(Coin.NICKEL);
myPurse.spendCoin(Coin.PENNY);
System.out.println("My Purse = " + myPurse.toString());
System.out.println(String.format("Total value = $%.2f\n",
myPurse.getTotalValue()));
}
}

public class PurseTester {
    public static void main(String[] args) 
    {
        Purse myPurse = new Purse(); 
        System.out.println("Created my purse!");
 	  System.out.println("My Purse = " + myPurse.toString());
        System.out.println(String.format("Total value = $%.2f\n",
                                         myPurse.getTotalValue()));

        System.out.println("Trying to add an invalid coin called Dollar...");
        myPurse.addCoin(new Coin("Dollar"));
 	 System.out.println("My Purse = " + myPurse.toString());
        System.out.println(String.format("Total value = $%.2f\n",
                                         myPurse.getTotalValue()));

	 System.out.println("Adding coins to purse");		
        myPurse.addCoin(new Coin(Coin.PENNY)); 
        myPurse.addCoin(new Coin(Coin.NICKEL)); 
        myPurse.addCoin(new Coin(Coin.PENNY)); 
        myPurse.addCoin(new Coin(Coin.PENNY));
        myPurse.addCoin(new Coin(Coin.QUARTER)); 
        myPurse.addCoin(new Coin(Coin.QUARTER));
 	 System.out.println("My Purse = " + myPurse.toString());
        System.out.println("I have " + myPurse.countCoin(Coin.PENNY) + " pennies, " +
                                       myPurse.countCoin(Coin.NICKEL) + " nickels, " +
                                       myPurse.countCoin(Coin.DIME) + " dimes, and " +
                                       myPurse.countCoin(Coin.QUARTER) + "quarters.");
        System.out.println(String.format("Total value = $%.2f\n",
                                       myPurse.getTotalValue()));

        /* extra credit from here on down */
	 System.out.println("Attempting to spend dime that you don't have.");
        if (myPurse.spendCoin(Coin.DIME))
            System.out.println(Coin.DIME + " was spent!");
        else
            System.out.println("No " + Coin.DIME + " was found in purse!");
        
        System.out.println("\nAdding a dime.");
        myPurse.addCoin(new Coin(Coin.DIME));
 	 System.out.println("My Purse = " + myPurse.toString());
        System.out.println("I have " + myPurse.countCoin(Coin.PENNY) + " pennies, " +
                                       myPurse.countCoin(Coin.NICKEL) + " nickels, " +
                                       myPurse.countCoin(Coin.DIME) + " dimes, and " +
                                       myPurse.countCoin(Coin.QUARTER) + "quarters.");
        System.out.println(String.format("Total value = $%.2f\n",
                                        myPurse.getTotalValue()));
	  
	 System.out.println("Spending all my money...");
	 myPurse.spendCoin(Coin.DIME);
        myPurse.spendCoin(Coin.QUARTER);
        myPurse.spendCoin(Coin.QUARTER);
        myPurse.spendCoin(Coin.PENNY);
        myPurse.spendCoin(Coin.PENNY);
        myPurse.spendCoin(Coin.NICKEL);
        myPurse.spendCoin(Coin.PENNY);
 	 System.out.println("My Purse = " + myPurse.toString());
        System.out.println(String.format("Total value = $%.2f\n",
                                         myPurse.getTotalValue()));
   }
}

OK, one thing at a time. One, don't START with the PurseTester you were given. START with something much smaller. You'll EVENTUALLY add everything in the PurseTester class, but don't try to use it yet. Your Purse class relies on your Coin class, so write a bare-bones Coin class first:

public class Coin
{
    String name;

    public Coin (String coinname)
    {
        name = coinname;
    }
}

Then write a bare-bones Purse class:

public class Purse
{
    public Purse ()
    {
    }
}

Then write a bare-bones PurseTester class (keep the one you have, but comment out everything in main except for this line):

Purse myPurse = new Purse();

This will get you started a little bit. Make sure it all compiles and runs flawlessly, then start adding things from there, one line at a time.

The programming
assignment is to implement a class Purse. A purse contains a collection of coins. Each coin object must contain its
name. You should not put a limit on the number of coins that a purse can hold. This program should provide the user with the ability to display the contents of the purse, add coins, count coins, calculate the amount of money in purse and for extra credit, spend coins. You will need 2 Java Object Classes: one to define the Coin objects, and one for the Purse object.

1. Decide what the "things"/Objects in your project assignment are. In this case, a purse and coins.
2. Decide what the relationships between the things in your project are. In this case, the main relationship is that a Purse holds coins.
3. Decide how to implement the relationships between your Objects, and decide what kind of data each class (Purse and Coin) needs. Well, since a purse holds coins, the Purse class needs to have a List of coins. But it also must impose no limit on the number of coins, i.e. it has to be able to grow. You can use the ArrayList class to do this. Since a coin must contain its name, according to the assignment, the Coin class must have a String for the name.
4. Determine what methods you need. The "this program must provide the user with the ability to. . . " is a dead giveaway. Pretty much everything listed after that needs to be a method.

Whoever mentioned the idea of starting off with a skeleton of your intended program and growing slowly from there is completely correct. For example, your program can easily show the contents of the purse without counting the coins. These are two completely separate tasks. Don't get overwhelmed, just do it piece by piece.

This article has been dead for over six months. Start a new discussion instead.