Create a Java class using generics ItemPair Objects of this

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)

Solution

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

   }

}

 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 Inte
 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 Inte

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site