19 (6 pts) Java has interfaces Comparable and Comparator. As discussed in class,
ID: 3873743 • Letter: 1
Question
19 (6 pts) Java has interfaces Comparable and Comparator. As discussed in class, what is the main advantage of Comparator? (6 pts) We can use a cmraorin our owrn code, but often we use u comarators with what useful Jaa ? (6 pts) What is the signature (name and parameters) of the method that all Comparator objects must have? (6 pts) What is the signature (name and parameters) of the method that all Comparable objects must have? (6 pts) If we wish to sort some class C using Comparable, then the method required by Comparable will be located (10 pts) Suppose you want to control whether two objects of class X are considered duplicates by a HashSet (a) Write the signature (visibility, name, parameters, types) of the method(s) required (b) Explain very briefly the circumstances under which they would be called. You will not need more space.Explanation / Answer
Answer 1:
Comparator: Objects can be sorted based on more than one fields of a class.
Comparable: allows you to sort items in a collections based on only one field.
Answer 3: Comparator
@Override
public int compare(Sample o1, Sample o2) {
return 0;
}
Answer 4: Comparable
@Override
public int compareTo(Sample o) {
return 0;
}
Answer 5:
method definition should be in the class C
Like
class c implements Comparable<C>{
@Override
public int compareTo(C o) {
return 0;
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.