Measurable.java
public interface Measurable
{
double getMeasurement();
/**
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.0;
for (Measurable obj : objects)
{
sum = sum + obj.getMeasurement();
}
if (objects.length > 0)
{
return sum / objects.length;
} else {
return 0.0;
}
}
/**
Finds the maximum of the measurements of the given objects.
@param objects an array of Measurable objects
@return the maximum of the measures, null if array is empty
*/
public static Measurable max(Measurable[] objects)
{
// WRITE CODE HERE
return null;
}
}
MeasurableTester.java
/**
This program demonstrates the measurable Country class.
*/
import java.util.*;
public class MeasurableTester
{
public static void main(String[] args)
{
Measurable[] countries = new Measurable[3];
countries[0] = new Country("Uruguay", 176220);
countries[1] = new Country("Thailand", 513120);
countries[2] = new Country("Belgium", 30510);
for(Measurable c: countries)
System.out.println(c);
System.out.println("nThe average surface are of these countries is: " +
Measurable.average(countries));
System.out.println("Expected: 239950.0");
/* ---------------- PART 2 ----------------------
Measurable maxCountry = Measurable.max(countries);
System.out.println("nMaximum area: " + maxCountry.getMeasurement());
System.out.println("Expected: 513120");
*/
/* ---------------- PART 3 ----------------------
Arrays.sort(countries);
for(Measurable c: countries)
System.out.println(c);
*/
}
}