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...
(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...
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...
Consider the following algorithm to find the kth largest elementof a given array A of...
Consider the following algorithm to find the kth largest element of a given array A of n numbers. We pick every fifth element of A and place them in the array B. We find the median of B recursively, and use this median of B as a pivot to partition the array A. Depending on k and the number of elements that are smaller than the chosen pivot, we recurse into an appropriate subproblem of A.Answer the following questions.Write the...
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...
For this assignment, you will implement the minimax algorithm with alpha-beta pruning in order to find...
For this assignment, you will implement the minimax algorithm with alpha-beta pruning in order to find the optimal move for a game of generalized tic-tac-toe. In this version of the game, the players can choose different board sizes, for example 4x4 or 5x5, instead of the normal 3x3. The game proceeds with the usual rules for tic-tac-toe (see https://en.wikipedia.org/wiki/Tic-tac-toe). Requirements You are to modify the mp2basecode (below) program to implement the alpha-beta search for making the computer’s move. This will...
Programming language: JAVA First, implement a recursive, Divide&Conquer-based algorithm to identify both the Minimum and Maximum...
Programming language: JAVA First, implement a recursive, Divide&Conquer-based algorithm to identify both the Minimum and Maximum element in an unsorted list. Second, convert your recursive algorithm to a non-recursive (or iterative) implementation. For your input, populate an "unsorted list" with random elements between 1 and 1,000,000.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT