beyondgrader.com Logo
DemoBrowseAboutTeamLogin

Comparable BiggerSmaller Counter

Geoffrey Challen // 2021.3.0

Complete the implementation of a public class named BiggerSmallerCounter. Provide a public constructor that accepts a single parameter, any Java object that implements Comparable. The parameter passed will not be null. This sets the initial threshold.

BiggerSmallerCounter should also provide the following instance methods:

  • add: takes a reference to any Java object that implements Comparable
  • bigger: retrieves the count (as an int) of the number of values passed to add that were bigger than the threshold set when the object was created
  • smaller: retrieves the count (as an int) of the number of values added to add that were smaller than the threshold set when the object was created

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.

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

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

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