Question: JAVA Programming As before: Add this to the cop2805 package. If you do not do so, do not worry (I will add it in), it

JAVA Programming

JAVA Programming As before: Add this to the cop2805 package. If youdo not do so, do not worry (I will add it in),

As before: Add this to the cop2805 package. If you do not do so, do not worry (I will add it in), it just hel Introduction We are going to write a function that utilizes generics Function will look like: public static >E min (E [] list) - This takes in a generic (E) that will extends the Comparable function (see last week's lecture) - Remember these functions show how to compare two items - Inside your code this allows access to the E.CompareTo() - Return a generic (E) - Function is named min - Input is an array of generic type E Objective: - This function will find and return the minimum element in the array Main Function - Create an array of String, Integer and Double - String[] colors = \{"Red","Green","Blue"\}; - Integer[] numbers ={1,2,3}; - Double[] circleRadius ={3.0,5.9,2.9}; - Pass each to the min() function that you are writing and print the output: - Example: System.out.println(min(colors)); - Will output "Blue" - Regardless of type, your function will find the minimum - String, Integer and Double already extend Comparable - Review how to find the minimum of something in an array... - Set minElement to the first index - Iterate in a for loop comparing the current element to minElement - If the current element of the array is less than minElement, update minElement to that element - Return minElement - How to do the comparison without knowing the type? - You can call .CompareTo() which will return.... - Negative number if less than the parameter object - Zero if equal to the parameter object - Positive number if greater than the parameter object - Hint: list[i].CompareTo(minElement) > 0 will be a useful check inside your loop - Copy the function exactly as presented - public static >E min (E [] list) - You are not creating a class with generics - Rather just a function that uses them - Inside the function you can declare and make use of the E data type - The E data type is a mystery to you - But thanks to the .CompareTo() function you can figure out what the minimum element is in the array Here is a sample output: Colors: Blue Numbers: 1 Circle Radius: 2.9

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!