Even or Odd in Java

The challenge

Create a function that takes an integer as an argument and returns “Even” for even numbers or “Odd” for odd numbers.

The solution in Java code

public class EvenOrOdd {
    public static String even_or_odd(int number) {
        return number%2==0 ? "Even" : "Odd";
    }
}

Test cases to validate our solution

import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;

public class EvenOrOddTest {
    @Test
    public void testEvenOrOdd() {
        EvenOrOdd eoo = new EvenOrOdd();
        assertEquals(eoo.even_or_odd(6), "Even");
        assertEquals(eoo.even_or_odd(7), "Odd");       
    }
}
Tags:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments