Hi, I need the answer of this question in Java and I need the program to be run
ID: 668265 • Letter: H
Question
Hi, I need the answer of this question in Java and I need the program to be run in the netbeans
please, I need the answer satisfy all the requirement and I need it today
20.8 (Generic Class Pair) Write a generic class Pair which has two type parameters—F and S—
each representing the type of the first and second element 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.8 In 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" method])
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
Provided java code done as per your requirements.
//Header file section
import java.util.Comparator;
//Class header
public class Pair<F extends Comparable<F>, S extends Comparable<S>>
{
//Declare variables
private F first;
private S second;
public int tv1Size;
public int tv2Size;
// Default Constructor
public Pair()
{
first = null;
second = null;
}
// Two argument constructor for data used to initialize "first" and "second"
public Pair(F fEle, S sEle)
{
first = fEle;
second = sEle;
}
// get and set methods
public F getFirst()
{
return first;
}
public S getSecond()
{
return second;
}
public void setFirst(F fEle)
{
first = fEle;
}
public void setSecond(S sEle)
{
second = sEle;
}
// "toString" method for displaying the "first" and "second" data elements
public String toString()
{
return "First: " + first + " Second: " + second;
}
//Creates a "Comparator" object
public final Comparator<Pair<F, S>> KEY_COMPARATOR = new Comparator<Pair<F, S>>()
{
@Override
public int compare(Pair<F, S> tv1, Pair<F, S> tv2)
{
return tv1.getFirst().compareTo(tv2.getFirst());
}
};
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.