Skip to content

Commit 5a11583

Browse files
add Factorial
1 parent 07d31b1 commit 5a11583

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

build.gradle

+5
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,10 @@ repositories {
1010
}
1111

1212
dependencies {
13+
implementation 'org.junit.jupiter:junit-jupiter:5.4.2'
1314
testCompile group: 'junit', name: 'junit', version: '4.12'
1415
}
16+
17+
test{
18+
useJUnitPlatform()
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.examplehub.basics;
2+
3+
public class HelloWorld {
4+
public static void main(String[] args) {
5+
System.out.println("Hello World from ExampleHub");
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.examplehub.maths;
2+
3+
public class Factorial {
4+
public static long factorial(int number) {
5+
if (number < 0) {
6+
throw new ArithmeticException(number + " is negative");
7+
}
8+
long fact = 1;
9+
for (int i = 1; i <= number; ++i) {
10+
fact *= i;
11+
}
12+
return fact;
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.examplehub.maths;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
class FactorialTest {
8+
9+
@Test
10+
void testFactorial() {
11+
try {
12+
long fact = Factorial.factorial(-1);
13+
fail(); /* will not happen */
14+
} catch (ArithmeticException e) {
15+
assertTrue(true); /* will happen */
16+
}
17+
18+
int[][] testNumbers = {{0, 1}, {1, 1}, {2, 2}, {3, 6}, {4, 24}, {5, 120}};
19+
for (int[] testNumber : testNumbers) {
20+
assertEquals(Factorial.factorial(testNumber[0]), testNumber[1]);
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)