Write a generic class Pair which has two type parameters F and S, each represent
ID: 3786669 • Letter: W
Question
Write a generic class Pair which has two type parameters F and S, each represents the type of the first and second elements of the pair respectively. Add get and set methods for the first and second elements of the pair. [Hint: The class header should be public class Pair<F, S>.]
Create an application that provides a solution for problem 20.8In addition to requirements specified in the description. The class must satisfy the following:
> Default Constructor
> Two argument constructor for data used to initialize "first" and "second"
> "toString" method for displaying the "first" and "second" data elements
> Creates a "Comparator" object (to be used by the sort method; i.e. create an instance of a class that implements the interface [must override "compare" and "equals" methods])
> The application must create an ArrayList of 5 Pair objects and then display the contents in sorted order largest to smallest, based on the method used to compare "Pair" objects. The class only supports types that extend Number. Assume the "Pair" objects contain x, y coordinates. Therefore calculate the distance from a 0,0 coordinate to determine the sorting order (i.e., ascending, smallest distance to largest distance).
Explanation / Answer
public class Pair{ private firstThing first;//first member of pair private secondThing second;//second member of pair public Pair(firstThing first, secondThing second){ this.first = first; this.second = second; } public void setFirst(firstThing first){ this.first = first; } public void setSecond(secondThing second) { this.second = second; } public thing getFirst() { return this.first; } public thing getSecond() { return this.second; } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.