Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions