Java Reference
In-Depth Information
Display 6.9
Accessor Method for an Array Instance Variable (part 2 of 2)
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++)
19 this .a[i] = new Date(object.a[i]);
20 }
Copy constructor for Date .
21 public Date[] getDateArray()
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;
27 }
Accessor method.
Copy constructor for Date .
<There presumably are other methods that are not shown,
but they are irrelevant to the point at hand.>
28 }
Self-Test Exercises
18. Defi ne a method named removeAll that can be added to the class
PartiallyFilledArray . This method has no parameters. When invoked, the
method removeAll deletes all the elements in its calling object.
19. Defi ne 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