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 that stores pairs of (key, value) pairs.

Usage example: Pair p = new Pair<>(1, "one"); System.out.println("The string value of " + p.k() + " is " + p.v());

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!