      1  /**

      2     This program demonstrates the measurable BankAccount and Country classes.

      3  */

      4  public class MeasurableDemo

      5  {

      6     public static void main(String[] args)

      7     {

      8        Measurable[] accounts = new Measurable[3];

      9        accounts[0] = new BankAccount(0);

     10        accounts[1] = new BankAccount(10000);

     11        accounts[2] = new BankAccount(2000);

     12

     13        System.out.println("Average balance: "

     14           + average(accounts));

     15

     16        Measurable[] countries = new Measurable[3];

     17        countries[0] = new Country("Uruguay", 176220);

     18        countries[1] = new Country("Thailand", 514000);

     19        countries[2] = new Country("Belgium", 30510);

     20

     21        System.out.println("Average area: "

     22           + average(countries));

     23     }

     24

     25     /**

     26        Computes the average of the measures of the given objects.

     27        @param objs an array of Measurable objects

     28        @return the average of the measures

     29     */

     30     public static double average(Measurable[] objs)

     31     {

     32        if (objs.length == 0) { return 0; }

     33        double sum = 0;

     34        for (Measurable obj : objs)

     35        {

     36           sum = sum + obj.getMeasure();

     37        }

     38        return sum / objs.length;

     39     }

     40  }