-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added example and test for functional interface.
- Loading branch information
1 parent
7c39259
commit c808ab5
Showing
2 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
...ctional-interfaces/src/main/java/io/reflectoring/function/custom/ArithmeticOperation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package io.reflectoring.function.custom; | ||
|
||
/** The arithmetic operation functional interface. */ | ||
public interface ArithmeticOperation { | ||
/** | ||
* Operates on two integer inputs to calculate a result. | ||
* | ||
* @param a the first number | ||
* @param b the second number | ||
* @return the result | ||
*/ | ||
int operate(int a, int b); | ||
} |
29 changes: 29 additions & 0 deletions
29
...nal-interfaces/src/test/java/io/reflectoring/function/custom/ArithmeticOperationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package io.reflectoring.function.custom; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class ArithmeticOperationTest { | ||
|
||
@Test | ||
void operate() { | ||
// Define operations | ||
ArithmeticOperation add = (a, b) -> a + b; | ||
ArithmeticOperation subtract = (a, b) -> a - b; | ||
ArithmeticOperation multiply = (a, b) -> a * b; | ||
ArithmeticOperation divide = (a, b) -> a / b; | ||
|
||
// Use the operations | ||
int addition = add.operate(10, 5); // Returns 15 | ||
int subtraction = subtract.operate(10, 5); // Returns 5 | ||
int multiplication = multiply.operate(10, 5); // Returns 50 | ||
int division = divide.operate(10, 5); // Returns 2 | ||
|
||
// Verify results | ||
assertEquals(15, addition, "Result of addition is not correct."); | ||
assertEquals(5, subtraction, "Result of subtraction is not correct."); | ||
assertEquals(50, multiplication, "Result of multiplication is not correct."); | ||
assertEquals(2, division, "Result of division is not correct."); | ||
} | ||
} |