Question: Please Help. What is wrong with my code? It works fine but the test cases validDate2 and validDate3 fail when running the program. Can you

Please Help. What is wrong with my code? It works fine but the test cases validDate2 and validDate3 fail when running the program. Can you please tell me what's wrong?

ORIGINAL CODE:

package Lab2;

public class Lab2 {

/**

* Takes a date as three integers:day, month, and year.

* The method returns a true if the date is valid and false otherwise.

* The method checks if the month is valid, and the year is after the year 1000.

* It checks if the day is valid according to the month. If the month is February,

* it checks if the year is a leap year

*/

public static boolean validDate(int day, int month, int year) {

// declaring boolean variables as originally false

boolean yearValid = false;

boolean monthValid = false;

boolean dayValid = false;

boolean isLeapYear = false;

// truth value = T if the month is greater than 1 and less than 12 inclusive

if (month >= 1 && month

monthValid = true;

}

// if the year is greater than 1000 inclusive, the truth value is T

else if (year >= 1000) {

yearValid = true;

}

// It is a leap year if the year is divisible by 400 or divisible by 4 and not divisible by 100

else if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0) {

isLeapYear = true;

}

// If statement for if the month is February and it is a leap year

else if (month == 2 && isLeapYear == true) {

// truth value = T if the day is greater than 1 and less than 29 inclusive

if (day >= 1 && day

dayValid = true;

}

}

// if the day is greater than 31 or less than 1, it is invalid

else if (day > 31 || day

dayValid = false;

}

// April, June, September and November have 30 days

else if (month == 4 || month == 6 || month == 9 || month == 11) {

// truth value = T if the day is greater than 1 and less than 30 inclusive

if (day >= 1 || day

dayValid = true;

}

}

// January, March, May, July, August, October, and December have 31 days

else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {

// truth value = T if the day is greater than 1 and less than 30 inclusive

if (day >= 1 || day

dayValid = true;

}

}

// If statement for if it is February and it is not a leap year

else if (month == 2) {

// truth value = T if the day is greater than 1 and less than 28 inclusive

if (day >= 1 || day

dayValid = true;

}

else if (day > 28) {

dayValid = false;

}

}

// If the above statements don't apply, the date is invalid and the method returns false

else {

return false;

}

// the method returns the truth value of the date

return (yearValid && monthValid && dayValid);

}

}

Please Help. What is wrong with my code? It works fine but

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!