Kotlinlearncs.online LogoJava
Return to List

Test Writing: Last 4 In Order

Created By: Geoffrey Challen
/ Version: 2020.9.1

Create a public class called Last4Ordered. You should expose two public methods:

  • add: adds a value, does not return a value
  • last: returns an array containing the last 4 values that were added in the order they were added.

You do not need a constructor, but you can add an empty one if you need. Until 4 values have been added you should return 0s in their place.

For example, here's how an instance of Last4Ordered should work:

Do not create a huge array to save the values. Submissions that do will be marked incorrect.

This problem is harder than it looks! A suggestion is to handle the cases until the array is initially filled separately. Once four values have been added, you will need to shift values around before adding a new one.

Test Design Challenge

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

  • Provide a public class named TestLast4Ordered 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