Java:FunWithCalendars日期验证类测试及代码修正指导
Hey Jacob, let's work through fixing your FunWithCalendars class and getting it tested properly in Eclipse—you're close, just a few key fixes needed!
1. Fixing the Core Logic Errors
Let's tackle each mistake you mentioned one by one:
a. Correcting the isLeapYear Method
Your original code used division (year / 4 == 0) instead of the modulus operator %, which is what we need to check for exact divisibility (by looking for a remainder of 0). Here's the correct implementation matching Gregorian calendar rules:
private boolean isLeapYear(int year) { // Leap year rules: divisible by 400, OR divisible by 4 but not 100 return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0); }
b. Fixing the isValidDay Method
You had an accidental assignment (=) instead of a comparison (==), plus we need to account for varying month lengths and leap years. Using an array to store standard month days makes this clean and easy to maintain:
private boolean isValidDay(int month, int day, int year) { if (day < 1) { return false; } // Index 0 unused to align with 1-12 month numbering int[] daysInMonth = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // Adjust February for leap years if (month == 2 && isLeapYear(year)) { return day <= 29; } else { return day <= daysInMonth[month]; } }
c. Fixing the isValid Method & Constructor
Your isValid method had mismatched parameters—let's tie it to the class's instance variables (set via a proper constructor) so you don't have to re-pass the date every time. Here's the full class structure with a working constructor and isValid method:
public class FunWithCalendars { private int month; private int day; private int year; // Constructor to initialize the date values public FunWithCalendars(int month, int day, int year) { this.month = month; this.day = day; this.year = year; } // Helper method to validate month (1-12 range) private boolean isValidMonth(int month) { return month >= 1 && month <= 12; } // ... include the corrected isLeapYear and isValidDay methods here ... // Public method to check if the entire date is valid public boolean isValid() { return isValidMonth(month) && isValidDay(month, day, year); } }
2. Testing the Class in Eclipse
To test your code, create a separate test class with a main method—this lets you pass different dates and verify the output easily:
Step 1: Create the Test Class
Make a new class called FunWithCalendarsTest with this code:
public class FunWithCalendarsTest { public static void main(String[] args) { // Test valid date: 7/20/2010 FunWithCalendars validDate = new FunWithCalendars(7, 20, 2010); System.out.println("7/20/2010 is valid? " + validDate.isValid()); // Should print true // Test invalid month: 13/1/2009 FunWithCalendars invalidMonth = new FunWithCalendars(13, 1, 2009); System.out.println("13/1/2009 is valid? " + invalidMonth.isValid()); // Should print false // Test leap day: 2/29/2020 FunWithCalendars leapDay = new FunWithCalendars(2, 29, 2020); System.out.println("2/29/2020 is valid? " + leapDay.isValid()); // Should print true // Test non-leap day: 2/29/2019 FunWithCalendars nonLeapDay = new FunWithCalendars(2, 29, 2019); System.out.println("2/29/2019 is valid? " + nonLeapDay.isValid()); // Should print false // Test invalid day: 4/31/2023 (April only has 30 days) FunWithCalendars invalidDay = new FunWithCalendars(4, 31, 2023); System.out.println("4/31/2023 is valid? " + invalidDay.isValid()); // Should print false } }
Step 2: Run the Test
- Right-click on
FunWithCalendarsTestin Eclipse's Package Explorer - Select
Run As→Java Application - Check the Console tab for results—you'll see each test case's validity printed out.
Troubleshooting Constructor Errors
If you were getting constructor issues before, it's likely because:
- You didn't define a constructor that accepts three
intparameters (Java's default constructor is no-arg, so you have to write this one explicitly) - You tried passing non-int values (like strings) when creating a
FunWithCalendarsinstance—make sure you're using plain integers for month/day/year.
3. Full Corrected Code
Here's the complete, working FunWithCalendars class for reference:
public class FunWithCalendars { private int month; private int day; private int year; public FunWithCalendars(int month, int day, int year) { this.month = month; this.day = day; this.year = year; } private boolean isLeapYear(int year) { return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0); } private boolean isValidMonth(int month) { return month >= 1 && month <= 12; } private boolean isValidDay(int month, int day, int year) { if (day < 1) { return false; } int[] daysInMonth = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if (month == 2 && isLeapYear(year)) { return day <= 29; } else { return day <= daysInMonth[month]; } } public boolean isValid() { return isValidMonth(month) && isValidDay(month, day, year); } }
内容的提问来源于stack exchange,提问作者jacob

