Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How can you write an exact .equals method for the Coin class. you can assume tha

ID: 3926358 • Letter: H

Question

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;
}
  

}

Explanation / Answer

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;
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote