Question: The result should exactly look like the sample output! Assignment Content Use the example code for the generic PrintArray that is provided below. Modify the
The result should exactly look like the sample output!
Assignment Content
- Use the example code for the generic PrintArray that is provided below. Modify the code to overload the generic method of PrintArray twice for each of the following:
- take one additional integer argument, high, entered by the user. When you call the method with this arguments, it prints out only the portion of the array that is between the first element and the "high" argument. Your program needs to validate that the high value is within the range of the array bounds. If it is out of range, then the overloaded PrintArray method should throw and InvalidSubscriptException; The exception should be caught by the main program and an error message to the user should notify them of the issue and allow them to enter a correct value. If no exception is thrown, then printArray should return the number of elements printed. This is a custom exception. The code for the exception class is provided below the PrintArray code.
- Overload the generic PrintArray with a non-generic version of the method that specifically prints an array of Strings that are animals in a zoo. Use a neat, tabular format as shown below:
- The following names are in the array;
- Elephant Panda Giraffe Bear Penguin Ostrich Tiger Lion
Using generic methods to print array of different types.
public class GenericMethodTest
{
// generic method printArray
public static < E > void printArray( E[] inputArray )
{
// display array elements
for ( E element : inputArray )
System.out.printf( "%s ", element );
System.out.println();
} // end method printArray
public static void main( String args[] )
{
// create an arrays of Integer, Double and Character
Integer[] integerArray = { 1, 2, 3, 4, 5, 6 , 7, 8, 9, 10};
Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7 , 8.8, 9.9, 10.1};
Character[] characterArray = { 'I', 'S', '2', '4', '7' ,' ' , 'i', 's', ' ' , 'f', 'u', 'n'};
System.out.println( "Array integers:" );
printArray( integerArray ); // pass an Integer array
System.out.println( " Array doubles:" );
printArray( doubleArray ); // pass a Double array
System.out.println( " Array characters:" );
printArray( characterArray ); // pass a Character array
} // end main
} // end class GenericMethodTest
public classInvalidSubscriptExceptionextendsRuntimeException {
// no-argument constructor
publicInvalidSubscriptException() {
this("Invalid subscript.");
}
// one-argument constructor
publicInvalidSubscriptException(String message) {
super(message);
}
// two-argument constructor
publicInvalidSubscriptException(String message, Throwable cause) {
super(message, cause);
}
}
Sample Output:
-----------------------
Enter a value for high: 25
Invalid subscript.
Enter a new high: 5
Array integers:
1 2 3 4 5
Array doubles:
1.1 2.2 3.3 4.4 5.5
Array characters:
I S 2 4 7
Zoo Animals:
Elephant Panda Giraffe Bear
Penguin Ostrich Tiger Lion
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
