Question
/** In this class, you will define a static method that calculates the average of the measure value of a set of objects that are
/**
In this class, you will define a static method that calculates the average
of the measure value of a set of objects that are Measurable.
*/
public class AveragePersonHeights
{
/**
Calculates the average measure from a collection of Measurable object
@param objects the objects
@return the average measure of objects
*/
public static double average(Measurable[] objects)
{
//-----------Start below here. To do: approximate lines of code = 6
// loop through the array of Measurable objects and add their measure to a total
// compute and return the average measure
// Hint: check to see if the length of the array is > 0 first. If it is == 0, return 0.0
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
}
public static void main(String[] args)
{
System.out.println("--- TEST 1: Calculate the average in a standard case");
APerson[] people = new APerson[4];
people[0] = new APerson("Larry", 65);
people[1] = new APerson("Susan", 45);
people[2] = new APerson("Joe", -45);
people[3] = new APerson("", 0);
System.out.printf("RESULT: %.2f ", average(people));
System.out.println("EXPECTED: 16.25");
System.out.println(" --- TEST 2: Calculate the average for empty array");
System.out.printf("RESULT: %.2f ", average(new APerson[0]));
System.out.println("EXPECTED: 0.00");
}
}
******************************************************************************************************************************************
/**
Basic person with name and height. You are to complete the following:
- Define the class and make it implement Measurable
- Implement getMeasure to return the height
*/
//-----------Start below here. To do: approximate lines of code = 1
//
// Define the class named APerson and make it implement Measurable
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
{
private String name;
private int height;
/**
Create person with a given name and height in centimeters.
@param name person's name
@param height person's height in centimeters
*/
public APerson(String name, int height)
{
this.name = name;
this.height = height;
}
//-----------Start below here. To do: approximate lines of code = 2
//
// Add the getMeasure method to return the height. See Measurable for the
// method signature
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
}
****************************************************************************************************************************************
public interface Measurable
{
double getMeasure();
/**
Computes the smallest of the measures of the given objects.
@param objects an array of Measurable objects
@return the object with the smallest measure, null if array is empty
*/
static Measurable smallest(Measurable[] objects)
{
if (objects.length == 0)
{
return null;
}
Measurable min = objects[0];
for (Measurable obj : objects)
{
if (obj.getMeasure() < min.getMeasure())
{
min = obj;
}
}
return min;
}
/**
Computes the largest of the measures of the given objects.
@param objects an array of Measurable objects
@return the object with the largest measure, null if array is empty
*/
static Measurable largest(Measurable[] objects)
{
if (objects.length == 0)
{
return null;
}
Measurable max = objects[0];
for (Measurable obj : objects)
{
if (obj.getMeasure() > max.getMeasure())
{
max = obj;
}
}
return max;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started