Kotlinlearncs.online LogoJava
Return to List

Solve: Comparable Cat

Created By: Geoffrey Challen
/ Version: 2020.10.0

Create a class called Cat that implements the Comparable interface. Cat should provide a public constructor that takes a single double argument that sets that cat's age. You should assert that the provided age is not negative. You should order cats based on their age. Specifically, compareTo should return:

  • -1 if this cat is younger than the passed cat
  • 0 if this cat is the same age as the passed cat, or if the passed value is not a Cat
  • 1 if this cat is older than the passed cat

As a reminder, the Comparable interface comprises a single method: int compareTo(Object other).

Related Lessons

Stuck? You may find these lessons helpful: