Assignment #3: Interfaces, Function Objects, Generics

Function Objects, Without Generics

Method transform takes two identically sized-arrays of double as parameters: input and output, and a third parameter representing a function to be applied to the input array. For instance, in the following code fragment:
   double [ ] input = { 1.0, -3.0, 5.0 };
   double [ ] output1 = new double [ 3 ];
   double [ ] output2 = new double [ 3 ];
   double [ ] output3 = new double [ 4 ];

   transform( input, output1, new ComputeSquare( ) );
   transform( input, output2, new ComputeAbsoluteValue( ) );
   transform( input, output3, new ComputeSquare( ) );
The intended result is that output1 contains { 1.0, 9.0, 25.0 }, output2 contains { 1.0, 3.0, 5.0 }, and the third call to transform throws an IllegalArgumentException because the arrays have different sizes. Implement the following components:
  1. An interface that will be used to specify the third parameter to transform.
  2. The transform method (which is a static method). Remember to throw an exception if the input and output arrays are not identically-sized.
  3. The classes ComputeSquare and ComputeAbsoluteValue.

Function Objects, With Generics

Method transform takes two identically sized-arrays as parameters: input and output, and a third parameter representing a function to be applied to the input array. The input array is of generic type InputType and the output array is of generic type OutputType. For instance, in the following code fragment:
   String [ ] input = { "hello", "world", "Zoo" };
   String [ ] output1 = new String [ 3 ];
   Integer [ ] output2 = new Integer [ 3 ];
   String [ ] output3 = new String [ 4 ];

   transform( input, output1, new ComputeUpperCase( ) );
   transform( input, output2, new ComputeLength( ) );
   transform( input, output3, new ComputeUpperCase( ) );
The intended result is that output1 contains { "HELLO", "WORLD", "ZOO" }, output2 contains { 5, 5, 3 }, and the third call to transform throws an IllegalArgumentException because the arrays have different sizes. Implement the following components:
  1. A generic interface that will be used to specify the third parameter to transform. The interface will have two type parameters.
  2. The transform method (which is a generic static method). Remember to throw an exception if the input and output arrays are not identically-sized.
  3. The classes ComputeUpperCase and ComputeLength.