In: Computer Science
Make the Measurable interface from Chapter 10 into a generic class. Provide a static method that returns the largest element of an ArrayList, provided that the elements are instances of Measurable. Be sure to return a value of type T.
Measurable interface:
public interface Measurable {
double getMeasure();
}
Using JAVA please
import java.util.ArrayList; import java.util.Arrays; public class LargestMeasurable { public static <T extends Measurable> T max(ArrayList<T> list) { if (list == null || list.isEmpty()) return null; T largest = list.get(0); for (int i = 0; i < list.size(); i++) { if (list.get(i).getMeasure() > largest.getMeasure()) { largest = list.get(i); } } return largest; } public static void main(String[] args) { Measurable[] arr = { new Measurable() { @Override public double getMeasure() { return 10; } }, new Measurable() { @Override public double getMeasure() { return 2; } }, new Measurable() { @Override public double getMeasure() { return 7; } }, new Measurable() { @Override public double getMeasure() { return 9; } }, }; System.out.println(max(new ArrayList<>(Arrays.asList(arr))).getMeasure()); } }