Chapter 8 Object-Based Programming 415 7 private int
Chapter 8 Object-Based Programming 415 7 private int day; // 1-31 based on month 8 private int year; // any year 9 10 // Constructor: Confirm proper value for month; 11 // call method checkDay to confirm proper 12 // value for day. 13 public Date( int theMonth, int theDay, int theYear ) 14 { 15 if ( theMonth > 0 && theMonth <= 12 ) // validate month 16 month = theMonth; 17 else { 18 month = 1; 19 System.out.println( "Month " + theMonth + 20 " invalid. Set to month 1." ); 21 } 22 23 year = theYear; // could validate year 24 day = checkDay( theDay ); // validate day 25 26 System.out.println( 27 "Date object constructor for date " + toString() ); 28 } 29 30 // Utility method to confirm proper day value 31 // based on month and year. 32 private int checkDay( int testDay ) 33 { 34 int daysPerMonth[] = 35 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 36 37 // check if day in range for month 38 if ( testDay > 0 && testDay <= daysPerMonth[ month ] ) 39 return testDay; 40 41 // check for leap year 42 if ( month == 2 && testDay == 29 && 43 ( year % 400 == 0 || 44 ( year % 4 == 0 && year % 100 !=0 ) ) ) 45 return testDay; 46 47 System.out.println( "Day " + testDay + 48 " invalid. Set to day 1." ); 49 50 return 1; // leave object in consistent state 51 } 52 53 // Create a String of the form month/day/year 54 public String toString() 55 { 56 return month + "/" + day + "/" + year; 57 } 58 59 } // end class Date Fig. 8.13 Dateclass (part 2 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision j2ee hosting services