Java Reference
In-Depth Information
Display 6.9
Accessor Method for an Array Instance Variable
8
public ToyExample( int arraySize)
9
{
10
a = new Date[arraySize];
11
for ( int i = 0; i < arraySize; i++)
12
a[i] = new Date();
13
}
Copy constructor for ToyExample
14
public ToyExample(ToyExample object)
15
{
16
int lengthOfArrays = object.a.length;
17
this .a = new Date[lengthOfArrays];
18
for ( int i = 0; i < lengthOfArrays; i++)
Copy constructor for Date
19
this .a[i] = new Date(object.a[i]);
20
}
21
public Date[] getDateArray()
Accessor method
22
{
23
Date[] temp = new Date[a.length];
24
for ( int i = 0; i < a.length; i++)
25
temp[i] = new Date(a[i]);
26
return temp;
Copy constructor for Date
27
}
<There presumably are other methods that are not shown,
but they are irrelevant to the point at hand.>
28
}
Self-Test Exercises
18. Define a method named removeAll that can be added to the class
PartiallyFilledArray . The method removeAll has no parameters. When
invoked, the method removeAll deletes all the elements in its calling object.
19. Define a method named increaseCapacity that can be added to the class
PartiallyFilledArray in Display 6.5. The method has one int parameter
named newCapacity that increases the capacity of the PartiallyFilledArray so
that it can hold up to newCapacity numbers. If newCapacity is less than or equal
to maxNumberOfElements , then the method does nothing. If newCapacity is
greater than maxNumberOfElements , then maxNumberElements is set equal to
newCapacity and a new array of length newCapacity is created for the array
instance variable a . The old values of the array instance variable are copied to the
newly created array.
 
Search WWH ::




Custom Search