Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core-java-modules/java-numbers-5/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
### Relevant Articles:
25 changes: 25 additions & 0 deletions core-java-modules/java-numbers-5/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>java-numbers-5</artifactId>
<name>java-numbers-5</name>
<packaging>jar</packaging>

<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<build>
<finalName>java-numbers-5</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
1 change: 1 addition & 0 deletions core-java-modules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
<module>java-numbers-2</module>
<module>java-numbers-3</module>
<module>java-numbers-4</module>
<module>java-numbers-5</module>
</modules>

<dependencyManagement>
Expand Down