equals method for the Purse class
that checks whether the other purse has the same coins in some order.public boolean equals(Purse other)
For example the purses
andPurse[Quarter,Dime,Nickel,Dime]
should be considered equal.Purse[Nickel,Dime,Dime,Quarter]
However the purses
andPurse[Quarter,Dime,Nickel,Dime]
should NOT be considered equal.Purse[Nickel,Dime,Quarter,Quarter]
Likewise
andPurse[Dime,Nickel,Dime]
should NOT be considered equal either.Purse[Quarter]
(You will probably need one or more helper methods.)
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 equals(...).