beyondgrader.com Logo
DemoBrowseAboutTeamLogin

Car Comparable

Geoffrey Challen // 2020.10.0

Create a class called Car that implements the Comparable interface. Car should provide a public constructor that takes a single int argument that sets that car's odometer. You should assert that the provided int is not negative. You should order cars based on their odometer reading. Specifically, compareTo should return:

  • -1 if this car has a lower odometer reading than the passed car
  • 0 if this car has the same odometer reading as the passed car, or if the passed value is not a Car
  • 1 if this car has a higher odometer reading than the passed car

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