that transfers the contents of one purse to another.public void transfer(Purse other)
For example if a is
andPurse[Quarter,Dime,Nickel,Dime]
b is
then after the callPurse[Dime,Nickel]
a.transfer(b), a is
andPurse[Quarter,Dime,Nickel,Dime,Dime,Nickel]
b is empty.
Here's how class Coin could look:
class Coin {
Coin(String name, int value) {
this.name = name;
this.value = value;
}
String name;
int value;
boolean equals(Coin other) {
return this.name.equals(other.name);
}
public String toString() {
return this.name;
}
}
Here's a possible Purse class:
import java.util.*;
class Purse {
ArrayList contents;
Purse() {
this.contents = new ArrayList();
}
void add(Coin c) {
this.contents.add(c);
}
public String toString() {
String result = "[";
for (int i = 0; i < this.contents.size(); i++) {
result += this.contents.get(i)+ " ";
}
return result + "]";
}
// your method(s) would come here
Object pop() {
return this.contents.remove(0);
}
boolean empty() {
return this.contents.size() == 0;
}
}
Remember you just need to write transfer(...).