Skip to content
Open

Test #138

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion app/src/main/java/control/Single.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,15 @@ public static int maxArray(int[] arr) {
* This method calculates the sum of the first n natural numbers, modulo m.
*
* @param n The number of natural numbers to sum.
* @param m The modulus.
* @param m The non-zero modulus.
* @throws IllegalArgumentException if m is zero.
*/
public static int sumModulus(int n, int m) {
// Validate first so modulo by zero produces a clearer exception than % would.
if (m == 0) {
throw new IllegalArgumentException("Modulus must be non-zero");
}

Vector<Integer> multiples = new Vector<Integer>();
for (int i = 0; i < n; i++) {
if (i % m == 0) {
Expand Down
2 changes: 2 additions & 0 deletions app/src/test/java/control/SingleTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package control;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -33,5 +34,6 @@ public void testSumModulus() {
assertEquals(20, Single.sumModulus(10, 2));
assertEquals(18, Single.sumModulus(10, 3));
assertEquals(12, Single.sumModulus(10, 4));
assertThrows(IllegalArgumentException.class, () -> Single.sumModulus(10, 0));
}
}