Question

In: Computer Science

In java: 4. Using non-generic techniques, implement a static method getMax() that takes an array of...

In java:

4. Using non-generic techniques, implement a static method getMax() that takes an array of type Comparable and returns the maximum element in the array.
(i.e. "public static Comparable getMax(Comparable [] anArray)").
5. Using the generic techniques to specify super-class relationships, implement a type safe version of the method in 4 named getMaxGen().

Solutions

Expert Solution

        public static Comparable getMax(Comparable [] anArray) {
                Comparable max = anArray[0];
                
                for(int i=0; i<anArray.length; i++) {
                        if(anArray[i].compareTo(max) > 0) {
                                max = anArray[i];
                        }
                }
                
                return max;
        }
        
        public static <T extends Comparable<T>> T getMaxGen(T [] anArray) {
                T max = anArray[0];
                
                for(int i=0; i<anArray.length; i++) {
                        if(anArray[i].compareTo(max) > 0) {
                                max = anArray[i];
                        }
                }
                
                return max;
        }

        
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

in java. using the following template as a method, public static void shellSort(int[] array) { create...
in java. using the following template as a method, public static void shellSort(int[] array) { create a program that counts the number of comparisons and swaps of the sorting method does using int array1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // Best Case Test int array2[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; // Worst Case Test int array3[] = {10, 4, 8, 2, 6, 1, 9, 3, 7, 5}; //...
// Java // This method takes an integer array as well as an integer (the starting...
// Java // This method takes an integer array as well as an integer (the starting // index) and returns the sum of the squares of the elements in the array. // This method uses recursion. public int sumSquaresRec(int[] A, int pos) { // TODO: implement this method        return -1; // replace this statement with your own return }    // This method takes a character stack and converts all lower case letters // to upper case ones....
JAVA CODE Give the definition of a static method called showArray that has an array of...
JAVA CODE Give the definition of a static method called showArray that has an array of base type char as a single parameter and that writes to the screen each character in the array on a separate line. It then writes the same array characters in reverse order. This method returns a character array that holds these two lines of characters in the order that you printed them.
JAVA CODE Give the definition of a static method called showArray that has an array of...
JAVA CODE Give the definition of a static method called showArray that has an array of base type char as a single parameter and that writes to the screen each character in the array on a separate line. It then writes the same array characters in reverse order. This method returns a character array that holds these two lines of characters in the order that you printed them.
In java we will need to create a method that takes the array {1,2,3,4,5} and returns...
In java we will need to create a method that takes the array {1,2,3,4,5} and returns the head reference to a linkedList. We will also need to display the linked list in out main method.
Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
***USE JAVA AND NO IMPORTS*** Method editString takes a string and breaks it into an array...
***USE JAVA AND NO IMPORTS*** Method editString takes a string and breaks it into an array of single        words which are then are edited based on the command        Possible Commands:        remove: for the sent words, remove those from the text        replace: replace the word in an even element of words with a word in an odd element of words        add: add the word given word in the index indicated after...
Write a Java method that takes an array of char and a String as input parameters...
Write a Java method that takes an array of char and a String as input parameters and and returns an boolean. The method returns true if we can find the input string inside the array by starting at any position of the array and reading either forwards or backwards.
Implement a method that meets the following requirements: (a) Takes as parameter 1) an int array...
Implement a method that meets the following requirements: (a) Takes as parameter 1) an int array A 2) an int number x to search for (b) determines whether x exists in A, and prints a message indicating the result (c) has the best worst case Big Oh complexity you can manage, using only your own thinking and the materials (the worst case growth rate for the number of items searched should be as low as possible, given that A contains...
Write a generic method in java code public static double jaccard(HashSet A, HashSet B) that on...
Write a generic method in java code public static double jaccard(HashSet A, HashSet B) that on input two sets represented as hash sets, returns their Jaccard similarity. The following are a few sample runs: Input :    A=1, 2, 3, 4,   B=2, 4, 6, 8 Return:   0.3333333333333333 Input :    A=Larry, Michael, Shaq, Kobe, LeBron                    B=Steve, Kobe, Shaq, LeBron, Steph, Jeremy, Michael Return:   0.5 Your method must have time complexity On and space complexity O1, where n is the size of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT