Programming Exercises


graphics/pe_icon.gif
6.1

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.

6.2

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.



A Programmer[ap]s Guide to Java Certification
A Programmer[ap]s Guide to Java Certification
ISBN: 201596148
EAN: N/A
Year: 2003
Pages: 284

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net