File tree Expand file tree Collapse file tree
core-java-modules/core-java-numbers-5/src
main/java/com/baeldung/positivenegative
test/java/com/baeldung/positivenegative Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .baeldung .positivenegative ;
2+
3+ public class PositiveOrNegative {
4+ enum Result {
5+ POSITIVE , NEGATIVE , ZERO
6+ }
7+
8+ public static Result byOperator (Integer integer ) {
9+ if (integer > 0 ) {
10+ return Result .POSITIVE ;
11+ } else if (integer < 0 ) {
12+ return Result .NEGATIVE ;
13+ }
14+ return Result .ZERO ;
15+ }
16+
17+ public static Result bySignum (Integer integer ) {
18+ int result = Integer .signum (integer );
19+
20+ if (result == 1 ) {
21+ return Result .POSITIVE ;
22+ } else if (result == -1 ) {
23+ return Result .NEGATIVE ;
24+ }
25+ return Result .ZERO ;
26+ }
27+
28+ public static Result bySignum (Float floatNumber ) {
29+ float result = Math .signum (floatNumber );
30+
31+ if (result == 1.0f ) {
32+ return Result .POSITIVE ;
33+ } else if (result == -1.0f ) {
34+ return Result .NEGATIVE ;
35+ }
36+ return Result .ZERO ;
37+ }
38+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .positivenegative ;
2+
3+ import org .junit .jupiter .api .Test ;
4+
5+ import static com .baeldung .positivenegative .PositiveOrNegative .Result .*;
6+ import static org .junit .jupiter .api .Assertions .assertEquals ;
7+
8+ class PositiveOrNegativeUnitTest {
9+ @ Test
10+ void givenIntegers_whenChkPositiveOrNegativeByOperator_thenReturnExpectedResult () {
11+ assertEquals (POSITIVE , PositiveOrNegative .byOperator (42 ));
12+ assertEquals (ZERO , PositiveOrNegative .byOperator (0 ));
13+ assertEquals (NEGATIVE , PositiveOrNegative .byOperator (-700 ));
14+ }
15+
16+ @ Test
17+ void givenIntegers_whenChkPositiveOrNegativeBySignum_thenReturnExpectedResult () {
18+ assertEquals (POSITIVE , PositiveOrNegative .bySignum (42 ));
19+ assertEquals (ZERO , PositiveOrNegative .bySignum (0 ));
20+ assertEquals (NEGATIVE , PositiveOrNegative .bySignum (-700 ));
21+ }
22+
23+ @ Test
24+ void givenFloats_whenChkPositiveOrNegativeBySignum_thenReturnExpectedResult () {
25+ assertEquals (POSITIVE , PositiveOrNegative .bySignum (4.2f ));
26+ assertEquals (ZERO , PositiveOrNegative .bySignum (0f ));
27+ assertEquals (NEGATIVE , PositiveOrNegative .bySignum (-7.7f ));
28+ }
29+ }
You can’t perform that action at this time.
0 commit comments