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

Here is the Data class code: package Unit5; public class Data { /** Computes the

ID: 3554889 • Letter: H

Question

Here is the Data class code:

package Unit5;

public class Data
{
/**
Computes the average of the measures of the given objects.
@param objects an array of Measurable objects
@return the average of the measures
*/
public static double average(Measurable[] objects)
{
double sum = 0;
for (Measurable obj : objects)
{
sum = sum + obj.getMeasure();
}
if (objects.length > 0) { return sum / objects.length; }
else { return 0; }
}
  
}

The assignment is to:

Add a method
         public static Measurable max(Measurable[] objects)
to the Data class (given in the example files) that returns the object with the largest measure.

A person has a name and height in centimeters. Use the Data class of Exercise 1 to process an array of Person objects. Display the average height and the name of the tallest person.

Add a method to the Data class the returns an object with the largest measure, as measured by the supplied measurer:
      public static Object max(Objects[] objects, Measurer m)

Explanation / Answer

package Unit5;

public class Data {

/**
* Computes the average of the measures of the given objects.
*
* @param objects an array of Measurable objects
* @return the average of the measures
*/
public static double average(Measurable[] objects) {
double sum = 0;
for (Measurable obj : objects) {
sum = sum + obj.getMeasure();
}
if (objects.length > 0) {
return sum / objects.length;
} else {
return 0;
}
}

public static Measurable max(Measurable[] objects) {
double max = objects[0].getMeasure();
int index;
for (int i = 0; i < objects.length; i++) {
if (max < objects[i].getMeasure()) {
max = objects[i].getMeasure();
index = i;
}
}
return objects[index];
}

public static void printMaxHeightAndName(Person[] objects) {
double sum = 0;
double max = objects[0].getHeight();
String name = objects[0].getName();
for (int i = 0; i < objects.length; i++) {
sum = sum + objects[i].getHeight();
if (max < objects[i].getHeight()) {
max = objects[i].getHeight();
name = objects[i].getName();
}
}
if (objects.length > 0) {
System.out.println("Average height is: " + sum / objects.length);
System.out.println("Person with maximum height is: " + name);
} else {
System.out.println("Average height is: zero");
}
}

public static Object max(Objects[] objects, Measurer m) {
double max = objects[0].getMeasure(m);
int index=0;
for (int i = 0; i < objects.length; i++) {
if (max < objects[i].getMeasure(m)) {
max = objects[i].getMeasure(m);
index = i;
}
}
return objects[index];
}
}