Java Reference
In-Depth Information
Program 5.3 Objects as function parameters and returned results
class Date
int dd ;
int mm ;
int yyyy ;
static final String [ ] months= {
"January" , "February" , "March" , "April" , "May" ,
"June" , "July" , "August" , "September" , "October" ,
"November" , "December"
}
;
// Constructor
Date( int day , int month , int year)
this .dd=day;
this .mm=month ;
this . yyyy=year ;
}
}
Observe the construction of a constant array of strings months using the
syntax static final String [] . The keyword public can be omitted in this
textbook (except for the main program class function) since we do not deal with
object inheritance, another important concept of object-oriented programming
languages.
Program 5.4 Testing the Date class
class TestDate {
static void Display(Date d) {
System . out . println ( "The " +d . dd+ "" +Date . months [ d .mm 1]+ "of
" +d . yyyy) ;
}
static boolean isBefore(Date d1, Date d2)
boolean result= true ;
if (d1 . yyyy > d2 . yyyy) result= false ;
if (d1 . yyyy==d2 . yyyy && d1 .mm > d2 .mm) r e s u l t= false ;
if (d1 . yyyy==d2 . yyyy && d1 .mm==d 2 . mm && d 1 . d d > d2 . dd)
r e s u l t=
false ;
return result ;
}
public static void main( String [ ]
args )
{ Date day1= new Date(23 ,12 ,1971) ;
Display(day1) ;
Date day2= new Date(23 ,6 ,1980) ;
System . out . println ( isBefore (day1 , day2) ) ;
 
Search WWH ::




Custom Search