Question

In: Computer Science

Design a Java program named LambdaTester2 which includes a main method that declares a lambda expression...

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).

Then write a 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

Solutions

Expert Solution

Explanation: I have written the lambda expression in the main method of the LambdaTester2 class,I have also instantiated the ArrayList with the given array of three Pair objects.I have used the for each loop to iterate over the ArrayList and call the multiply() method from the lambda expression from each object. I have also shown the output of the program, please find it attached with the answer.Please upvote if you liked my answer and comment of you need any explanation or modification.

//code starts

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

//Pair 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;
   }
}

//LambdaTester2 class

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

class LambdaTester2 {

   public static void main(String[] args) {
       // lambda expression
       Multiplier multiplier = (num1, num2) -> {
           return num1 * num2;
       };
       // array declaration
       Pair[] pArray = { new Pair(2, 4), new Pair(3, 6), new Pair(4, 7) };
       // declaration of an ArrayList which is instantiated using the declared array
       List<Pair> pairs = new ArrayList<>();
       pairs = Arrays.asList(pArray);
       for (Pair pair : pairs) {
           System.out.println("Multiply " + pair.getNum1() + " * " + pair.getNum2() + " = "
                   + multiplier.multiply(pair.getNum1(), pair.getNum2()));
       }
   }
}

Output:


Related Solutions

Java Write a program that declares a constant named QUARTS_IN_GALLON which holds the number of quarts...
Java Write a program that declares a constant named QUARTS_IN_GALLON which holds the number of quarts in a gallon (4). Also declare a variable named quartsNeeded to represent the number of quarts needed for a painting job, and assign an appropriate value. Compute and display the number of gallons and quarts needed for the job. Display explanatory text with the values—for example, A job that needs 18 quarts requires 4 gallons plus 2 quarts. When submitting the program for testing/grading,...
Java Programming Create a class named Problem1, and create a main method, the program does the...
Java Programming Create a class named Problem1, and create a main method, the program does the following: - Prompt the user to enter a String named str. - Prompt the user to enter a character named ch. - The program finds the index of the first occurrence of the character ch in str and print it in the format shown below. - If the character ch is found in more than one index in the String str, the program prints...
Write a program that contains a main method and another method named userName. The main method...
Write a program that contains a main method and another method named userName. The main method should prompt the user to enter a full name. The userName method takes the full name as an argument and prints the name in reverse order and returns the number of characters in the name. See Sample Output (input shown in blue). Sample Output Please enter your FULL name Billy Joe McCallister Here is the name Billy Joe McCallister in reverse: retsillaCcM eoJ ylliB...
Create a class named TestLease whose main() method declares four Lease objects. Call a getData() method...
Create a class named TestLease whose main() method declares four Lease objects. Call a getData() method three times. Within the method, prompt a user for values for each field for a Lease, and return a Lease object to the main() method where it is assigned to one of main()’s Lease objects. Do not prompt the user for values for the fourth Lease object, but let it continue to hold the default values. Then, in main(), pass one of the Lease...
Java program Create a public method named saveData for a class named Signal that will hold...
Java program Create a public method named saveData for a class named Signal that will hold digitized acceleration data. Signal has the following field declarations private     double timeStep;               // time between each data point     int numberOfPoints;          // number of data samples in array     double [] acceleration = new double [1000];          // acceleration data     double [] velocity = new double [1000];        // calculated velocity data     double [] displacement = new double [1000];        // calculated disp...
Write a program named MyHometown_Icon.java. The program will be an application (i.e have a main method)....
Write a program named MyHometown_Icon.java. The program will be an application (i.e have a main method). It will be in the default package. It will import edu.wiu.StdDraw. Its main method will make calls to methods in StdDraw to draw something iconic about your hometown. Multiple colors should be used.Multiple primitive types will be used and the drawing will consists of more than 20 primitive shapes.
Write a C++ program that converts an infix expression, which includes (, ), +, -, *,...
Write a C++ program that converts an infix expression, which includes (, ), +, -, *, and / operations to postfix notation. The program should allow the user to enter an infix expression using lower case characters, then it will display the result of conversion on the screen. (Note: Use the STL stack class to accomplish the solution.).
Write a java program using the information given Design a class named Pet, which should have...
Write a java program using the information given Design a class named Pet, which should have the following fields (i.e. instance variables):  name - The name field holds the name of a pet (a String type)  type - The type field holds the type of animal that a pet is (a String type). Example values are “Dog”, “Cat”, and “Bird”.  age - The age field holds the pet’s age (an int type) Include accessor methods (i.e. get...
Write a complete Java program, including comments in each method and in the main program, to...
Write a complete Java program, including comments in each method and in the main program, to do the following: Outline: The main program will read in a group of three integer values which represent a student's SAT scores. The main program will call a method to determine if these three scores are all valid--valid means in the range from 200 to 800, including both end points, and is a multiple of 10 (ends in a 0). If the scores are...
Write a complete Java program, including comments in each method and in the main program, to...
Write a complete Java program, including comments in each method and in the main program, to do the following: Outline: The main program will read in a group of three int||eger values which represent a student's SAT scores. The main program will call a method to determine if these three scores are all valid--valid means in the range from 200 to 800, including both end points, and is a multiple of 10 (ends in a 0). If the scores are...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT