My Java Projects


Even or Odd
Fizz Buzz

Even or Odd - determine if a number is even or odd without using the modulus or divide operators.
This is the code
                        
package com.elamorgan.learning.evenorodd;

/**
 * Problem:
 * Write a method that identifies if a given positive decimal number is even or odd
 * without using the % or / operator.
 */
class EvenOrOdd {

    /**
     * Checks to see if a given positive integer is even.
     * @param number -  the number to check.
     * @return true it the number is even, otherwise false.
     * @throws IllegalArgumentException - if the number is less than one.
     */
    boolean isEven(int number) throws IllegalArgumentException {

        if (number < 1) {
            throw new IllegalArgumentException("Number cannot be less than one");
        }

        while(number >= 2) {
            number = number - 2;
        }

        if (number == 1) {
            return false;
        }
        return true;
    }


}


                        
Here are the unit tests
                        
package com.elamorgan.learning.evenorodd;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
 * Test class for {@link EvenOrOdd}.
 */
class EvenOrOddTest {

    private EvenOrOdd evenOrOdd;

    @BeforeEach
    public void setup() {
        evenOrOdd = new EvenOrOdd();
    }

    @Test
    void isEven_WithZero() throws IllegalArgumentException {
        assertThrows(IllegalArgumentException.class, () -> evenOrOdd.isEven(0));
    }

    @Test
    void isEven_WithEvenNumber() throws IllegalArgumentException {
        boolean result = evenOrOdd.isEven(12);
        assertTrue(result);
    }

    @Test
    void isEven_WithOddNumber() throws IllegalArgumentException {
        boolean result = evenOrOdd.isEven(31);
        assertFalse(result);
    }

    @Test
    void isEven_WithManyEvenNumbers() throws IllegalArgumentException {
        int limit = 100;
        for (int i = 2; i <= limit; i = i + 2) {
            boolean result = evenOrOdd.isEven(i);
            System.out.println("i is " + i);
            System.out.println("result is " + result);
            assertTrue(result);
        }
    }

    @Test
    void isEven_WithManyOddNumbers() throws IllegalArgumentException {
        int limit = 100;
        for (int i = 1; i <= limit; i = i + 2) {
            boolean result = evenOrOdd.isEven(i);
            System.out.println("i is " + i);
            System.out.println("result is " + result);
            assertFalse(result);
        }
    }

}
                        
                        
Fizz Buzz
This is the code
package com.elamorgan.learning.fizzbuzz;

import java.util.ArrayList;
import java.util.List;

/**
 * Runs the game called 'fizzbuzz' which involves handling a list of integers and
 * returning those that are divisible by 3 or 5.
 */
class FizzBuzzManager {

    private static final Integer BUZZ_DIVISOR = 3;
    private static final Integer FIZZ_DIVISOR = 5;

    /**
     * Runs the game.
     *
     * Given a list of integers, it will return those that are divisible by 3 and/or 5.
     * 
     *
     * @param numbers - the list of integers to run the game with.
     * @return - a class containing the integers that are divisible by 3 or 5.
     */
    public FizzBuzzResults performFizzBuzz(List<Integer> numbers) {

        List<Integer> fizzResults = new ArrayList<>();
        List<Integer> buzzResults = new ArrayList<>();

        if (numbers != null && !numbers.isEmpty()) {

            for (Integer num : numbers) {
                if (num % BUZZ_DIVISOR == 0) {
                    buzzResults.add(num);
                }
                if (num % FIZZ_DIVISOR == 0) {
                    fizzResults.add(num);
                }
            }
        }

        return new FizzBuzzResults(fizzResults, buzzResults);
    }
}

package com.elamorgan.learning.fizzbuzz;

import java.util.List;

/**
 * Holds the results of running the fizzbuzz game.
 */
record FizzBuzzResults(List<Integer> fizzResults, List<Integer> buzzResults) {

}


                        
Here are the unit tests
                        
package com.elamorgan.learning.fizzbuzz;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;

import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;

/**
 * Test class for {@link FizzBuzzManagerTest}.
 */
class FizzBuzzManagerTest {

  @Test
  void performFizzBuzz_WithNullList() {
    FizzBuzzManager fizzBuzzManager = new FizzBuzzManager();
    FizzBuzzResults fizzBuzzResults = fizzBuzzManager.performFizzBuzz(null);

    List<Integer> fizzList = fizzBuzzResults.fizzResults();
    List<Integer> buzzList = fizzBuzzResults.buzzResults();

    assertThat(fizzList, is(notNullValue()));
    assertThat(fizzList.size(), is(0));

    assertThat(buzzList, is(notNullValue()));
    assertThat(buzzList.size(), is(0));
  }

