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

For each of the methods equals and hashCode in the following class, explain whet

ID: 3904288 • Letter: F

Question

For each of the methods equals and hashCode in the following class, explain whether or not they are implemented correctly (with respect to their specification in the Object class and any additional specification in this class). If they are not implemented correctly, show how they may be rewritten to address the problems that you have identified /**An immutable representation of a term in a polynomial. */ public class Term [ private int coeff 1/ the coefficient of the term private int exp; // the exponent of the term /** Creates a new term with coefficient coeff *and exponent exp ? public Term (int coeff, int exp) this.coeffcoeff; this exp = exp; /**Returns the coefficient of the term * public int getCoeff O return coeff; /**Returns the exponent of the term / public int getExp )f return exp; * Returns true if obj is a Term with the same *exponent and coefficient as this, and false otherwise. @Override public boolean equals (Object obj Term term(Term) obj; // the Term to compare return ((term.coeff - this.coeff) && (term.exp== this.exp )); @Override public int hashCode () i return super.hashCode );

Explanation / Answer

public class Term { private int coeff; private int exp; public Term(int coeff, int exp) { this.coeff = coeff; this.exp = exp; } public int getCoeff() { return coeff; } public int getExp() { return exp; } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Term)) return false; Term term = (Term) o; if (coeff != term.coeff) return false; return exp == term.exp; } @Override public int hashCode() { int result = coeff; result = 31 * result + exp; return result; } }