Question

Please use java language in an easy way and comment as much as you can! Thanks...

Please use java language in an easy way and comment as much as you can! Thanks

  1. (Write a code question) Write a generic method called "findMin" that takes an array of Comparable objects as a parameter. (Use proper syntax, so that no type checking warnings are generated by the compiler.) The method should search the array and return the index of the smallest value.

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

//TestCode.java
public class TestCode {
    public static <E extends Comparable<E>> E findMin(E[] arr) {
        E max = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (arr[i].compareTo(max) < 0) {
                max = arr[i];
            }
        }
        return max;
    }

    public static void main(String[] args)
    {
        String arr[] = {"ghi","xyz","abc","def"};
        System.out.println(findMin(arr));
    }
}
public static <E extends Comparable<E>> E findMax(E[] arr) {
    E max = arr[0];
    for (int i = 1; i < arr.length; i++) {
        if (arr[i].compareTo(max) > 0) {
            max = arr[i];
        }
    }
    return max;
}
Add a comment
Know the answer?
Add Answer to:
Please use java language in an easy way and comment as much as you can! Thanks...
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
  • Please Do It With Java. Make it copy paste friendly so i can run and test...

    Please Do It With Java. Make it copy paste friendly so i can run and test it. Thnak You. Write programs for challenges 1 and 2 and 6 at the end of the chapter on Generics. 1) Write a generic class named MyList, with a type parameter T. The type parameter T should be constrained to an upper bound: the Number class. The class should have as a field an ArrayList of T. Write a public method named add, which...

  • please use c ++ language and write the comment too. thanks Pointer Arithmetic Write a function...

    please use c ++ language and write the comment too. thanks Pointer Arithmetic Write a function that passes an array address to a function as a pointer. Using pointer arithmetic, the function determines the average, high, and low value of the array. Your function should have this header: void pointerArithmetic(int *array, int size, double &average, int &high, int &low) Parameter size is the number of elements in array. You may not use the subscript notation [] inside your function –...

  • JAVA Question a) Write a generic method public static <...> Collection<Pair<...>> sortPairCollection(Collection <Pair<....>> col) in a...

    JAVA Question a) Write a generic method public static <...> Collection<Pair<...>> sortPairCollection(Collection <Pair<....>> col) in a Utils class that takes as parameter a collection of Pair<K,V> objects and returns a new collection object (use ArrayList<...>) with the pair elements from collection col sorted in ascending order. For comparing pairs, the K type must implement Comparable<....> Use the proper type constraints. b) Write a main() function in Utils.java that tests the sortPairCollection() with K=String and V=Integer.

  • Loops | Methods | Arrays(programming language java) in software (ATOM) Write a toolkit that includes some...

    Loops | Methods | Arrays(programming language java) in software (ATOM) Write a toolkit that includes some common functions a user might want or need An encryption/ decryption method. Both methods should take in a String (the String that needs to be changed), and an encryption value. The methods need to change the String by the value Write a method that takes an integer array as an argument. The method should return the sum of all of the elements Write a...

  • This is in java language. Please use simple java programming language. Each of the parts a....

    This is in java language. Please use simple java programming language. Each of the parts a. through c. below is preceded by a comment indicating what the code do. There is a least one problem with each section of code and it fails to do what was inte Show how to modify or rewrite the code so that it works as intended. Assume that all varia used have already been declared. а. // INTENT : given an array arr of...

  • Q 4(b) 6 Marks] Write the code for a single Java class of your own choice that demonstrates the use of the following features in the Java language: Method overloadingg The use of super and this Ident...

    Q 4(b) 6 Marks] Write the code for a single Java class of your own choice that demonstrates the use of the following features in the Java language: Method overloadingg The use of super and this Identify clearly where in your code each of these points is being demonstrated Q 4(c) [6 Marks] Write an abstract Student class in Java which has a name, id_number, year and programme_code as member variables. Add a constructor to your class and a display()...

  • In Java programming language Please write code for the 6 methods below: Assume that Student class...

    In Java programming language Please write code for the 6 methods below: Assume that Student class has name, age, gpa, and major, constructor to initialize all data, method toString() to return string reresentation of student objects, getter methods to get age, major, and gpa, setter method to set age to given input, and method isHonors that returns boolean value true for honors students and false otherwise. 1) Write a method that accepts an array of student objects, and n, the...

  • Please solve this C language progrm (2-D Array program) the most basic way ! Thank You!...

    Please solve this C language progrm (2-D Array program) the most basic way ! Thank You! Write a function that takes a 4 x 4 array of integers as its parameter. The function must use two nested for-loops to determine the largest integer in the array as well as its row and column numbers. Recall that C-functions cannot return multiple values. Use pointers in the function parameter list to relay the results to the caller program. Write another function, which...

  • Write code in Java programming language. The ShoppingCart class will be composed with an array of...

    Write code in Java programming language. The ShoppingCart class will be composed with an array of Item objects. You do not need to implement the Item class for this question, only use it. Item has a getPrice() method that returns a float and a one-argument constructor that takes a float that specifies the price. The ShoppingCart class should have: Constructors: A no-argument and a single argument that takes an array of Items. Fields: • Items: an array of Item objects...

  • This is JAVA language Create a .java class called MoreArrayFun.java with only a main method. This...

    This is JAVA language Create a .java class called MoreArrayFun.java with only a main method. This program will use the ArrayManager class. The final version of the program is described below, but initially use it to test your ArrayManager class as you write it. Complete the ArrayManager class as specified below: The class will have exactly two instance variables:  An array of integers  A count of the number of elements currently in the array The class will have...

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