Question

The Language is Java GenericMethodTest (20) Download the OverloadedMethods.java program. Replace all the methods except main...

The Language is Java

GenericMethodTest (20)

Download the OverloadedMethods.java program. Replace all the methods except main with a single generic method that produces the same output. Call the new file GenericMethodsTest.java.

OverloadMethods.java:

// Printing array elements using overloaded methods.

public class OverloadedMethods 
{ 
        // method printArray to print Integer array 
        
        public static void printArray(Integer[] inputArray) 
        { 
                // display array elements       
                for (Integer element : inputArray) 
                { 
                        System.out.printf("%s ", element);      
                }               
                System.out.printf("\n");        
        } 
        
        // method printArray to print Double array 
        
        public static void printArray(Double[] inputArray) 
        { 
                // display array elements 
        
                for (Double element : inputArray) 
                { 
                        System.out.printf("%s ", element); 
                } 
                
                System.out.printf("\n");        
        } 
        
        //method printArray to print Character array 
        
        public static void printArray(Character[] inputArray) 
        { 
                //display array elements 
        
                for (Character element : inputArray) 
                { 
                        System.out.printf("%s ", element);      
                } 
                System.out.printf("\n");                
        }
        
        public static void main(String[] args) 
        { 
                // create arrays of Integer, Double and Character 
                Integer[] integerArray =  {1, 2, 3, 4, 5, 6}; 
                Double[] doubleArray = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7}; 
                Character[] characterArray = {'H', 'E', 'L', 'L', 'O'}; 
                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(characterArray); // pass a Character array 
        } 
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please let me know if you need more information:-

===================================

public class GenericMethodsTest {

   public static <T> void printArray(T[] inputArray) {
       for (T element : inputArray) {
           System.out.printf("%s ", element);
       }
       System.out.printf("\n");
   }

   public static void main(String[] args) {
       // create arrays of Integer, Double and Character
       Integer[] integerArray = { 1, 2, 3, 4, 5, 6 };
       Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7 };
       Character[] characterArray = { 'H', 'E', 'L', 'L', 'O' };
       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(characterArray); // pass a Character array
   }
}

==========================

OUTPUT:-

==

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

===

===

public class GenericMethods Test { public static <T> void printArray ( [] inputArray) { for (i element : inputArray) { System

==

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 contain

=================

OverloadMethods.java:

// Printing array elements using overloaded methods.

public class OverloadedMethods 
{ 
        // method printArray to print Integer array 
        
        public static void printArray(Integer[] inputArray) 
        { 
                // display array elements       
                for (Integer element : inputArray) 
                { 
                        System.out.printf("%s ", element);      
                }               
                System.out.printf("\n");        
        } 
        
        // method printArray to print Double array 
        
        public static void printArray(Double[] inputArray) 
        { 
                // display array elements 
        
                for (Double element : inputArray) 
                { 
                        System.out.printf("%s ", element); 
                } 
                
                System.out.printf("\n");        
        } 
        
        //method printArray to print Character array 
        
        public static void printArray(Character[] inputArray) 
        { 
                //display array elements 
        
                for (Character element : inputArray) 
                { 
                        System.out.printf("%s ", element);      
                } 
                System.out.printf("\n");                
        }
        
        public static void main(String[] args) 
        { 
                // create arrays of Integer, Double and Character 
                Integer[] integerArray =  {1, 2, 3, 4, 5, 6}; 
                Double[] doubleArray = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7}; 
                Character[] characterArray = {'H', 'E', 'L', 'L', 'O'}; 
                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(characterArray); // pass a Character array 
        } 
}

============

Please let me know if you need more information.

==

Thanks

Add a comment
Know the answer?
Add Answer to:
The Language is Java GenericMethodTest (20) Download the OverloadedMethods.java program. Replace all the methods except main...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 14.8 Prog 8 Overload methods (find avg) Write a program to find the average of a...

    14.8 Prog 8 Overload methods (find avg) Write a program to find the average of a set numbers (assume that the numbers in the set is less than 20) using overload methods. The program first prompts the user to enter a character; if the character is 'I' or 'i', then invokes the appropriate methods to process integer data set; if the character is 'R' or 'r', then invokes the appropriate methods to process real number data set; if the inputted...

