Question
Purse 1. Implement a class Purse. A purse contains a collection of coins. For simplicity, we will only store the coin names in an ArrayList
Purse
1. Implement a class Purse. A purse contains a collection of coins. For simplicity, we will only store the coin names in an ArrayList
a. Supply a method
void addcoin(String coinName)
that adds a coin to the purse.
b. Write a method toString() to the Purse class that prints the coins in the purse in the format
Purse[Quarter, Dime, Nickel, Dime]
2. Write a method reverse that reverses the sequence of coins in a purse. Use the toString method of the preceding assignment to test your code. For example, if reverse is called with a purse
Purse[Quarter, Dime, Nickel, Dime]
Then the purse is changed to
Purse[Dime, Nickel, Dime, Quarter]
3. Write a method for the Purse class,
public void transfer(Purse other)
that transfers the contents of one purse to another. For example, if a is
Purse[Quarter, Dime, Nickel, Dime]
and b is
Purse[Dime, Nickel]
then after the call, a.transfer(b), a is
Purse[Quarter, Dime, Nickel, Dime, Dime, Nickel]
and b is empty.
4. Write a method for the Purse class
public boolean sameContents(Purse other)
That checks whether the other purse has the same coins in the same order.
5. Write a method
public boolean sameCoins(Purse other)
that checks whether the other purse has the same coins, perhaps in a different order. For example, the purses
Purse[Quarter, Dime, Nickel, Dime]
and
Purse[Nickel, Dime, Dime, Quarter]
should be considered equal. You will probably need one or more helper methods.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started