Programming Exercises



Declare an interface called Function that has a method named evaluate that takes an int parameter and returns an int value.>

Create a class Half that implements Function. Make the implementation of the method evaluate() return the value obtained by dividing the int argument by 2.

In a client, create a method that takes an arbitrary array of int values as parameter and returns an array that has the same length, but the value of an element in the new array is half that of the value in the corresponding element in the array passed as parameter. Let the implementation of this method create an instance of Half and use this instance to calculate the values in the array to be returned.


Rewrite the method that operated on arrays from the previous exercise: the method should now also take a Function reference as an argument and use this instead of creating an instance of Half.

Create a class called Print that implements Function, has a method that simply prints the int value given as an argument, and returns the value.

Now, write a program that creates an array of the int values from 1 to 10 and does the following:

  1. Prints the array using an instance of the Print class and the method described earlier.

  2. Halves the values in the array and prints the values again, using the Half and Print classes and the method described above.