Menu

[Solved]Designing Java Collection Class Use Generics Class Called Itempair Objects Class Hold Pair Q37224153

You are designing a Java collection class that will usegenerics. The class is called ItemPair. Objects of this class holda pair of two items that are the same type.

For example, an ItemPair can hold two Integers or two Strings ortwo Students, etc. An ItemPair could not hold anInteger and a String.

Write the complete class using generics.Include:

  • the class header
  • instance data
  • a constructor that takes both items in as parameters
  • getters and setters for each item in the pair
  • a toString method
  • a sameItemsPair method

The sameItemsPair determines whether the two items in a pair arelogically equivalent to each other. The method header is:

public boolean sameItemsPair()=====================

Use the driver below to test your code DO NOT ADD TO THE DRIVERCOURSEc!!!

//Test Driver

import java.util.*;

public class HomeworkM12Driver {

public static void main(String[] args) {

// UN-COMMENT TO TEST YOUR ITEMPAIR CLASS

System.out.println(“n”);

ItemPair numberPair = new ItemPair(1,

2);

System.out.println(numberPair);

System.out.println(“Same items? false: ” +

numberPair.sameItemsPair());

numberPair.setItem2(1);

System.out.println(“Same items? true: ” +

numberPair.sameItemsPair());

ItemPair wordPair = new ItemPair(“hello”,

“bye”);

System.out.println(wordPair);

System.out.println(“Same items? false: ” +

wordPair.sameItemsPair());

wordPair.setItem1(new String(“bye”));

System.out.println(“Same items? true: ” +

wordPair.sameItemsPair());

// these lines should not compile!!

// numberPair.setItem(“hello”);

//wordPair.setItem1(4);

}

Expert Answer


Answer to You are designing a Java collection class that will use generics. The class is called ItemPair. Objects of this class ho… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *