beyondgrader.com Logo
DemoBrowseAboutTeamLogin

OurComparable Dog

Geoffrey Challen // 2023.10.0

Create a class called Dog that implements the OurComparable interface. Dog should provide a public constructor that takes a single Double argument that sets that dog's weight. You should assert that the provided weight is greater than zero. Do not expose the weight property.

You should order dogs based on their weight from lightest to heaviest. Specifically, compareTo should return:

  • -1 if this dog is lighter than the passed dog
  • 0 if this dog is the same weight as the passed dog
  • 1 if this dog is heavier than the passed dog

If the passed value is not a Dog, throw an IllegalArgumentException.

As a reminder, the OurComparable interface comprises a single method: fun compareTo(other: Any?): Int.