Kotlinlearncs.online LogoJava
Return to List

Test Writing: Both Greater Comparable

Created By: Geoffrey Challen
/ Version: 2021.10.0

Create a class named BothGreater that stores two Int values set by the primary constructor. Neither should be publicly visible. BothGreater should also implement the Comparable<BothGreater> interface, returning 1 for a positive result and -1 for a negative result. An instance of BothGreater is greater than a second instance if both Int values are larger, and is lesser than if both Int values are smaller. Otherwise, compareTo should return 0.

You will probably need to review the documentation for Comparable. (Note that this is Java documentation.) Because we are using the type parameter BothGreater to the Comparable interface, compareTo accepts an BothGreater as an argument.

Test Design Challenge

You're challenge is to write tests for this problem described above.

  • Provide a public class named TestBothGreater with a single non-private class method named test that accepts no arguments and does not return a value.
  • If the implementation of the class described above is incorrect, your test method should throw an exception.
  • If it is correct, do not throw an exception.
  • You may want to use Kotlin's assert or check methods