1 /**
2 This program demonstrates the string measurer.
3 */
4 public class MeasurerDemo
5 {
6 public static void main(String[] args)
7 {
8 String[] words = { "Mary", "had", "a", "little", "lamb" };
9 Measurer strMeas = new StringMeasurer();
10 System.out.println("Average length: "
11 + average(words, strMeas));
12 }
13
14 /**
15 Computes the average of the measures of the given objects.
16 @param objs an array of objects
17 @param meas the measurer
18 @return the average of the measures
19 */
20 public static double average(Object[] objs, Measurer meas)
21 {
22 if (objs.length == 0) return 0;
23 double sum = 0;
24 for (Object obj : objs)
25 {
26 sum = sum + meas.measure(obj);
27 }
28 return sum / objs.length;
29 }
30 }