Java Reference
In-Depth Information
In this program, the statements in Lines 1 and 2 create the objects myClock and
yourClock . The statement in Line 3 creates the array arrivalTimeEmp of 10 elements,
wherein each element is a reference variable of the Clock type. The for loop in the
statements in Lines 4 and 5 instantiates the objects of the array arrivalTimeEmp .
The statement in Line 6 sets the arrival time of employee 5, which is the sixth element
of the array. The statement in Line 7 calls the method printTimes with two actual
parameters, and the statement in Line 9 calls this method with arrivalTimeEmp as the
actual parameter, an array of 10 elements.
Note that the for loop in Lines 10 and 11 can be replaced with the following foreach loop:
for (Clock clockObject : clockList)
//Line 10
System.out.println(clockObject);
//Line 11
Example 9-12 illustrates that a constructor of a class can have a variable length formal
parameter list.
EXAMPLE 9-12
Consider the class StudentData :
public class StudentData
{
private String firstName;
private String lastName;
9
private double [] testScores; //array to store
//the test scores
private char grade;
//Default constructor
public StudentData()
{
firstName = "";
lastName = "";
grade = '*';
testScores = new double [5];
}
//Constructor with parameters
//The parameter list is of varying length.
//Postcondition: firstName = fName; lastName = lName;
// testScores = list;
// Calculate and assign the grade to
// grade.
public StudentData(String fName, String lName,
double ... list)
{
firstName = fName;
lastName = lName;
testScores = list;
 
Search WWH ::




Custom Search