Skip to content

Commit ade630d

Browse files
committed
BAEL-5560 - checking even and odd numbers
1 parent 633fd71 commit ade630d

File tree

6 files changed

+142
-1
lines changed

6 files changed

+142
-1
lines changed

core-java-modules/java-numbers-4/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010
- [Generate a Random Value From an Enum](https://www.baeldung.com/java-enum-random-value)
1111
- [Reverse a Number in Java](https://www.baeldung.com/java-reverse-number)
1212
- [Check if BigDecimal Value Is Zero](https://www.baeldung.com/java-bigdecimal-zero)
13-
- More articles: [[<-- prev]](../java-numbers-3)
13+
- More articles: [[<-- prev]](../java-numbers-3) [[next -->]](../java-numbers-5)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
### Relevant Articles:
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<artifactId>java-numbers-5</artifactId>
6+
<name>java-numbers-5</name>
7+
<packaging>jar</packaging>
8+
9+
<parent>
10+
<groupId>com.baeldung.core-java-modules</groupId>
11+
<artifactId>core-java-modules</artifactId>
12+
<version>0.0.1-SNAPSHOT</version>
13+
</parent>
14+
15+
<build>
16+
<finalName>java-numbers-5</finalName>
17+
<resources>
18+
<resource>
19+
<directory>src/main/resources</directory>
20+
<filtering>true</filtering>
21+
</resource>
22+
</resources>
23+
</build>
24+
25+
</project>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.evenodd;
2+
3+
public class EvenOdd {
4+
5+
static boolean isEven(int x) {
6+
return x % 2 == 0;
7+
}
8+
9+
static boolean isOdd(int x) {
10+
return x % 2 == 1;
11+
}
12+
13+
static boolean isOrEven(int x) {
14+
return (x | 1) > x;
15+
}
16+
17+
static boolean isOrOdd(int x) {
18+
return (x | 1) == x;
19+
}
20+
21+
static boolean isAndEven(int x) {
22+
return (x & 1) == 0;
23+
}
24+
25+
static boolean isAndOdd(int x) {
26+
return (x & 1) == 1;
27+
}
28+
29+
static boolean isXorEven(int x) {
30+
return (x ^ 1) > x;
31+
}
32+
33+
static boolean isXorOdd(int x) {
34+
return (x ^ 1) < x;
35+
}
36+
37+
static boolean isLsbEven(int x) {
38+
return Integer.toBinaryString(x)
39+
.endsWith("0");
40+
}
41+
42+
static boolean isLsbOdd(int x) {
43+
return Integer.toBinaryString(x)
44+
.endsWith("1");
45+
}
46+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.baeldung.evenodd;
2+
3+
import static com.baeldung.evenodd.EvenOdd.isAndEven;
4+
import static com.baeldung.evenodd.EvenOdd.isAndOdd;
5+
import static com.baeldung.evenodd.EvenOdd.isEven;
6+
import static com.baeldung.evenodd.EvenOdd.isLsbEven;
7+
import static com.baeldung.evenodd.EvenOdd.isLsbOdd;
8+
import static com.baeldung.evenodd.EvenOdd.isOdd;
9+
import static com.baeldung.evenodd.EvenOdd.isOrEven;
10+
import static com.baeldung.evenodd.EvenOdd.isOrOdd;
11+
import static com.baeldung.evenodd.EvenOdd.isXorEven;
12+
import static com.baeldung.evenodd.EvenOdd.isXorOdd;
13+
import static org.junit.Assert.assertEquals;
14+
15+
import org.junit.Test;
16+
17+
public class EvenOddUnitTest {
18+
19+
@Test
20+
public void whenNumberIsEven_thenReturnTrue() {
21+
assertEquals(true, isEven(2));
22+
}
23+
24+
@Test
25+
public void whenNumberIsOdd_thenReturnTrue() {
26+
assertEquals(true, isOdd(3));
27+
}
28+
29+
@Test
30+
public void whenNumberIsEven_thenReturnTrueWithOr() {
31+
assertEquals(true, isOrEven(4));
32+
}
33+
34+
@Test
35+
public void whenNumberIsOdd_thenReturnTrueOr() {
36+
assertEquals(true, isOrOdd(5));
37+
}
38+
39+
@Test
40+
public void whenNumberIsEven_thenReturnTrueAnd() {
41+
assertEquals(true, isAndEven(6));
42+
}
43+
44+
@Test
45+
public void whenNumberIsOdd_thenReturnTrueAnd() {
46+
assertEquals(true, isAndOdd(7));
47+
}
48+
49+
@Test
50+
public void whenNumberIsEven_thenReturnTrueXor() {
51+
assertEquals(true, isXorEven(8));
52+
}
53+
54+
@Test
55+
public void whenNumberIsOdd_thenReturnTrueXor() {
56+
assertEquals(true, isXorOdd(9));
57+
}
58+
59+
@Test
60+
public void whenNumberIsEven_thenReturnTrueLsb() {
61+
assertEquals(true, isLsbEven(10));
62+
}
63+
64+
@Test
65+
public void whenNumberIsOdd_thenReturnTrueLsb() {
66+
assertEquals(true, isLsbOdd(11));
67+
}
68+
}

core-java-modules/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@
127127
<module>java-numbers-2</module>
128128
<module>java-numbers-3</module>
129129
<module>java-numbers-4</module>
130+
<module>java-numbers-5</module>
130131
</modules>
131132

132133
<dependencyManagement>

0 commit comments

Comments
 (0)