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

in java Suppose you had the following class Quest2 defined. Further assume that

ID: 3575267 • Letter: I

Question

in java Suppose you had the following class Quest2 defined. Further assume that you wished to override thepublic boolean equals( Object obj ) method so that two objects of the class can be compared to determine if they are equal or not. We will define two Quest2 objects to be equal if their name fields hold the same String value and their val fields hold the same integer value and age fields are also the same.The other fields do not impact equivalence. Write the overriding equals method below.

public class Quest2 {

protected int val;

protected String name;

protected float salary;

protected int   age;

protected float amount;

// assume constructors and other methods are defined

}

Explanation / Answer

public class Quest2
{

protected int val;
protected String name;
protected float salary;
protected int age;
protected float amount;

// assume constructors and other methods are defined

   // Overriding equals() to compare two Quest2 objects
    @Override
    public boolean equals(Object obj)
    {
       if (obj == null)
       {
           return false;
       }

        // If the object is compared with itself then return true
        if (obj == this)
        {
            return true;
        }

        // typecast obj to Quest2 so that we can compare data members
        Quest2 q2 = (Quest2) obj;
       
        // equal if their name fields hold the same String value and their val fields hold the same integer value and age fields are also the same
        if ((this.val == q2.val) && (this.age == q2.age) && (this.name.equals(q2.name) == true))
        {
           return true;
       }
       else
       {
           return false;
       }

    }

}