0% found this document useful (0 votes)
2 views1 page

Functional Interface

The document defines a functional interface 'MathOperation' for performing mathematical operations. It implements a 'LambdaCalculator' class that uses lambda expressions for addition, subtraction, multiplication, and division, with a safety check for division by zero. The main method demonstrates the usage of these operations with example outputs.

Uploaded by

pranavstudy173
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

Functional Interface

The document defines a functional interface 'MathOperation' for performing mathematical operations. It implements a 'LambdaCalculator' class that uses lambda expressions for addition, subtraction, multiplication, and division, with a safety check for division by zero. The main method demonstrates the usage of these operations with example outputs.

Uploaded by

pranavstudy173
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

@FunctionalInterface

interface MathOperation {

int operate(int a, int b);

public class LambdaCalculator {

public static void main(String[] args) {

// Addition lambda

MathOperation addition = (a, b) -> a + b;

// Subtraction lambda

MathOperation subtraction = (a, b) -> a - b;

// Multiplication lambda

MathOperation multiplication = (a, b) -> a * b;

// Division lambda with block syntax for safety

MathOperation division = (a, b) -> {

if (b == 0) return 0;

return a / b;

};

[Link]("10 + 5 = " + [Link](10, 5)); // Output: 15

[Link]("10 / 0 = " + [Link](10, 0)); // Output: 0

You might also like