i have absolutely no idea what to do, can someone explain the steps, or do it and explain what they did?

/**
   A purse has an array list of coin names that can 
   be changed by adding coins.
*/
public class Purse
{  
    //instance variables
    private String[] coins; //the money in the purse
    private int count ; // the number of coins in the purse
    private int size ; // the real size of the purse
    private final int SIZE = 100 ; //the default size

    /**
       Constructs a purse with a default size.
    */
    public Purse()
    {   
	// todo: initialize coins, count and size.
	// approximate lines of code required: 3
    }
    
    /**
       Constructs a purse with a given size.
       @param size the size of the purse
    */
    public Purse(int size)
    {   
	// todo: initialize coins, count and size.
	// approximate lines of code required: 3
    }
    
    /**
       Deposits money into the purse.
       @param coin the coin to deposit
       @return false if the coin cannot be added (the purse is full
               or the coin is not legal)
    */
    public boolean addCoin(String coin)
    {  
	// todo: fill in the method
	// approximate lines of code required: 4
    }
    
    /**
       Private helper method to check legality of coin
       @return true if legal, false otherwise
    */
    private boolean legal(String coin)
    {   
	// todo: fill in the method
	// approximate lines of code required: 4
    }
    /**
       Produces string representation of money in purse.
       @return Purse[coin,coin,...]
    */
    public String toString()
    {   
	// todo: fill in the method
	// approximate lines of code required: 8
    }
    /**
       Reverses the order of the money in the purse
    */
    public void reverse()
    {   
	// todo: fill in the method
	// approximate lines of code required: 6
    }
    /**
       Removes top coin from the purse
       @return name of the coin removed, null if no coin removed
    */
    public String remove()
    {   
	// todo: fill in the method
	// approximate lines of code required: 8
    }
    /**
       Transfers all the money from a given purse to this purse
       @param other the reference to the other purse
    */
    public void transfer(Purse other)
    {   
	// todo: fill in the method
	// approximate lines of code required: 5
    }
    
}
/**
   A class to test the Purse class.
*/
public class PurseTester
{
   /**
      Tests the methods of the Purse class.
      @param args not used
   */
   public static void main(String[] args)
   {
      Purse harrysPurse = new Purse();
      Tester.test(1, harrysPurse.addCoin("Dime"), true);
      Tester.test(2, harrysPurse.addCoin("Nickel"), true);
      harrysPurse.addCoin("Quarter");
      Tester.test(3, harrysPurse.addCoin("Yen"), false);
      harrysPurse.addCoin("Dime");
      Tester.test(4, harrysPurse.toString(),
		  "Purse[Dime,Nickel,Quarter,Dime]");
      harrysPurse.reverse() ;
      Tester.test(5, harrysPurse.toString(),
		  "Purse[Dime,Quarter,Nickel,Dime]");
      Purse marysPurse = new Purse(2) ;
      marysPurse.addCoin("Dime") ;
      marysPurse.addCoin("Nickel") ;
      harrysPurse.transfer(marysPurse) ;
      Tester.test(6, harrysPurse.toString(),
		  "Purse[Dime,Quarter,Nickel,Dime,Nickel,Dime]");
      Tester.test(7, marysPurse.toString(),
		  "Purse[]");
   }
}
/**
   This class contains some static methods to compare values.
 */
public class Tester
{
    /**
       Compares two boolean values.
       Prints a message if they are not equal.
       @param number the number of the test
       @param obtained the typically unknown value
       @param expected the expected value
     */
    public static void test(int number, boolean obtained, boolean expected)
    {
	System.out.println(number + ". Obtained: " + obtained) ;
	System.out.println(number + ". Expected: " + expected) ;
	System.out.println("--------------------------------------") ;
	if (obtained != expected) {
	    System.out.println("ERROR! OBTAINED IS NOT EXPECTED") ;
	    System.out.println("--------------------------------------") ;
	}
    }
    /**
       Compares two String values.
       Prints a message if they are not equal.
       @param number the number of the test
       @param obtained the typically unknown value
       @param expected the expected value
     */
    public static void test(int number, String obtained, String expected)
    {
	System.out.println(number + ". Obtained: " + obtained) ;
	System.out.println(number + ". Expected: " + expected) ;
	System.out.println("--------------------------------------") ;
	if (! obtained.equals(expected)) {
	    System.out.println("ERROR! OBTAINED IS NOT EXPECTED") ;
	    System.out.println("--------------------------------------") ;
	}
    }
    /**
       Compares two integer values.
       Prints a message if they are not equal.
       @param number the number of the test
       @param obtained the typically unknown value
       @param expected the expected value
     */
    public static void test(int number, int obtained, int expected)
    {
	System.out.println(number + ". Obtained: " + obtained) ;
	System.out.println(number + ". Expected: " + expected) ;
	System.out.println("--------------------------------------") ;
	if (obtained != expected) {
	    System.out.println("ERROR! OBTAINED IS NOT EXPECTED") ;
	    System.out.println("--------------------------------------") ;
	}
    }
}

Recommended Answers

All 7 Replies

Your class outline already has pretty straightforward instructions.

Ask specific questions about the parts which confuse you.

what type should my array be?
should it look like this?

String[] purse = new purse[]

and this

#
public boolean addCoin(String coin)
#
{
#
// todo: fill in the method
#
// approximate lines of code required: 4
#
}

i dont know what to type so it adds coins

Please use the code-tags when posting code.

A String array is declared

String[] purse = new String[the size of the array]

So if you want the variable purse to be an array of size 100 the code will be:

String[] purse = new String[100]

Regarding the method addCoin(String coin) - you get a String with a coin name, and you need to add it into the coins array - if you can add it to the array (if the array still have unused cells) you add and return true, otherwise you reutrn false.

and for count, its count++,right?

what does the code look like for addCoin?

How about showing some effort and trying to code? Show us where you get stuck and we will continue from there.

The count variable will hold the place in which the new coin needs to be inserted. When you add a new coin you will put it in the count place and then you will increase its value by one so the next coin will be inserted at the next element of the array.

Before inserting you need to check using if statements if the count value is greater than the length of the array. If the count is greater than the size of the array you cannot add any more coins and the methods needs to return false. Also inside the method add, you need to call the legal method to check if the argument is truly a coin. If the legal method returns false then you shouldn't insert.

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.