AveragePersonHeights.java import java.util.ArrayList; import java.util.Scanner; /** A class to read and calculate the average height of many
Question:
AveragePersonHeights.java
import java.util.ArrayList;
import java.util.Scanner;
/**
A class to read and calculate the average height of many people.
*/
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)
{
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.println("The average height is: " + average(people));
System.out.println("Expected:The average height is: 16.25");
}
}
APerson.java
/**
Basic person with name and height.
*/
public class APerson implements Measurable
{
private String name;
private int height;
/**
Create a 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;
}
/**
A person is measured by their height.
*/
public double getMeasure()
{
return height;
}
}
Measurable.java
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;
}
}
P.s: please besides the code, also include a screenshot of code, and the output to confirm code doesn't get edited out and the output satisfies the problem!!
Probability & Statistics for Engineers & Scientists
ISBN: 978-0130415295
7th Edition
Authors: Ronald E. Walpole, Raymond H. Myers, Sharon L. Myers, Keying