Class Date
This composition example contains classes Date (Fig. F.7), Employee (Fig. F.8) and EmployeeTest (Fig. F.9). Class Date (Fig. F.7) declares instance variables month, day and year (lines 6–8) to represent a date. The constructor receives three int parameters. Line 17 invokes utility method checkMonth (lines 26–32) to validate the month—if the value is out of range the method throws an exception. Line 15 assumes that the value for year is correct and doesn’t validate it. Line 19 invokes utility method checkDay (lines 35–48) to validate the day based on the current month and year. Line 38 determines whether the day is correct based on the number of days in the particular month. If the day is not correct, lines 42–43 determine whether the ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access