How can you write an exact equals method for the Coin class
How can you write an exact .equals method for the Coin class. you can assume that the Country class has its own .equals method.
public class Coin {
//data
protected Country country;
protected String coinName;
protected int value;
// constructor
// default constructor
public Coin()
{
this.country=new Country(\"US\");
this.coinName=\"Quater\";
this.value=25;
}
//parametirezed constructor
public Coin(Country where,String theName,int theValue)
{
this.country=where;
this.coinName=theName;
this.value=theValue;
}
//copy constructor
public Coin(Coin another)
{
}
//methods
//getValue()-returns its value
public int getValue()
{
return value;
}
//toString-returns its String representation
public String tostring()
{
return coinName+\"with value\"+value;
}
}
Solution
public class Coin {
// data
protected Country country;
protected String coinName;
protected int value;
/**
* Default constructor
*/
public Coin() {
this.country = new Country(\"US\");
this.coinName = \"Quater\";
this.value = 25;
}
/**
* Parameterized constructor
* @param where
* @param theName
* @param theValue
*/
public Coin(Country where, String theName, int theValue) {
this.country = where;
this.coinName = theName;
this.value = theValue;
}
/**
* Copy constructor
* @param another
*/
public Coin(Coin another) {
this.country = another.country;
this.coinName = another.coinName;
this.value = another.value;
}
// methods
/**
* getValue()-returns the coin\'s value
* @return
*/
public int getValue() {
return value;
}
/**
* toString-returns its String representation
* @return
*/
public String tostring() {
return coinName + \"with value\" + value;
}
/**
* Checks if another coin is same as this coin
*/
@Override
public boolean equals(Object obj) {
if ((obj == null) || !(obj instanceof Coin))
return false;
Coin another = (Coin) obj;
if((this == another) ||
(this.country.equals(another) &&
this.coinName.equalsIgnoreCase(another.coinName) &&
this.value == another.value))
return true;
return false;
}
}