  @Test
  void performFizzBuzz_WithEmptyList() {

    List<Integer> emptyList = new ArrayList<>();

    FizzBuzzManager fizzBuzzManager = new FizzBuzzManager();
    FizzBuzzResults fizzBuzzResults = fizzBuzzManager.performFizzBuzz(emptyList);

    List<Integer> fizzList = fizzBuzzResults.fizzResults();
    List<Integer> buzzList = fizzBuzzResults.buzzResults();

    assertThat(fizzList, is(notNullValue()));
    assertThat(fizzList.size(), is(0));

    assertThat(buzzList, is(notNullValue()));
    assertThat(buzzList.size(), is(0));
  }

  @Test
  void performFizzBuzz_WithSingleNonFizzNumber() {

    List<Integer> singleNumberList = new ArrayList<>();
    singleNumberList.add(1);

    FizzBuzzManager fizzBuzzManager = new FizzBuzzManager();
    List<Integer> fizzList = fizzBuzzManager.performFizzBuzz(singleNumberList).fizzResults();

    assertThat(fizzList, is(notNullValue()));
    assertThat(fizzList.size(), is(0));
  }

  @Test
  void performFizzBuzz_WithSingleFizzNumber() {

    List<Integer> singleNumberFizzList = new ArrayList<>();
    singleNumberFizzList.add(5);

    FizzBuzzManager fizzBuzzManager = new FizzBuzzManager();
    List<Integer> fizzList = fizzBuzzManager.performFizzBuzz(singleNumberFizzList).fizzResults();

    assertThat(fizzList, is(notNullValue()));
    assertThat(fizzList.size(), is(1));
    assertThat(fizzList.contains(5), is(true));

  }

  @Test
  void performFizzBuzz_WithNoFizzNumbers() {

    List<Integer> noMatchesFizzList = new ArrayList<>();
    noMatchesFizzList.add(1);
    noMatchesFizzList.add(2);
    noMatchesFizzList.add(9);
    noMatchesFizzList.add(999);

    FizzBuzzManager fizzBuzzManager = new FizzBuzzManager();
    List<Integer> fizzList = fizzBuzzManager.performFizzBuzz(noMatchesFizzList).fizzResults();

    assertThat(fizzList, is(notNullValue()));
    assertThat(fizzList.size(), is(0));
  }

  @Test
  void performFizzBuzz_WithFizzNumbers() {

    List<Integer> someMatchesFizzList = new ArrayList<>();
    someMatchesFizzList.add(1);
    someMatchesFizzList.add(5);
    someMatchesFizzList.add(9);
    someMatchesFizzList.add(125);

    FizzBuzzManager fizzBuzzManager = new FizzBuzzManager();
    List<Integer> fizzList = fizzBuzzManager.performFizzBuzz(someMatchesFizzList).fizzResults();

    assertThat(fizzList, is(notNullValue()));
    assertThat(fizzList.size(), is(2));
    assertThat(fizzList.contains(5), is(true));
    assertThat(fizzList.contains(125), is(true));
  }

  @Test
  void performFizzBuzz_WithFizzNumbers_SomeNegative() {

    List<Integer> someMatchesAndNegativeFizzList = new ArrayList<>();
    someMatchesAndNegativeFizzList.add(1);
    someMatchesAndNegativeFizzList.add(5);
    someMatchesAndNegativeFizzList.add(-9);
    someMatchesAndNegativeFizzList.add(-125);

    FizzBuzzManager fizzBuzzManager = new FizzBuzzManager();
    List<Integer> fizzList = fizzBuzzManager.performFizzBuzz(someMatchesAndNegativeFizzList).fizzResults();

    assertThat(fizzList, is(notNullValue()));
    assertThat(fizzList.size(), is(2));
    assertThat(fizzList.contains(5), is(true));
    assertThat(fizzList.contains(-125), is(true));
  }

  @Test
  void performFizzBuzz_WithFizzNumbers_AllNegative() {

    List<Integer> someMatchesAndNegativeFizzList = new ArrayList<>();
    someMatchesAndNegativeFizzList.add(-101);
    someMatchesAndNegativeFizzList.add(-5);
    someMatchesAndNegativeFizzList.add(-1250);
    someMatchesAndNegativeFizzList.add(-9);
    someMatchesAndNegativeFizzList.add(-5555);

    FizzBuzzManager fizzBuzzManager = new FizzBuzzManager();
    List<Integer> fizzList = fizzBuzzManager.performFizzBuzz(someMatchesAndNegativeFizzList).fizzResults();

    assertThat(fizzList, is(notNullValue()));
    assertThat(fizzList.size(), is(3));
    assertThat(fizzList.contains(-5), is(true));
    assertThat(fizzList.contains(-1250), is(true));
    assertThat(fizzList.contains(-5555), is(true));
  }

  @Test
  void performFizzBuzz_WithSingleNonBuzzNumber() {

    List<Integer> singleNumberList = new ArrayList<>();
    singleNumberList.add(1);

    FizzBuzzManager fizzBuzzManager = new FizzBuzzManager();
    List<Integer> buzzList = fizzBuzzManager.performFizzBuzz(singleNumberList).buzzResults();

    assertThat(buzzList, is(notNullValue()));
    assertThat(buzzList.size(), is(0));
  }

