Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Rewrite the applyFunction method from the Module 7 Ungraded Practice Exercise solution that operated on int values. The method should now also accept a Function


Rewrite the applyFunction method from the Module 7 Ungraded Practice Exercise solution that operated on int values. The method should now also accept a Function reference as an argument, and use this argument instead of creating an instance of the Half class. The new method signature looks like this:

public static int[] applyFunction(Function func, int... arrIn) { Create class called Print that implements the method evaluate() in the Function interface. This method prints the int value passed as an argument, followed by a concatenated space character " ", and returns the same argument it was passed.

Now write program that :

Prints an arbitrary number of int values using an instance of the Print class and the method described earlier Halves the values in the array and prints the value again, using the Half and Print classes and the method described earlier. */

/* Sample output (this time calling the method applyFunction with a Function reference argument which just prints a value, followed by calling the method applyFunction again but this time with a Function reference which halves the values).

The first call to the modified applyFunction looks like this:

int[] myArr = applyFunction(print, 2 ,4, 6, 8); where an instance of the Print class named "print" has been instantiated. The second call can use the myArr array reference returned by this call and pass an instance of the Half class which contains the evaluate method which halves its arguments.

Original values: 2 4 6 8

Halved values: 1 2 3 4 */

interface Function { int evaluate(int arg); class Half implements Function { @Override public int evaluate(int arg) { return arg/2; } } class print { int evaluate(int arg); } }

public class client { public static int[] applyFunction(Function func, int... arrIn) { int length = arrIn.length; int[] arrOut = new int[length]; // call evaluate for all elements of arrIn for (int i = 0; i < length; i++) { arrOut[i] = func.evaluate(arrIn[i]); } return arrOut; } public static void main(String[] args) { // halve the values int[] myArr = applyFunction(print, 2, 4, 6, 8); System.out.println("The first call to the modified applyFunction looks like this: "); for (int value : myArr) { System.out.print(value + " "); } System.out.println(); } }



Need help getting expected output using instructions.. Instructions are commented above code.

Step by Step Solution

3.42 Rating (152 Votes )

There are 3 Steps involved in it

Step: 1

To achieve the expected output we need to make several modifications to the provided code Implement ... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Elements Of Chemical Reaction Engineering

Authors: H. Fogler

6th Edition

013548622X, 978-0135486221

More Books

Students also viewed these Programming questions