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 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...
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 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...
Write a program in Java which performs the sort operation. The main method accepts ten numbers...
Write a program in Java which performs the sort operation. The main method accepts ten numbers in an array and passes that to the method sort. The method sort accepts and sorts the numbers in ascending and descending order. The method display shows the result. You can use integers or floating point numbers.
Write a complete Java program, including comments in both the main program and in each method,...
Write a complete Java program, including comments in both the main program and in each method, which will do the following: 0. The main program starts by calling a method named introduction which prints out a description of what the program will do. This method is called just once.      This method is not sent any parameters, and it does not return a value. The method should print your name. Then it prints several lines of output explaining what the...
Write a complete Java program, including comments in both the main program and in each method,...
Write a complete Java program, including comments in both the main program and in each method, which will do the following: 0. The main program starts by calling a method named introduction which prints out a description of what the program will do. This method is called just once.      This method is not sent any parameters, and it does not return a value. The method should print your name. Then it prints several lines of output explaining what the...
Write a complete Java program, including comments in both the main program and in each method,...
Write a complete Java program, including comments in both the main program and in each method, which will do the following: 0. The main program starts by calling a method named introduction which prints out a description of what the program will do. This method is called just once.      This method is not sent any parameters, and it does not return a value. The method should print your name. Then it prints several lines of output explaining what the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT