I'm looking for 3 different answers:
- Using Java predefined functions
- Converting from string to number
- Without the above two (any other types of answers are also welcome)
I am looking for improving my code (as per the above 3 conditions for 3 answers) taken from my repository here.
/*
* Checks if the year passed is a leap year
*
* @param year; requires the year to have 4 digits
* @return true if leap, else false
*
*/
public static boolean isLeapYear(int year) {
String testYear = String.valueOf(year);
if (testYear.charAt(2) == '1' || testYear.charAt(2) == '3' ||
testYear.charAt(2) == 5 || testYear.charAt(2) == '7' ||
testYear.charAt(2) == '9') {
//If the third digit is odd
if (testYear.charAt(3)=='2'||testYear.charAt(3)=='6'){
return true;
}
else{
return false;
}
}
else{
if (testYear.charAt(2) == '0' && testYear.charAt(3) == '0') {
if(testYear.charAt(0) == '1' || testYear.charAt(0) == '3' ||
testYear.charAt(0) == 5 || testYear.charAt(0) == '7' ||
testYear.charAt(0) == '9'){
//If first digit is odd
if(testYear.charAt(1)=='2'||testYear.charAt(1)=='6'){
return true;
}
else{
return false;
}
}
//If second digit is not odd
else if(testYear.charAt(1)=='0'||testYear.charAt(1)=='4'||
testYear.charAt(1)=='8'){
return true;
}
else
return false;
}
else if (testYear.charAt(3)=='0'||testYear.charAt(3)=='4'||
testYear.charAt(3)=='8'){
//If fourth digit is not odd
return true;
}
else
return false;
}
}
.gitignore
file to ignorebin/
. \$\endgroup\$