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

Create a Java class using generics: ItemPair. Objects of this class hold a pair

ID: 3940379 • Letter: C

Question

Create a Java class using generics: ItemPair. Objects of this class hold a pair of two items that are the same type. For example, an ItemPair can hold two Integers or it could hold two Strings or it could hold two ArrayLists, etc. An ItemPair could not, however hold an Integer and a String. Write the following using generics: the class header instance data a constructor that takes both items in as parameters getters and setters for each item in the pair an equals method: two ItemPairs are the same (logically equivalent) if both items within the pair are the same (logically equivalent)

Explanation / Answer

Please find my implementation.

Please met me know in case of any issue.

// class Header

public class ItemPair<T> {

   // instance variables

   private T first;

   private T second;

  

   // constructor

   public ItemPair(T f, T s){

       first = f;

       second = s;

   }

   // getters and setters

  

   public T getFirst() {

       return first;

   }

   public void setFirst(T first) {

       this.first = first;

   }

   public T getSecond() {

       return second;

   }

   public void setSecond(T second) {

       this.second = second;

   }

  

   @Override

   public boolean equals(Object obj) {

       return first.equals(second);

   }

}