Java Reference
In-Depth Information
10
11
// constructor to initialize name, birth date and hire date
12
public Employee(String firstName, String lastName, Date birthDate,
13
Date hireDate)
14
{
15
this .firstName = firstName;
16
this .lastName = lastName;
17
this .birthDate = birthDate;
18
this .hireDate = hireDate;
19
}
20
21
// convert Employee to String format
22
public String toString()
23
{
24
return String.format( "%s, %s Hired: %s Birthday: %s" ,
25
lastName, firstName, hireDate, birthDate);
26
}
27
} // end class Employee
Fig. 8.8 | Employee class with references to other objects. (Part 2 of 2.)
Class EmployeeTest
Class EmployeeTest (Fig. 8.9) creates two Date objects to represent an Employee 's birth-
day and hire date, respectively. Line 10 creates an Employee and initializes its instance vari-
ables by passing to the constructor two String s (representing the Employee 's first and last
names) and two Date objects (representing the birthday and hire date). Line 12 implicitly
invokes the Employee 's toString method to display the values of its instance variables and
demonstrate that the object was initialized properly.
1
// Fig. 8.9: EmployeeTest.java
2
// Composition demonstration.
3
4
public class EmployeeTest
5
{
6
public static void main(String[] args)
7
{
8
Date birth = new Date( 7 , 24 , 1949 );
9
Date hire = new Date( 3 , 12 , 1988 );
10
Employee employee = new Employee( "Bob" , "Blue" , birth, hire);
11
12
System.out.println(employee);
13
}
14
} // end class EmployeeTest
Date object constructor for date 7/24/1949
Date object constructor for date 3/12/1988
Blue, Bob Hired: 3/12/1988 Birthday: 7/24/1949
Fig. 8.9 | Composition demonstration.
Search WWH ::




Custom Search