* Gets whether the given year is a leap year * * @param y The year * @return Whe
ID: 3723520 • Letter: #
Question
* Gets whether the given year is a leap year
*
* @param y The year
* @return Whether y is a leap year
*/
public static boolean isLeapYear(int year) {
if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) {
return true;
}
else return false;
/**
* Gets whether a date is a valid date
* @param m The month
* @param d The day
* @param y The year
* @return Whether m/d/y is a valid date
*/
public static boolean checkValidity(int m, int d, int y) {
//YOU DO THIS.
//if m is not between 1-12 - return false
//if y is negative - return false
//Use if/else if statements to see which month you are in (m)
//make sure the day (d) is >= 1 and <= the number of days in that month (see below)
//if it is - return true
//if not - return false
//month numbers with 31 days: 1,3,5,7,8,10,12
//month numbers with 30 days: 4,6,9,11
//February (m=2) usually has 28 days, unless it is a leap year (call isLeapYear to check)
Explanation / Answer
Answer:
/* Gets whether the given year is a leap year
*
* @param y The year
* @return Whether y is a leap year
*/
public static boolean isLeapYear(int year) {
if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) {
return true;
}
else return false;
}
/**
* Gets whether a date is a valid date
* @param m The month
* @param d The day
* @param y The year
* @return Whether m/d/y is a valid date
*/
public static boolean checkValidity(int m, int d, int y) {
//YOU DO THIS.
//if m is not between 1-12 - return false
if(m < 1 || m > 12) {
return false;
}
//if y is negative - return false
if(y<0) {
return false;
}
//Use if/else if statements to see which month you are in (m)
//make sure the day (d) is >= 1 and <= the number of days in that month (see below)
//if it is - return true
//if not - return false
//month numbers with 31 days: 1,3,5,7,8,10,12
if(m==1 || m==3 || m==5||m==7||m==8||m==10||m==12) {
if(d>=1 && d<31) {
return true;
}
return false;
}
//month numbers with 30 days: 4,6,9,11
else if(m==4||m==6||m==9||m==11) {
if(d>=1 && d<=30) {
return true;
}
return false;
} else {
if(isLeapYear(y)) {
if(d>=1 && d<=29) {
return true;
}
return false;
} else{
if(d>=1 && d<=28) {
return true;
}
return false;
}
}
//February (m=2) usually has 28 days, unless it is a leap year (call isLeapYear to check)
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.