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

(20 pts) Assume two cows are the same if they have the same name. Write class Co

ID: 3710992 • Letter: #

Question

(20 pts) Assume two cows are the same if they have the same name. Write class Cow and the necessary methods to make main this work, and put a print statement inside each method. Then on the lines next to each call to add, write what the methods would print. class HashSetPractice * On the comment lines provided, write *a11* that is printed as a * result of each line of code. public static void main (String[] args) HashSet herd - new HashSet); herd.add (new Cow ("A")); System.out.println (); herd.add (new Cow ("Alba") ) System.out.println (); herd.add (new Cow ("A")); System.out.println ) herd.add (new Cow ("Aga")) System.out.println (); herd.add (new Cow ("A")); System.out.println ); System.out.println (herd.size ()); //

Explanation / Answer

import java.util.*;

class Cow

{

    String name;

   

    Cow(String name)

    {

        this.name = name;

        System.out.println("Inside Constructor - Setting cow name as " + name);

    }

   

    // Overriding the hashCode method so that two Cow objects wth same name

    // returns the same hashCode

    @Override

    public int hashCode() {

        System.out.println("Overriding hashCode for Cow " + name);

        return name.hashCode();

    }

   

    // Overriding the equals to operator so that when two Cow objects are

    // compared inside the HashSet, the result should be based on their names.

    @Override

    public boolean equals(Object obj) {

        Cow other = (Cow) obj;

        System.out.println("Comparing Cow " + name + " and Cow " + other.name);

        if (this == obj)

            return true; // if both are same objects

        if (obj == null)

            return false; // if object is null

        if (getClass() != obj.getClass())

            return false; // if both have different class types

        if (name != other.name)

            return false; // if both have different names

        return true;

}

}

public class HashSetPratice

{

    public static void main(String[] args)

    {

        HashSet<Cow> herd = new HashSet<Cow>();

       

        herd.add(new Cow("A")); // Inside Constructor - Setting cow name as A

                                // Overriding hashCode for Cow

        System.out.println();

        herd.add(new Cow("Alba")); // Inside Constructor - Setting cow name as Alba

                                   // Overriding hashCode for Cow Alba

        System.out.println();

        herd.add(new Cow("A")); // Inside Constructor - Setting cow name as A

                                // Overriding hashCode for Cow A

                                // Comparing Cow A and Cow A

        System.out.println();

        herd.add(new Cow("Aga")); // Inside Constructor - Setting cow name as Aga

                                  // Overriding hashCode for Cow Aga

        System.out.println();

        herd.add(new Cow("A")); // Inside Constructor - Setting cow name as A

                                // Overriding hashCode for Cow A

                                // Comparing Cow A and Cow A

        System.out.println();

        System.out.println(herd.size()); //3

    }

}