Java Reference
In-Depth Information
To output the arrival time of each employee, you can use a loop such as the following:
for
(
int
j = 0; j < arrivalTimeEmp.length; j++)
//Line 5
System.out.println("Employee " + (j + 1)
+ " arrival time: "
+ arrivalTimeEmp[j]);
//Line 6
The statement in Line 6 assumes that the method
toString
is defined for the
class
Clock
, as described in Chapter 8, to return the time in the form
hr:min:sec
.
To keep track of the departure time of each employee, you can use the array
departureTimeEmp
.
Next we give additional examples to illustrate how to create an array of objects.
EXAMPLE 9-8
In Chapter 8, we created the
class
Circle
to implement the basic properties of a circle.
In this example, we show how to create an array of circles. Consider the following
program:
// Program to create an array of circles.
import
java.util.*;
//Line 1
public class
TestProgArrayOfCircles
//Line 2
{
//Line 3
static
Scanner console =
new
Scanner(System.in);
//Line 4
public static void
main(String[] args)
//Line 5
{
//Line 6
Circle[] circles =
new
Circle[5];
//Line 7
double
radius;
//Line 8
for
(
int
i = 0; i < 5; i++)
//Line 9
{
//Line 10
System.out.print("Enter the radius of circle "
+ (i + 1) + ": ");
//Line 11
radius = console.nextDouble();
//Line 12
circles[i] =
new
Circle(radius);
//Line 13
System.out.println();
//Line 14
}
//Line 15
for
(
int
i = 0; i < 5; i++)
//Line 16
System.out.printf("Circle " + (i + 1) + ": "
+ circles[i]);
//Line 17
}
//end main
//Line 18
}
//Line 19
Search WWH ::
Custom Search