Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Leap Year kata
Write a function that returns true or false depending on whether its input integer is a leap year or not.
A leap year is divisible by 4, but is not otherwise divisible by 100 unless it is also divisible by 400.
2001 is a typical common year
1996 is a typical leap year
1900 is an atypical common year
2000 is an atypical leap year
@Ashwaths

This comment has been minimized.

Copy link

@Ashwaths Ashwaths commented May 16, 2020

package leapyear;

public class LeapYear {

public boolean isLeap(int year) {
	boolean result = isMultipleOf4(year) && 
			isNotMiltipleOf100_or_isMiltipleOf100And400(year);
	return result;
}

private boolean isNotMiltipleOf100_or_isMiltipleOf100And400(int year) {
	return (isNotMiltipleOf100(year) || (isMiltipleOf100And400(year)) );
}

private boolean isMiltipleOf100And400(int year) {
	return isMultipleOf(year, 100) && isMultipleOf(year, 400);
}

private boolean isNotMiltipleOf100(int year) {
	return !isMultipleOf(year, 100);
}

private boolean isMultipleOf4(int i) {
	return isMultipleOf(i, 4);
}

private boolean isMultipleOf(int num, int base) {
	return ((num % base) == 0);
}

}

@Ashwaths

This comment has been minimized.

Copy link

@Ashwaths Ashwaths commented May 16, 2020

package leapyear;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

public class TestLeapYear {

private LeapYear leapYear;

@Before
public void setUp() throws Exception {
	leapYear = new LeapYear();
}

@Test
public void when1996_thenIsLeap() {
	boolean isLeap = isLeap(1996);
	assertTrue(isLeap);
}

@Test
public void when2001_thenIsNotLeap() {
	boolean isLeap = this.isLeap(2001);
	assertFalse(isLeap);
}

@Test
public void when1900_thenIsNotLeap() {
	assertFalse(this.isLeap(1900));
}

@Test
public void when2000_thenIsLeap() {
	assertTrue(isLeap(2000));
}


private boolean isLeap(int year) {
	return leapYear.isLeap(year);
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.