Question: Overload generic method printArray of Fig. 20.3 so that it takes two additional integer arguments, lowSubscript and highSubscript. A call to this method prints only
Overload generic method printArray of Fig. 20.3 so that it takes two additional integer arguments, lowSubscript and highSubscript. A call to this method prints only the designated portion of the array. Validate lowSubscript and highSubscript.
If either is out of range, the overloaded printArray method should throw an InvalidSubscriptException; otherwise, printArray should return the number of elements printed. Then modify main to exercise both versions of printArray on arrays integerArray, doubleArray and characterArray. Test all capabilities of both versions of printArray.
Fig. 20.3

1 // Fig. 20.3: GenericMethodTest.java 2 // Printing array elements using generic method printArray. 1234 3 4 public class GenericMethodTest { 5 6 7 8 9 10 [I 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 } public static void main(String[] args) { // create arrays of Integer, Double and Character Integer [] integerArray ={1, 2, 3, 4, 5}; Double[] doubleArray = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7}; Character [] characterArray = {'H', 'E', 'L', 'L', '0'}; System.out.printf("Array integerArray contains: "); printArray (integerArray); // pass an Integer array System.out.printf("Array doubleArray contains: "); printArray(doubleArray); // pass a Double array System.out.printf("Array characterArray contains: "); printArray (character Array); // pass a Character array } // generic method printArray public static void printArray (T[] inputArray) { // display array elements for (T element : inputArray) { System.out.printf("%s ", element); } } System.out.println(); Array integerArray contains: 1 2 3 4 5 Array doubleArray contains: 1.1 2.2 3.3 4.4 5.5 6.6 7.7 Array characterArray contains: HELLO
Step by Step Solution
3.50 Rating (153 Votes )
There are 3 Steps involved in it
To answer your question I will provide an implementation for the overloaded printArray method accord... View full answer
Get step-by-step solutions from verified subject matter experts