  @Test
  void performFizzBuzz_WithSingleBuzzNumber() {

    List<Integer> singleNumberBuzzList = new ArrayList<>();
    singleNumberBuzzList.add(6);

    FizzBuzzManager fizzBuzzManager = new FizzBuzzManager();
    List<Integer> buzzList = fizzBuzzManager.performFizzBuzz(singleNumberBuzzList).buzzResults();

    assertThat(buzzList, is(notNullValue()));
    assertThat(buzzList.size(), is(1));
    assertThat(buzzList.contains(6), is(true));
  }

  @Test
  void performFizzBuzz_WithNoBuzzNumbers() {

    List<Integer> noMatchesBuzzList = new ArrayList<>();
    noMatchesBuzzList.add(1);
    noMatchesBuzzList.add(2);
    noMatchesBuzzList.add(11);
    noMatchesBuzzList.add(1000);

    FizzBuzzManager fizzBuzzManager = new FizzBuzzManager();
    List<Integer> buzzList = fizzBuzzManager.performFizzBuzz(noMatchesBuzzList).buzzResults();

    assertThat(buzzList, is(notNullValue()));
    assertThat(buzzList.size(), is(0));
  }

  @Test
  void performFizzBuzz_WithBuzzNumbers() {

    List<Integer> someMatchesBuzzList = new ArrayList<>();
    someMatchesBuzzList.add(1);
    someMatchesBuzzList.add(5);
    someMatchesBuzzList.add(9);
    someMatchesBuzzList.add(120);

    FizzBuzzManager fizzBuzzManager = new FizzBuzzManager();
    List<Integer> buzzList = fizzBuzzManager.performFizzBuzz(someMatchesBuzzList).buzzResults();

    assertThat(buzzList, is(notNullValue()));
    assertThat(buzzList.size(), is(2));
    assertThat(buzzList.contains(9), is(true));
    assertThat(buzzList.contains(120), is(true));
  }

  @Test
  void performFizzBuzz_WithBuzzNumbers_SomeNegative() {

    List<Integer> someMatchesAndNegativeBuzzList = new ArrayList<>();
    someMatchesAndNegativeBuzzList.add(1);
    someMatchesAndNegativeBuzzList.add(5);
    someMatchesAndNegativeBuzzList.add(-9);
    someMatchesAndNegativeBuzzList.add(-125);

    FizzBuzzManager fizzBuzzManager = new FizzBuzzManager();
    List<Integer> buzzList = fizzBuzzManager.performFizzBuzz(someMatchesAndNegativeBuzzList).buzzResults();

    assertThat(buzzList, is(notNullValue()));
    assertThat(buzzList.size(), is(1));
    assertThat(buzzList.contains(-9), is(true));
  }

  @Test
  void performFizzBuzz_WithBuzzNumbers_AllNegative() {

    List<Integer> someMatchesAndNegativeBuzzList = new ArrayList<>();
    someMatchesAndNegativeBuzzList.add(-101);
    someMatchesAndNegativeBuzzList.add(-5);
    someMatchesAndNegativeBuzzList.add(-1250);
    someMatchesAndNegativeBuzzList.add(-9);
    someMatchesAndNegativeBuzzList.add(-999);

    FizzBuzzManager fizzBuzzManager = new FizzBuzzManager();
    List<Integer> buzzList = fizzBuzzManager.performFizzBuzz(someMatchesAndNegativeBuzzList).buzzResults();

    assertThat(buzzList, is(notNullValue()));
    assertThat(buzzList.size(), is(2));
    assertThat(buzzList.contains(-9), is(true));
    assertThat(buzzList.contains(-999), is(true));
  }

  @Test
  void performFizzBuzz_WithBuzzNumbers_AndFuzzNumbers() {

    List<Integer> fuzzAndBuzzNumbers = new ArrayList<>();
    fuzzAndBuzzNumbers.add(-101);
    fuzzAndBuzzNumbers.add(5);
    fuzzAndBuzzNumbers.add(-1250);
    fuzzAndBuzzNumbers.add(-9);
    fuzzAndBuzzNumbers.add(999);

    FizzBuzzManager fizzBuzzManager = new FizzBuzzManager();
    List<Integer> buzzList = fizzBuzzManager.performFizzBuzz(fuzzAndBuzzNumbers).buzzResults();
    List<Integer> fuzzList = fizzBuzzManager.performFizzBuzz(fuzzAndBuzzNumbers).fizzResults();

    assertThat(buzzList, is(notNullValue()));
    assertThat(buzzList.size(), is(2));
    assertThat(buzzList.contains(-9), is(true));
    assertThat(buzzList.contains(999), is(true));

    assertThat(fuzzList, is(notNullValue()));
    assertThat(fuzzList.size(), is(2));
    assertThat(fuzzList.contains(5), is(true));
    assertThat(fuzzList.contains(-1250), is(true));
  }
}