diff --git a/core-java-modules/java-numbers-5/README.md b/core-java-modules/java-numbers-5/README.md new file mode 100644 index 0000000000..7d843af9ea --- /dev/null +++ b/core-java-modules/java-numbers-5/README.md @@ -0,0 +1 @@ +### Relevant Articles: diff --git a/core-java-modules/java-numbers-5/pom.xml b/core-java-modules/java-numbers-5/pom.xml new file mode 100644 index 0000000000..f5805981ae --- /dev/null +++ b/core-java-modules/java-numbers-5/pom.xml @@ -0,0 +1,25 @@ + + 4.0.0 + java-numbers-5 + java-numbers-5 + jar + + + com.baeldung.core-java-modules + core-java-modules + 0.0.1-SNAPSHOT + + + + java-numbers-5 + + + src/main/resources + true + + + + + \ No newline at end of file diff --git a/core-java-modules/java-numbers-5/src/main/java/com/baeldung/evenodd/EvenOdd.java b/core-java-modules/java-numbers-5/src/main/java/com/baeldung/evenodd/EvenOdd.java new file mode 100644 index 0000000000..5323dcbe11 --- /dev/null +++ b/core-java-modules/java-numbers-5/src/main/java/com/baeldung/evenodd/EvenOdd.java @@ -0,0 +1,46 @@ +package com.baeldung.evenodd; + +public class EvenOdd { + + static boolean isEven(int x) { + return x % 2 == 0; + } + + static boolean isOdd(int x) { + return x % 2 == 1; + } + + static boolean isOrEven(int x) { + return (x | 1) > x; + } + + static boolean isOrOdd(int x) { + return (x | 1) == x; + } + + static boolean isAndEven(int x) { + return (x & 1) == 0; + } + + static boolean isAndOdd(int x) { + return (x & 1) == 1; + } + + static boolean isXorEven(int x) { + return (x ^ 1) > x; + } + + static boolean isXorOdd(int x) { + return (x ^ 1) < x; + } + + static boolean isLsbEven(int x) { + return Integer.toBinaryString(x) + .endsWith("0"); + } + + static boolean isLsbOdd(int x) { + return Integer.toBinaryString(x) + .endsWith("1"); + } +} diff --git a/core-java-modules/java-numbers-5/src/test/java/com/baeldung/evenodd/EvenOddUnitTest.java b/core-java-modules/java-numbers-5/src/test/java/com/baeldung/evenodd/EvenOddUnitTest.java new file mode 100644 index 0000000000..82810977ba --- /dev/null +++ b/core-java-modules/java-numbers-5/src/test/java/com/baeldung/evenodd/EvenOddUnitTest.java @@ -0,0 +1,68 @@ +package com.baeldung.evenodd; + +import static com.baeldung.evenodd.EvenOdd.isAndEven; +import static com.baeldung.evenodd.EvenOdd.isAndOdd; +import static com.baeldung.evenodd.EvenOdd.isEven; +import static com.baeldung.evenodd.EvenOdd.isLsbEven; +import static com.baeldung.evenodd.EvenOdd.isLsbOdd; +import static com.baeldung.evenodd.EvenOdd.isOdd; +import static com.baeldung.evenodd.EvenOdd.isOrEven; +import static com.baeldung.evenodd.EvenOdd.isOrOdd; +import static com.baeldung.evenodd.EvenOdd.isXorEven; +import static com.baeldung.evenodd.EvenOdd.isXorOdd; +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class EvenOddUnitTest { + + @Test + public void whenNumberIsEven_thenReturnTrue() { + assertEquals(true, isEven(2)); + } + + @Test + public void whenNumberIsOdd_thenReturnTrue() { + assertEquals(true, isOdd(3)); + } + + @Test + public void whenNumberIsEven_thenReturnTrueWithOr() { + assertEquals(true, isOrEven(4)); + } + + @Test + public void whenNumberIsOdd_thenReturnTrueOr() { + assertEquals(true, isOrOdd(5)); + } + + @Test + public void whenNumberIsEven_thenReturnTrueAnd() { + assertEquals(true, isAndEven(6)); + } + + @Test + public void whenNumberIsOdd_thenReturnTrueAnd() { + assertEquals(true, isAndOdd(7)); + } + + @Test + public void whenNumberIsEven_thenReturnTrueXor() { + assertEquals(true, isXorEven(8)); + } + + @Test + public void whenNumberIsOdd_thenReturnTrueXor() { + assertEquals(true, isXorOdd(9)); + } + + @Test + public void whenNumberIsEven_thenReturnTrueLsb() { + assertEquals(true, isLsbEven(10)); + } + + @Test + public void whenNumberIsOdd_thenReturnTrueLsb() { + assertEquals(true, isLsbOdd(11)); + } +} diff --git a/core-java-modules/pom.xml b/core-java-modules/pom.xml index 8d30c545e2..c2576d84f1 100644 --- a/core-java-modules/pom.xml +++ b/core-java-modules/pom.xml @@ -127,6 +127,7 @@ java-numbers-2 java-numbers-3 java-numbers-4 + java-numbers-5