Question: JAVA problem2_getElements Make the Measurer interface from the week 5 demo code into a generic interface . Add to the interface one static method: T

JAVA

problem2_getElements

Make the Measurer interface from the week 5 demo code into a generic interface. Add to the interface one static method:

T max(T[] values, Measurer meas)

that computes and returns the largest of the measures of given objects in the values array (or null if the array is empty). If multiple objects have the same measure, just return any one of them.

Examples: String[] words = { "Mary", "had", "a", "little", "lamb" };

// (using Lambda expression here for brevity in this example; Lambda expressions will be covered in week 10):

Measurer.max(words, s -> s.length()); // returns "little"

// this example uses java.awt.Rectangle:

Rectangle[] rects = { new Rectangle(1, 2, 10, 20), new Rectangle(5, 15, 17, 23),

new Rectangle(9, 15, 25, 22), new Rectangle(12, 8, 18, 23) };

// (using Lambda expression here for brevity in this example; Lambda expressions will be covered in week 10):

Measurer.max(rects, r -> r.getWidth() * r.getHeight()); // returns java.awt.Rectangle[x=9,y=15,width=25,height=22]

public interface Measurer { /** * Computes the measure of an object. * * @param anObject the object to be measured * @return the measure */ double measure(Object anObject); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!