Last active
July 29, 2016 02:32
-
-
Save daichan4649/6350529 to your computer and use it in GitHub Desktop.
月齢計算(calculate age of the moon)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package daichan4649.moon; | |
public class MoonUtil { | |
/** | |
* 月齢取得 | |
* @param year 年(1, 2, ..) | |
* @param month 月(1, 2, ..) | |
* @param day 日(1, 2, ..) | |
* @return 月齢 | |
* @see <a href="http://ja.wikipedia.org/wiki/%E6%9C%88%E9%BD%A2">月齢</a> | |
*/ | |
public static int calculateAgeOfTheMoon(int year, int month, int day) { | |
return (((year - 11) % 19) * 11 + c(month) + day) % 30; | |
} | |
private static int c(int month) { | |
switch (month) { | |
case 1: | |
return 0; | |
case 2: | |
return 2; | |
case 3: | |
return 0; | |
case 4: | |
return 2; | |
case 5: | |
return 2; | |
case 6: | |
return 4; | |
case 7: | |
return 5; | |
case 8: | |
return 6; | |
case 9: | |
return 7; | |
case 10: | |
return 8; | |
case 11: | |
return 9; | |
case 12: | |
return 10; | |
} | |
throw new RuntimeException(String.format("invalid value: %d", month)); | |
} | |
/** | |
* 月相種別取得 | |
* @param ageOfTheMoon 月齢 | |
* @return 月相種別 | |
* @see <a href="http://ja.wikipedia.org/wiki/%E6%9C%88%E7%9B%B8">月相</a> | |
*/ | |
public static LunarPhaseType getLunarPhaseType(int ageOfTheMoon) { | |
switch (ageOfTheMoon) { | |
case 0: | |
// 新月(朔) | |
return LunarPhaseType.NEW_MOON; | |
case 7: | |
// 上弦 | |
return LunarPhaseType.HALF_MOON_UP; | |
case 14: | |
// 満月(望) | |
return LunarPhaseType.FULL_MOON; | |
case 21: | |
// 下弦 | |
return LunarPhaseType.HALF_MOON_DOWN; | |
default: | |
return LunarPhaseType.NONE; | |
} | |
} | |
/** | |
* 月相種別 | |
*/ | |
public enum LunarPhaseType { | |
NONE(""), | |
NEW_MOON("新月"), | |
HALF_MOON_UP("上弦"), | |
HALF_MOON_DOWN("下弦"), | |
FULL_MOON("満月"); | |
private String text; | |
LunarPhaseType(String text) { | |
this.text = text; | |
} | |
public String getText() { | |
return text; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package daichan4649.moon; | |
import junit.framework.Assert; | |
import junit.framework.TestCase; | |
import daichan4649.moon.MoonUtil.LunarPhaseType; | |
public class MoonUtilTest extends TestCase { | |
public void testCalculateAgeOfTheMoon() { | |
int year = 0; | |
int month = 0; | |
int day = 0; | |
int expected = 0; | |
int actual = 0; | |
// 新月-1 | |
year = 2013; | |
month = 8; | |
day = 6; | |
expected = 29; | |
actual = MoonUtil.calculateAgeOfTheMoon(year, month, day); | |
Assert.assertEquals(expected, actual); | |
// 新月 | |
year = 2013; | |
month = 8; | |
day = 7; | |
expected = 0; | |
actual = MoonUtil.calculateAgeOfTheMoon(year, month, day); | |
Assert.assertEquals(expected, actual); | |
// 新月+1 | |
year = 2013; | |
month = 8; | |
day = 8; | |
expected = 1; | |
actual = MoonUtil.calculateAgeOfTheMoon(year, month, day); | |
Assert.assertEquals(expected, actual); | |
// 上弦-1 | |
year = 2013; | |
month = 8; | |
day = 13; | |
expected = 6; | |
actual = MoonUtil.calculateAgeOfTheMoon(year, month, day); | |
Assert.assertEquals(expected, actual); | |
// 上弦 | |
year = 2013; | |
month = 8; | |
day = 14; | |
expected = 7; | |
actual = MoonUtil.calculateAgeOfTheMoon(year, month, day); | |
Assert.assertEquals(expected, actual); | |
// 上弦+1 | |
year = 2013; | |
month = 8; | |
day = 15; | |
expected = 8; | |
actual = MoonUtil.calculateAgeOfTheMoon(year, month, day); | |
Assert.assertEquals(expected, actual); | |
// 満月-1 | |
year = 2013; | |
month = 8; | |
day = 20; | |
expected = 13; | |
actual = MoonUtil.calculateAgeOfTheMoon(year, month, day); | |
Assert.assertEquals(expected, actual); | |
// 満月 | |
year = 2013; | |
month = 8; | |
day = 21; | |
expected = 14; | |
actual = MoonUtil.calculateAgeOfTheMoon(year, month, day); | |
Assert.assertEquals(expected, actual); | |
// 満月+1 | |
year = 2013; | |
month = 8; | |
day = 22; | |
expected = 15; | |
actual = MoonUtil.calculateAgeOfTheMoon(year, month, day); | |
Assert.assertEquals(expected, actual); | |
// 下弦-1 | |
year = 2013; | |
month = 8; | |
day = 27; | |
expected = 20; | |
actual = MoonUtil.calculateAgeOfTheMoon(year, month, day); | |
Assert.assertEquals(expected, actual); | |
// 下弦 | |
year = 2013; | |
month = 8; | |
day = 28; | |
expected = 21; | |
actual = MoonUtil.calculateAgeOfTheMoon(year, month, day); | |
Assert.assertEquals(expected, actual); | |
// 下弦+1 | |
year = 2013; | |
month = 8; | |
day = 29; | |
expected = 22; | |
actual = MoonUtil.calculateAgeOfTheMoon(year, month, day); | |
Assert.assertEquals(expected, actual); | |
} | |
public void testGetLunarPhaseType() { | |
int ageOfTheMoon = 0; | |
LunarPhaseType expected = LunarPhaseType.NONE; | |
LunarPhaseType actual = LunarPhaseType.NONE; | |
// 新月 | |
ageOfTheMoon = 0; | |
expected = LunarPhaseType.NEW_MOON; | |
actual = MoonUtil.getLunarPhaseType(ageOfTheMoon); | |
Assert.assertEquals(expected, actual); | |
// 上弦 | |
ageOfTheMoon = 7; | |
expected = LunarPhaseType.HALF_MOON_UP; | |
actual = MoonUtil.getLunarPhaseType(ageOfTheMoon); | |
Assert.assertEquals(expected, actual); | |
// 満月 | |
ageOfTheMoon = 14; | |
expected = LunarPhaseType.FULL_MOON; | |
actual = MoonUtil.getLunarPhaseType(ageOfTheMoon); | |
Assert.assertEquals(expected, actual); | |
// 下弦 | |
ageOfTheMoon = 21; | |
expected = LunarPhaseType.HALF_MOON_DOWN; | |
actual = MoonUtil.getLunarPhaseType(ageOfTheMoon); | |
Assert.assertEquals(expected, actual); | |
// 上記以外 | |
ageOfTheMoon = -1; | |
expected = LunarPhaseType.NONE; | |
actual = MoonUtil.getLunarPhaseType(ageOfTheMoon); | |
Assert.assertEquals(expected, actual); | |
ageOfTheMoon = 32; | |
expected = LunarPhaseType.NONE; | |
actual = MoonUtil.getLunarPhaseType(ageOfTheMoon); | |
Assert.assertEquals(expected, actual); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment