Chapter 8 Object-Based Programming 397 40 41 // (Web hosting control panel)

Chapter 8 Object-Based Programming 397 40 41 // Time2 constructor: another Time2 object supplied 42 public Time2( Time2 time ) 43 { 44 setTime( time.hour, time.minute, time.second ); 45 } 46 47 // Set a new time value using universal time. Perform 48 // validity checks on data. Set invalid values to zero. 49 public void setTime( int h, int m, int s ) 50 { 51 hour = ( ( h >= 0 && h < 24 ) ? h : 0 ); 52 minute = ( ( m >= 0 && m < 60 ) ? m : 0 ); 53 second = ( ( s >= 0 && s < 60 ) ? s : 0 ); 54 } 55 56 // convert to String in universal-time format 57 public String toUniversalString() 58 { 59 DecimalFormat twoDigits = new DecimalFormat( "00" ); 60 61 return twoDigits.format( hour ) + ":" + 62 twoDigits.format( minute ) + ":" + 63 twoDigits.format( second ); 64 } 65 66 // convert to String in standard-time format 67 public String toString() 68 { 69 DecimalFormat twoDigits = new DecimalFormat( "00" ); 70 71 return ( (hour == 12 || hour == 0) ? 12 : hour % 12 ) + 72 ":" + twoDigits.format( minute ) + 73 ":" + twoDigits.format( second ) + 74 ( hour < 12 ? " AM" : " PM" ); 75 } 76 77 } // end class Time2 Fig. 8.6 Class Time2with overloaded constructors (part 2 of 2). Most of the code in class Time2is identical to that in class Time1, so we concentrate on only the new features here (i.e., the constructors). Lines 16 19 define the no-argument (default) constructor. Lines 23 26 define a Time2 constructor that receives a single int argument representing the hour. Lines 30 33 defines a Time2 constructor that receives two intarguments representing the hourand minute. Lines 36 39 define a Time2constructor that receives three intarguments representing the hour, minuteand second. Lines 42 45 define a Time2constructor that receives a Time2reference to another Time2 object. In this case, the values from the Time2 argument are used to initialize the hour, minute and second. Notice that none of the constructors specifies a return data type (remember, this is not allowed for constructors). Also, notice that all the constructors receive different numbers of arguments and/or different types of arguments. Even though only two Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01
Note: If you are looking for high quality webhost to host and run your jsp application check Vision florida web design services

Leave a Reply