This approach totally constructs two sets for each parity representable by an int
type. The contains(Object)
, iterator()
, and size()
should work as expected.
import java.util.Set; public class EvenOrOdd { public static final Set<Integer> EVEN; public static final Set<Integer> ODD; public static String evenOrOdd(final int n) { if (EVEN.contains(n)) return "Even"; if (ODD.contains(n)) return "Odd"; throw new InternalError("Unable to determine the parity of " + n); } static{EVEN=new java.util.AbstractSet<Integer>(){@Override public boolean contains(final Object o){return o instanceof final Integer x&&(x&1)==0;}@Override public java.util.Iterator<Integer>iterator(){return new java.util.Iterator<Integer>(){int current = 0;@Override public boolean hasNext(){return current<2<<31;}public Integer next(){if(!hasNext())throw new java.util.NoSuchElementException();final int current=this.current;this.current++;this.current++;return current;}};}@Override public int size(){return 1<<020;}};} static{ODD=new java.util.AbstractSet<Integer>(){@Override public boolean contains(final Object o){return o instanceof final Integer x&&(x&1)==1;}@Override public java.util.Iterator<Integer>iterator(){return new java.util.Iterator<Integer>(){int current = 1;@Override public boolean hasNext(){return current<2<<31+1;}public Integer next(){if(!hasNext())throw new java.util.NoSuchElementException();final int current=this.current;this.current++;this.current++;return current;}};}@Override public int size(){return 1<<020;}};} }
class EvenOrOdd {static String evenOrOdd(int n) {return (n & 1) == 1 ? "Odd" : "Even";}}- import java.util.Set;
- public class EvenOrOdd {
- public static final Set<Integer> EVEN;
- public static final Set<Integer> ODD;
- public static String evenOrOdd(final int n) {
- if (EVEN.contains(n))
- return "Even";
- if (ODD.contains(n))
- return "Odd";
- throw new InternalError("Unable to determine the parity of " + n);
- }
- static{EVEN=new java.util.AbstractSet<Integer>(){@Override public boolean contains(final Object o){return o instanceof final Integer x&&(x&1)==0;}@Override public java.util.Iterator<Integer>iterator(){return new java.util.Iterator<Integer>(){int current = 0;@Override public boolean hasNext(){return current<2<<31;}public Integer next(){if(!hasNext())throw new java.util.NoSuchElementException();final int current=this.current;this.current++;this.current++;return current;}};}@Override public int size(){return 1<<020;}};}
- static{ODD=new java.util.AbstractSet<Integer>(){@Override public boolean contains(final Object o){return o instanceof final Integer x&&(x&1)==1;}@Override public java.util.Iterator<Integer>iterator(){return new java.util.Iterator<Integer>(){int current = 1;@Override public boolean hasNext(){return current<2<<31+1;}public Integer next(){if(!hasNext())throw new java.util.NoSuchElementException();final int current=this.current;this.current++;this.current++;return current;}};}@Override public int size(){return 1<<020;}};}
- }
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; @DisplayName("Test Even or Odd") class EvenOddTests { @Test @DisplayName("Even Tests") void evenTests () { testEven(0); testEven(24); testEven(620); testEven(98); testEven(22); testEven(56); testEven(-56); testEven(0); } @Test @DisplayName("Odd Tests") void oddTests () { testOdd(1); testOdd(77); testOdd(947); testOdd(431); testOdd(91); testOdd(21); testOdd(-21); testOdd(-91); } void testOdd(int i) { assertEquals("Odd", EvenOrOdd.evenOrOdd(i), i + " is odd"); } void testEven(int i) { assertEquals("Even", EvenOrOdd.evenOrOdd(i), i + " is even"); } }
- import org.junit.jupiter.api.DisplayName;
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- @DisplayName("Test Even or Odd")
- class EvenOddTests {
- @Test
- @DisplayName("Even Tests")
- void evenTests () {
- testEven(0);
- testEven(24);
- testEven(620);
- testEven(98);
- testEven(22);
- testEven(56);
- testEven(-56);
- testEven(0);
- }
- @Test
- @DisplayName("Odd Tests")
- void oddTests () {
- testOdd(1);
- testOdd(77);
- testOdd(947);
- testOdd(431);
- testOdd(91);
- testOdd(21);
- testOdd(-21);
- testOdd(-91);
- }
- void testOdd(int i) {
- assertEquals("Odd", EvenOrOdd.evenOrOdd(i), i + " is odd");
- }
- void testEven(int i) {
- assertEquals("Even", EvenOrOdd.evenOrOdd(i), i + " is even");
- }
- }