Question
7.1. a. Implement a generic class Pair that stores pairs of (key, value) pairs. Usage example: Pair p = new Pair (1, one); System.out.println(The string
7.1.
a. Implement a generic class Pair
Usage example: Pair
A pair object has a constructor
public Pair(K k, V v) {...}
has accessors:
public K k() {...} public V v() {...}
and overrides these methods inherited from class Object:
public boolean equals(Object obj) {...} public int hashCode() {...} public String toString() {...} public Object clone() {...}
Your class must be immutable, serializable, and cloneable. It is OK to return a shallow clone. Follow the guidelines from the textbook when implementing the above functions.
b. Write a PairTest class with method main() where you test: - equality with equals() - cloning : make a clone and compare with the original - serialization. Serialize to an ObjectStream backed by a file and then load the object back to memory and compare it to the original with equals(). - hashCode(). Must be compatible with equals (i.e. equal hashCode() for equal objects, and different values (with high probability) for different key or/and value objects
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started