public class Lab6 { // an interface for entities which have a value public static interface Valuable { public int getValue(); } // a simple class which implements the Valuable interface. // its value is the value of the number. public static class ValNum implements Valuable { private int value; public ValNum(int value) { this.value = value; } public int getValue() { return value } public void setValue(int value) { this.value = value; } } // another class with a value: this contains an array, and its // value is the sum of its positive elements (negative ones are ignored) public static class TotalArray implements Valuable { private int[] arr; public TotalArray(int[] arr) { this.arr = arr; } public int getValue() { // this will sum up every positive number in arr, // and return the total sum int sum = 0; int i = 0; while (arr[i] > 0 && i < arr.length) { sum += arr[i]; i++; } } } public static void main(String[] args) { // declare three variables: a value-number and two value-arrays ValNum num = new ValNum(5); TotalArray tarr1 = new TotalArray( new int[]{1, 2, 3} ); TotalArray tarr2 = new TotalArray( new int[]{1, 2, -3, 2, 1} ); // a list of valuables containing the number and both arrays from above Valuable[] valuables = { num, tarr1, tarr2 }; // print what's inside of each for (Valuable v : valuables) System.out.println( v.getValue() ); } }