6.1k views
0 votes
Create a class called Minimum that provides a single class method min. min should take an array of any object that implements Comparable and return the minimum value from the array, using compareTo to compare object instances. So the function signature of min should be public static Comparable min(Comparable[] values) As a reminder, first.compareTo(second) returns a positive value if first is larger than second, a negative value if first is smaller than second, and O if they are equal. Note that the array of Comparable references passed to min will all be the same type and contain no null references. However, the array may be null or empty in which case you should return nul1 Note that the warning about unchecked or unsafe operations is normal and not a sign that you did something wrong. We'll discuss this soon in class.) Minimum.java

1 Answer

4 votes

Answer:

The required code is given below:

Step-by-step explanation:

public class Minimum {

public static Comparable min(Comparable[] values) {

if (values == null || values.length == 0) {

return null;

} else {

Comparable minValue = values[0];

for (int i = 0; i < values.length; i++) {

if (values[i].compareTo(minValue) < 0) {

minValue = values[i];

}

}

return minValue;

}

}

}

User Blazeroni
by
3.7k points