beyondgrader.com Logo
DemoBrowseAboutTeamLogin

Comparable MinMax

Geoffrey Challen // 2020.10.0

Complete the implementation of a public class named MinMax. MinMax should provide the following instance methods:

  • add: takes a reference to any Java object that implements Comparable
  • min: takes no arguments and returns a Comparable reference that is the minimum of all the values that have been previously passed to add
  • max: takes no arguments and returns a Comparable reference that is the maximum of all the values that have been previously passed to add

You should compare Comparable objects using compareTo. (You do not need to handle specially the case where different Comparable classes cannot be compared to each other.) 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 0 if they are equal.

MinMax should also provide a single public constructor that sets the initial min and max to a passed Comparable argument.

When you are done your MinMax class should work as follows:

Note that null will not be passed as an argument to your constructor or to add.

You should not use an array to solve this problem. You will lose credit for doing so.