Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using the Comparable Interface Write a class Compare3 that provides a static met

ID: 3728631 • Letter: U

Question

Using the Comparable Interface Write a class Compare3 that provides a static method largest. The method takes three parameters of Comparable type and returns the largest of the three (that is, the return type should be also Comparable). Use the compareTo method in the largest method to compare the passed values. Recall that the method compareTo is part of the Comparable interface, so largest can use the compareTo method of its parameters to compare them. Write a class Comparisons that has the main method for testing your largest method. . First prompt the user for and read in three strings, use your largest method to find the largest of the three strings, and print it out. (It's easiest to put the call to largest directly in the call to println, Add code to prompt the user for three integers and try to use your largest method to find the largest of the three integers. Does this work? If it does, it's thanks to autoboxing, which is automatic conversion of ints to Integers °

Explanation / Answer

public class Compare3 { public static T largest(T t1, T t2, T t3) { T largest = t1; if(t2.compareTo(largest) > 0) { largest = t2; } if(t3.compareTo(largest) > 0) { largest = t3; } return largest; } }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote