Kotlinlearncs.online LogoJava
Return to List

Test Writing: Array Range Sum

Created By: Geoffrey Challen
/ Version: 2022.1.0

Write a method named arrayRangeSum. It receives an int[] and a positive int range as parameters. You should return the sum of all the elements in the array that are between range and range * -1, non-inclusive.

For example, given the array {1, -4, 2, 24, -124} and the range 8, you would return -1: 1 + -4 + 2, since these are the values in the array strictly greater than -8 and strictly less than 8. Given the range 124, you should return 23: 1 + -4 + 2 + 24, since these are the values in the array strictly greater than -124 and less than 124.

Note that this problem is a great fit for the enhanced for loop!

Test Design Challenge

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

  • Provide a 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 Java's assert method