Skip to content

Instantly share code, notes, and snippets.

@SDiamante13
Created July 27, 2022 21:07
Show Gist options
  • Select an option

  • Save SDiamante13/787b4e93d473fc17723acda72a5c0c98 to your computer and use it in GitHub Desktop.

Select an option

Save SDiamante13/787b4e93d473fc17723acda72a5c0c98 to your computer and use it in GitHub Desktop.
Convert isLeapYear to instance method. Doing this so that we can have year be an instance variable. That way we can have access to it in the isDivisibleBy method. It will make it very easy to read. This also makes Year like a Wrapper class. I'd imagine that Year could hold other properties that makes a year a year.
package tech.pathtoprogramming.tdd;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import static org.assertj.core.api.Assertions.assertThat;
class AYearShould {
@ParameterizedTest
@CsvSource({
"2001,false",
"1996,true",
"1992,true",
"1900,false",
"2000,true"
})
void determineWhetherAGivenYearIsALeapYearOrNot(int givenYear, boolean isExpectedLeapYear) {
boolean isLeapYear = new Year().isLeapYear(givenYear);
assertThat(isLeapYear).isEqualTo(isExpectedLeapYear);
}
}
class Year {
private static boolean isDivisibleBy(int year, int four) {
return year % four == 0;
}
public boolean isLeapYear(int year) {
if (isDivisibleBy(year, 4) && (year % 100 != 0 || isDivisibleBy(year, 400))) {
return true;
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment