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, ...

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.