Binary expressions in arithmetic o represent statements such as a report mapping
ID: 3597086 • Letter: B
Question
Binary expressions in arithmetic o represent statements such as a report mapping expression oper lambda expression. with two or tour times three." The simulate method prints out computed result. Invoke the simulate method with a add one mapping sst of wo oerads or terms joined by an operator. They are used to sio Show the output of your inwocation. rands with their amb interface Binaryoperator int yield(int a, int b): atatie void simulate(Binaryoperator op) for(int a-o, ace 2; a for(int b, b2:b+) sten. out . print f(*d-> id:n., a, b. op-Yield(a, b)); simulatet / your code hereExplanation / Answer
answer:
simulate((a,b) -> a+b);
Here the method simulate expects a BinaryOperator interface object as specified in quesiton. The interface has a method with 2 parameters a and b returning an int value. So we have used simulate() with takes 2 parameters and returns their sum. If we were to simulate multiplication , we can replace a+b with a*b.
The complete program on how to use the lamda expresion and output is shown below
interface BinaryOperator
{
int yield(int a , int b);
}
public class LamdaExample {
static void simulate(BinaryOperator op)
{
for(int a = 0; a <= 2; a++)
for(int b = 0; b <=2 ; b++)
System.out.printf("%d %d => %d ", a, b, op.yield(a, b));
}
public static void main(String[] args) {
simulate((a,b) -> a+b);
}
}
output
0 0 => 0
0 1 => 1
0 2 => 2
1 0 => 1
1 1 => 2
1 2 => 3
2 0 => 2
2 1 => 3
2 2 => 4
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.