Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Overload generic method printArray shown below so that it takes two additional i

ID: 3657212 • Letter: O

Question

Overload generic method printArray shown below 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, or if highSub-script is less than or equal to lowSubscript, the overloaded

printArray method InvalidSubscriptException; otherwise, printArray should return the number

Then modify main to exercise both versions of printArray on arrays integerArray, and characterArray. Test all capabilities of both versions of printArray.

// GenericMethodTest.java

2 // Using generic methods to print array of different types.

3

4 public class GenericMethodTest

5 {

6 // generic method printArray

7 public static < E > void printArray( E[] inputArray )

8 {

9 // display array elements

10 for ( E element : inputArray )

11 System.out.printf( "%s ", element );

12

13 System.out.println();

14 } // end method printArray

15

16 public static void main( String args[] )

17 {

18 // create arrays of Integer, Double and Character

19 Integer[] intArray = { 1, 2, 3, 4, 5 };

20 Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7 };

21 Character[] charArray = { 'H', 'E', 'L', 'L', 'O' };

22

23 System.out.println( "Array integerArray contains:" );

24 printArray( integerArray ); // pass an Integer array

25 System.out.println( " Array doubleArray contains:" );

26 printArray( doubleArray ); // pass a Double array

27 System.out.println( " Array characterArray contains:" );

28 printArray( characterArray ); // pass a Character array

29 } // end main

30 } // end class GenericMethodTest

Array integerArray contains:

1 2 3 4 5 6

Array doubleArray contains:

1.1 2.2 3.3 4.4 5.5 6.6 7.7

Array characterArray contains:

H E L L O



// GenericMethodTest.java

2 // Using generic methods to print array of different types.

3

4 public class GenericMethodTest

5 {

6 // generic method printArray

7 public static < E > void printArray( E[] inputArray )

8 {

9 // display array elements

10 for ( E element : inputArray )

11 System.out.printf( "%s ", element );

12

13 System.out.println();

14 } // end method printArray

15

16 public static void main( String args[] )

17 {

18 // create arrays of Integer, Double and Character

19 Integer[] intArray = { 1, 2, 3, 4, 5 };

20 Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7 };

21 Character[] charArray = { 'H', 'E', 'L', 'L', 'O' };

22

23 System.out.println( "Array integerArray contains:" );

24 printArray( integerArray ); // pass an Integer array

25 System.out.println( " Array doubleArray contains:" );

26 printArray( doubleArray ); // pass a Double array

27 System.out.println( " Array characterArray contains:" );

28 printArray( characterArray ); // pass a Character array

29 } // end main

30 } // end class GenericMethodTest

Array integerArray contains:

1 2 3 4 5 6

Array doubleArray contains:

1.1 2.2 3.3 4.4 5.5 6.6 7.7

Array characterArray contains:

H E L L O

Explanation / Answer

public class GenericMethodTest { public static void printArray(E[] inputArray) { for (E element : inputArray) System.out.printf("%s ", element); System.out.println(); } public static void printArray(E[] inputArray, int lowSubscript, int highSubscript) { try { if (lowSubscript > 0 && highSubscript