Question

This is a short answer question Implement the following method that returns the maximum element in...

This is a short answer question

Implement the following method that returns the maximum element in an array. (6 pts)

public static <T extends Comparable<E>> E MaxInArray(T[] array)

{ … }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

Add a comment
Know the answer?
Add Answer to:
This is a short answer question Implement the following method that returns the maximum element in...
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
  • "Java question" Implement a generic method that returns the minimum element in an array.        public...

    "Java question" Implement a generic method that returns the minimum element in an array.        public static<EextendsComparable<E>> E min(E[ ] list)

  • Write a method max() which take a generic singly linked list as an argument and returns...

    Write a method max() which take a generic singly linked list as an argument and returns the maximum element reference in the list.      If the list is empty, please throw an empty collection exception.      The method prototype is defined as follows. Please write down your code in the method body enclosed in braces: public static <T extends Comparable<T>> T max(SingleLinkedList<T> list) throws EmptyCollectionException { } 2) What is the big O notation of the max method? a) O(N)...

  • public class SelectionSorter { //Returns the index of the largest element in the arrayOfIntegers, beginning from...

    public class SelectionSorter { //Returns the index of the largest element in the arrayOfIntegers, beginning from the fromIndex. public static Integer[] selectSort(Integer[] incoming) {        Integer[] ret = new Integer[incoming.length]; for (int i = 0; i < incoming.length; i++) {            ret[i] = incoming[i];        }        int temp = 0;        for (int i = 0; i < ret.length - 1; i++) {            if (ret[i] > ret[i + 1]) {...

  • Can someone help me with the main class of my code. Here is the assignment notes....

    Can someone help me with the main class of my code. Here is the assignment notes. Implement project assignment �1� at the end of chapter 18 on page 545 in the textbook. Use the definition shown below for the "jumpSearch" method of the SkipSearch class. Notice that the method is delcared static. Therefore, you do not need to create a new instance of the object before the method is called. Simply use "SkipSearch.jumpSearch(...)" with the appropriate 4 parameter values. When...

  • Check if an array is a heap in Java. Complete the method isHeapTree Together, these methods...

    Check if an array is a heap in Java. Complete the method isHeapTree Together, these methods are meant to determine if an array of objects corresponds to a heap. The methods are generic, and they should work with an array of any type T that implement Comparable<T>. Implement the second method so that it uses recursion to process the complete tree/subtree whose root is at position i in the array arr. The method should return true if that tree/subtree is...

  • *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 =...

  • You must implement the following methods using Java's Stack Object. /**    * Computes the factorial of...

    You must implement the following methods using Java's Stack Object. /**    * Computes the factorial of n    * @param n-integer value greater or equal to 0    * @return n!    */    public static int factorial( int n ) { } /**    * Computes the nth term of the Fibonacci sequence    * @param n -nth term to find    * @return -the nth term    */    public static int fibonacci( int n ) {}    /**    * Find the min value using the comparable interface...

  • please answer in java and make possible to copy and paste. thank you!!!!! Re-implement the Rational...

    please answer in java and make possible to copy and paste. thank you!!!!! Re-implement the Rational class ****JAVA**** Extend the Number class and implement the Comparable interface to introduce new functionality to your Rational class. The class definition should look like this: public class Rational extends Number implements Comparable<Rational> { // code goes here } In addition a new private method gcd should be defined to ensure that the number is always represented in the lowest terms. Here is the...

  • Lab #4 – Recursive Methods for Generic ArrayList ----- JAVA The problem Use recursion to implement...

    Lab #4 – Recursive Methods for Generic ArrayList ----- JAVA The problem Use recursion to implement some list functionalities on an ArrrayList of generic type using type parameters in method definition Our goal for this lab is to continue using the divide and conquer approach of splitting a list into its head and tail and keep recursing on the tail (which happens to be smaller). However, instead of trying the approach on a string (a list of characters) we would...

  • Consider the following recursive method for finding the maximum element in an int array: public int...

    Consider the following recursive method for finding the maximum element in an int array: public int static max(int[] a, int lo, int hi) { if (lo > hi) return a[lo]; int mid = (lo + hi) / 2; int loMax = max(a, lo, mid); int hiMax = max(a, mid+1, hi); if (loMax > hiMax) return loMax; else return hiMax; } Write down the recurrence relation for counting the number of times the comparison if (loMax > hiMax) is performed. Use...

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