Question

In: Computer Science

You'll implement a completely generic version of an algorithm to find the maximum of an array....

You'll implement a completely generic version of an algorithm to find the maximum of an array. Unlike in the past, when our algorithm only worked for int[] or double[], this version will work on any Java objects that are comparable, specifically any Java object that implements the Comparable interface. Create a public class named Max with a single class method named max. max should accept an array of Objects that implement Comparable and return the maximum. If the array is null or empty, you should return null.

Solutions

Expert Solution

Explanation:I have implemented the Max class with the max() method that accepts a generic array that implements the Comparable object.I have shown the output of the program, please find the images attached with the answer.Please upvote if you liked my answer and comment if you need any modification or explanation.

//code starts

import java.util.Arrays;

public class Max {

   public static <T extends Comparable<T>> T max(T[] arr) {
       T maxElement = arr[0];
       if (arr == null || arr.length == 0)
           return null;
       for (T arrayElement : arr) {
           if (arrayElement.compareTo(maxElement) > 0) {
               maxElement = arrayElement;
           }
       }
       return maxElement;
   }

   public static void main(String[] args) {
       Integer[] integerArray = {55, 23, 135, -44, 24443, 2224, 1234, 67};
       Double[] doubleArray = {251.13, 2667.26, 3123.33, 54124.4, 124.45,
               3124.53, 1124.54, 65.78};
       Character[] characterArray = {'H', 'A', 'K', 'T', 'O', 'S', 'F', 'W'};
       System.out.println("The maximum element in : "
               + Arrays.toString(integerArray) + " is " + max(integerArray));
       System.out.println("The maximum element in : "
               + Arrays.toString(doubleArray) + " is " + max(doubleArray));
       System.out.println(
               "The maximum element in : " + Arrays.toString(characterArray)
                       + " is " + max(characterArray));
   }
}

Output:


Related Solutions

Behold, the power of interfaces! On this homework problem you'll implement a completely generic version of...
Behold, the power of interfaces! On this homework problem you'll implement a completely generic version of an algorithm to find the maximum of an array. Unlike in the past, when our algorithm only worked for int[] or double[], this version will work on any Java objects that are comparable, specifically any Java object that implements the Comparable interface. Create a public class named Max with a single class method named max. max should accept an array of Objects that implement...
for java, Behold, the power of interfaces! On this homework problem you'll implement a completely generic...
for java, Behold, the power of interfaces! On this homework problem you'll implement a completely generic version of an algorithm to find the maximum of an array. Unlike in the past, when our algorithm only worked for int[] or double[], this version will work on any Java objects that are comparable, specifically any Java object that implements the Comparable interface. Create a public class named Max with a single class method named max. max should accept an array of Objects...
write a recursive algorithm to find the maximum element in an array of n elements and...
write a recursive algorithm to find the maximum element in an array of n elements and analyze its time efficiency. (I am using c++ programming language)
(a) Implement the following algorithm, which is given a duplicate-free array array as input, in C++....
(a) Implement the following algorithm, which is given a duplicate-free array array as input, in C++. whatDoIDo (array): 1) Build a heap from array (using buildHeap as explained in class), where the heap starts at position array[0]. 2) Starting from j = size of array - 1, as long as j>0: i. Swap the entries array[0] and array[j]. ii. Percolate down array[0], but only within the subarray array[0..j-1]. iii. Decrement j by 1. Provide three input/output examples for duplicate-free arrays...
c++ You will implement a template class with the following members: An array of our generic...
c++ You will implement a template class with the following members: An array of our generic type. The size of this array should be determined by an integer argument to the constructor. An integer for storing the array size. A constructor capable of accepting one integer argument. The constructor should initialize the array and set the array size. A method, find Max, that returns the maximum value in the array. A method, find Min, that returns the minimum value in...
implement in LEGV8 find the smallest value in an array.
implement in LEGV8 find the smallest value in an array.
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().
Java Programm please! Design and implement an algorithm using recursion and backtracking to sort an array...
Java Programm please! Design and implement an algorithm using recursion and backtracking to sort an array of integers into ascending order. Consider the given array as input and produce a sorted array as output. Each time you take an integer from the input array, place it at the end of the output array. If the result is unsorted, backtrack.
Problem 3 (4+2+2 marks). (a) Implement the following algorithm, which is given a duplicate-free array array...
Problem 3 (4+2+2 marks). (a) Implement the following algorithm, which is given a duplicate-free array array as input, in C++. whatDoIDo (array): 1) Build a heap from array (using buildHeap as explained in class), where the heap starts at position array[0]. 2) Starting from j = size of array - 1, as long as j>0: i. Swap the entries array[0] and array[j]. ii. Percolate down array[0], but only within the subarray array[0..j-1]. iii. Decrement j by 1. Provide three input/output...
Implement a generic utility to summarize a table of data. In particular, for specified columns, find...
Implement a generic utility to summarize a table of data. In particular, for specified columns, find the minimum, average, and maximum value for each of those columns and print out a nicely-formatted report. Hint: You might want to process one line at a time maintaining, in a dictionary, the running stats for each column. For example: stats = '{'test1': {'min': 80.5, 'sum':845.0, 'max':100.0}, 'test2': {...}, ...etc.' If you also maintain a row counter, you’ll be able to calculate the averages...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT