Implement a class Purse. A purse contains a collection of coins. For simplicity,
ID: 3640100 • Letter: I
Question
Implement a class Purse. A purse contains a collection of coins. For simplicity, we will only store the coin names in an ArrayList<String>. Supply a methodvoid addCoin(String coinName)
Add a method toString to the Purse class that returns a string showing the coins in the purse in the format
Add a method to your Purse class that returns a true value if a particular type of coin is in the purse, and returns false otherwise. The method is passed in a string representing the coin to be found:
public boolean findCoin(String coin)
Explanation / Answer
import java.util.ArrayList; /** A purse holds a collection of coins. */ public class Purse { /** Constructs an empty purse. */ public Purse() { coins = new ArrayList(); } /** Adds a coin to the purse. @param coinName the coin to add */ public void addCoin(String coinName) { . . . } /** Returns a string describing the object. @return a string in the format "Purse[coinName1,coinName2,...]" */ public String toString() { . . . } private ArrayList coins; }
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.