Purse

Purse - @return output the string representation of coins...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
import java.util.ArrayList; i /** A purse holds a collection of coins. */ public class Purse { /** instance variables */ private ArrayList<String> coins; /** Constructs an empty purse. */ public Purse() { coins = new ArrayList<String>(); } /** Add a coin to the purse. @param coinName the coin to add */ public void addCoin(String coinName) { coins.add(coinName); } /** Displays the string representation of the object.
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: @return output the string representation of coins */ public String toString() { if (coins.size() == 0) return &quot;Purse&quot;; String output = &quot;Purse[&quot;; for (String coin : coins) { output = output + coin + &quot;,&quot;; } output = output.substring(0, output.length() - 1); // remove the last &quot;,&quot; return output + &quot;]&quot;; } }...
View Full Document

This note was uploaded on 06/07/2010 for the course CS 201 taught by Professor Staff during the Spring '08 term at Iona.

Ask a homework question - tutors are online