Question: Using Java please Generic Pair Class Define a Pair class which tracks a pair of like types such as a pair of Strings, pair of

Using Java please

Generic Pair Class

Define a Pair class which tracks a pair of like types such as a pair of Strings, pair of Integers, or pair of Files. The types of the two items should be identical and if generics are used appropriately, it should be impossible to create a mixed pair. Examples including the getFirst() and getSecond() are below.

> Pair = new Pair("hi","bye"); Invalid top level statement > Pair sp = new Pair("hi","bye"); > sp.toString() (hi,bye) > sp.getFirst() hi > String s = sp.getFirst(); > s hi > Integer i = sp.getFirst(); Static Error: Bad types in assignment: from String to Integer > Pair ip = new Pair(4,2); > ip (4,2) > ip.getSecond() 2 > Integer i = ip.getSecond(); > i 2 > Pair mixed = new Pair("hi",4); Static Error: No constructor in Pair matches this invocation Arguments: (String, int) Expected return type: Pair Candidate signatures: Pair(Integer, Integer)

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!