From 842a71ad92fc29fc7383cf125f42a23a6b9a690a Mon Sep 17 00:00:00 2001 From: Kai Yuan Date: Sun, 21 Aug 2022 04:46:14 +0200 Subject: [PATCH] Check if a number is positive or negative in Java (#12604) * Check if a number is positive or negative in Java * reformat single line if/else statements * Check if a number is positive or negative in Java --- .../positivenegative/PositiveOrNegative.java | 38 +++++++++++++++++++ .../PositiveOrNegativeUnitTest.java | 29 ++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 core-java-modules/core-java-numbers-5/src/main/java/com/baeldung/positivenegative/PositiveOrNegative.java create mode 100644 core-java-modules/core-java-numbers-5/src/test/java/com/baeldung/positivenegative/PositiveOrNegativeUnitTest.java diff --git a/core-java-modules/core-java-numbers-5/src/main/java/com/baeldung/positivenegative/PositiveOrNegative.java b/core-java-modules/core-java-numbers-5/src/main/java/com/baeldung/positivenegative/PositiveOrNegative.java new file mode 100644 index 0000000000..a580aa5ac5 --- /dev/null +++ b/core-java-modules/core-java-numbers-5/src/main/java/com/baeldung/positivenegative/PositiveOrNegative.java @@ -0,0 +1,38 @@ +package com.baeldung.positivenegative; + +public class PositiveOrNegative { + enum Result { + POSITIVE, NEGATIVE, ZERO + } + + public static Result byOperator(Integer integer) { + if (integer > 0) { + return Result.POSITIVE; + } else if (integer < 0) { + return Result.NEGATIVE; + } + return Result.ZERO; + } + + public static Result bySignum(Integer integer) { + int result = Integer.signum(integer); + + if (result == 1) { + return Result.POSITIVE; + } else if (result == -1) { + return Result.NEGATIVE; + } + return Result.ZERO; + } + + public static Result bySignum(Float floatNumber) { + float result = Math.signum(floatNumber); + + if (result == 1.0f) { + return Result.POSITIVE; + } else if (result == -1.0f) { + return Result.NEGATIVE; + } + return Result.ZERO; + } +} diff --git a/core-java-modules/core-java-numbers-5/src/test/java/com/baeldung/positivenegative/PositiveOrNegativeUnitTest.java b/core-java-modules/core-java-numbers-5/src/test/java/com/baeldung/positivenegative/PositiveOrNegativeUnitTest.java new file mode 100644 index 0000000000..4807e8b928 --- /dev/null +++ b/core-java-modules/core-java-numbers-5/src/test/java/com/baeldung/positivenegative/PositiveOrNegativeUnitTest.java @@ -0,0 +1,29 @@ +package com.baeldung.positivenegative; + +import org.junit.jupiter.api.Test; + +import static com.baeldung.positivenegative.PositiveOrNegative.Result.*; +import static org.junit.jupiter.api.Assertions.assertEquals; + +class PositiveOrNegativeUnitTest { + @Test + void givenIntegers_whenChkPositiveOrNegativeByOperator_thenReturnExpectedResult() { + assertEquals(POSITIVE, PositiveOrNegative.byOperator(42)); + assertEquals(ZERO, PositiveOrNegative.byOperator(0)); + assertEquals(NEGATIVE, PositiveOrNegative.byOperator(-700)); + } + + @Test + void givenIntegers_whenChkPositiveOrNegativeBySignum_thenReturnExpectedResult() { + assertEquals(POSITIVE, PositiveOrNegative.bySignum(42)); + assertEquals(ZERO, PositiveOrNegative.bySignum(0)); + assertEquals(NEGATIVE, PositiveOrNegative.bySignum(-700)); + } + + @Test + void givenFloats_whenChkPositiveOrNegativeBySignum_thenReturnExpectedResult() { + assertEquals(POSITIVE, PositiveOrNegative.bySignum(4.2f)); + assertEquals(ZERO, PositiveOrNegative.bySignum(0f)); + assertEquals(NEGATIVE, PositiveOrNegative.bySignum(-7.7f)); + } +}