Kotlinlearncs.online LogoJava
Return to List

Test Writing: Sum Array Odds

Created By: Rohith Sanjay
/ Version: 2024.9.1

Write a method sumArrayOdds that takes in one argument: an array of ints called input. This method will return the "Odd Sum", as described below. The "Odd Sum" is the sum of the odd elements with odd indices in the array. So for example, if input = [3, 3, 3, 4, 4], then sumArrayOdds will return 3, because the second element (index 1) is the only element with an odd value (3) and odd index (1), so it is the only element in the sum. If input = [1, 1, 1, 1, 1], then sumArrayOdds will return 2. If input is null or empty, then return 0. Remember that an array is zero-indexed, so the first element has index 0, the second element has index 1, and so on.

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
Report a Problem

Attribution must link to this page: https://www.learncs.online/testtesting/java/sum-array-odds/[email protected]