  • *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods...

    *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods that has the following generic methods: (1) Write the following method that returns a new ArrayList. The new list contains the nonduplicate (i.e., distinct) elements from the original list. public static ArrayList removeDuplicates(ArrayList list) (2) Write the following method that shuffles an ArrayList. It should do this specifically by swapping two indexes determined by the use of the random class (use Random rand =...

  • In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are...

    In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are in the range 0 to 100 inclusive. The program will ask a user to re-enter an integer if the user inputs a number outside of that range. The inputted integers must then be stored in a single dimensional array of size 20. Please create 3 methods: 1. Write a method public static int countEven(int[] inputArray) The method counts how many even numbers are in...

  • Question 2: Execute the following program. import java.util.Arrays; public class ArrayManipulations { public static void main(String[]...

    Question 2: Execute the following program. import java.util.Arrays; public class ArrayManipulations { public static void main(String[] args) { // sort doubleArray into ascending order char [] A = {'g', ', 'y', 'a', 'e','d' }; Arrays.sort( A); for (char value: A) System.out.printf("%2C", value); System.out.printf("\n"); char [] A Copy = new char[ 3 ]; System.arraycopy( A, 2, A Copy, 0, A Copy.length); for(char value: A Copy) System.out.printf("%2C", value); System.out.printf("\n"); int location = Arrays.binary Search(A Copy, 'y'); if(location >=0) System.out.printf("y Found at element...

  • 1.Write code for a Java method, printArray, that takes an int array as parameter and prints...

    1.Write code for a Java method, printArray, that takes an int array as parameter and prints it out, one element per line. public static void printArray (int[] values){ Methods that do NOT return a result have “void” as return type. Notice how an array parameter type is passed, int [] }// end printArray 2.Write code for a Java method that takes in as input parameter an integer number, say num, and returns a double array of size num with all...

  • JAVA Objectives: 1. Apply linear search algorithm 2. Apply select sort algorithm 3. Apply array iteration...

    JAVA Objectives: 1. Apply linear search algorithm 2. Apply select sort algorithm 3. Apply array iteration skill Problem description: Write the following eight methods and write a main function to test these methods // return the index of the first occurrence of key in arr // if key is not found in arra, return -1 public static int linearSearch(int arr[], int key) // sort the arr from least to largest by using select sort algorithm public stati void selectSort(int arr[])...

  • In Java Write a program that reads an arbitrary number of 25 integers that are positive...

    In Java Write a program that reads an arbitrary number of 25 integers that are positive and even. The program will ask the user to re-enter an integer if the user inputs a number that is odd or negative or zero. The inputted integers must then be stored in a two dimensional array of size 5 x 5. Please create 3 methods: 1. Write a method public static int sum2DArray( int [1] inputArray ) The method sums up all elements...

  • Write a JAVA program with methods that initializes an array with 20 random integers between 1...

    Write a JAVA program with methods that initializes an array with 20 random integers between 1 and 50 and then prints four lines of output, containing 1)The initialized array. 2)Every element at an even index. 3)Every even element. 4)All elements in reverse order. Requirements (and hints): 1. Build a method that takes any integer array as input and prints the elements of the array. ALL printing in this lab must be done using a call to the printArray method and...

  • . In the method main, prompt the user for a non-negative integer and store it in...

    . In the method main, prompt the user for a non-negative integer and store it in an int variable num (Data validation is not required for the lab). Create an int array whose size equals num+10. Initialize the array with random integers between 0 and 50 (including 0 and excluding 50). Hint: See Topic 4 Decision Structures and File IO slides page 42 for how to generate a random integer between 0 (inclusive) and bound (exclusive) by using bound in...

  • In Java write the following array- processing methods into the same application, Lab13.java. Use the main...

    In Java write the following array- processing methods into the same application, Lab13.java. Use the main method in this application to test your methods. 1) Write the void method, shiftLeft, which accepts an array of int and changes the elements of this array so that the index of each element is now less by one and the last element of the array is now zero. For example, if the parameter array is {7, 3, 2, 9, 5}, then when this...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT