Question: fix the code to match directions. The directions are commented above the code provided below ------------------------------------------------------------------------------------------------------------------------------------------ // LambdaTester2.java /* Design a Java program named

fix the code to match directions. The directions are commented above the code provided below

 


------------------------------------------------------------------------------------------------------------------------------------------


// LambdaTester2.java

/* Design a Java program named LambdaTester2 which includes a main method that declares a lambda expression reference which implements the following interface:

interface Multiplier { int multiply(int num1, int num2); }

The program must declare an array of Pair objects (see class specification below), followed by the declaration of an ArrayList which is instantiated using the declared array (the ArrayList stores Pair objects). Hint: the asList method of the java.util.Arrays class will help here.

Then write loop which iterates through each element of the ArrayList, calls the lambda expression reference with the element fields as arguments and displays the resulting multiplied value.

The Pair class is implemented as follows, include it with default (not public) visibility in the same source file as your LambdaTester2 class.

class Pair { private int num1, num2; public Pair() {} public Pair(int num1, int num2) { this.num1 = num1; this.num2 = num2; } public int getNum1() { return num1; } public int getNum2() { return num2; } }

Sample output follows for a Pair array declared as

Pair[] pArray = { new Pair(2, 4), new Pair(3, 6), new Pair(4, 7) }; Remember that your multiplied values must come from an ArrayList created from an array similar to the one shown above. Multiply 2 * 4 = 8 Multiply 3 * 6 = 18 Multiply 4 * 7 = 28 */

import java.util.Arrays; import java.util.ArrayList;

interface Multiplier { int multiply(int num1, int num2); }

public class lambdaTester2 { public static void main(String[] args) { Pair[] pArray = { new Pair(2, 4), new Pair(3, 6), new Pair(4, 7) }; ArrayList pList = new ArrayList<>(Arrays.asList(pArray)); SimplePair sp = val -> val * 4.0; for (int i = 0; i < pList.size(); i++) { Pair p = pList.get(i); System.out.println("Double of " + p + ": " + sp.pairValue(p)); }

} }

class Pair { private int num1, num2; public Pair() {} public Pair(int num1, int num2) { this.num1 = num1; this.num2 = num2; } public int getNum1() { return num1; } public int getNum2() { return num2; } }

Step by Step Solution

3.37 Rating (147 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

import javautilArrayList import javautilArrays interface Multiplier int multiplyint num1 int num2 pu... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!