Class Date
This composition example contains classes Date (Fig. 8.7), Employee (Fig. 8.8) and EmployeeTest (Fig. 8.9). Class Date (Fig. 8.7) declares instance variables month, day and year (lines 6–8) to represent a date. The constructor receives three int parameters. Lines 17–19 validate the month—if it’s out-of-range, lines 18–19 throw an exception. Lines 22–25 validate the day. If the day is incorrect based on the number of days in the particular month (except February 29th which requires special testing for leap years), lines 24–25 throw an exception. Lines 28–31 perform the leap year testing for February. If the month is February and the day is 29 and the year is not a leap year, lines 30–31 throw an exception. If no exceptions are thrown, ...
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