File tree Expand file tree Collapse file tree 2 files changed +41
-0
lines changed
03_recursion/java/03_factorial/src Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change
1
+ public class Factorial2 {
2
+ public static void main (String [] args ) {
3
+ Factorial2 factorial2 = new Factorial2 ();
4
+ System .out .println ("The factorial of 5 is " + factorial2 .getFactorial (5 ));
5
+ }
6
+
7
+ public int getFactorial (int number ) {
8
+ if (isZeroOrOne (number )) {
9
+ return 1 ;
10
+ }
11
+
12
+ return number * getFactorial (number - 1 );
13
+ }
14
+
15
+ public boolean isZeroOrOne (int number ) {
16
+ if (number > 1 ) {
17
+ return false ;
18
+ }
19
+ return true ;
20
+ }
21
+ }
Original file line number Diff line number Diff line change
1
+ import org .junit .Assert ;
2
+ import org .junit .Test ;
3
+
4
+ public class Factorial2Test {
5
+ @ Test
6
+ public void testIsZeroOrOne () {
7
+ Factorial2 factorial2 = new Factorial2 ();
8
+
9
+ Assert .assertEquals (true , factorial2 .isZeroOrOne (0 ));
10
+ Assert .assertEquals (true , factorial2 .isZeroOrOne (1 ));
11
+ Assert .assertEquals (false , factorial2 .isZeroOrOne (5 ));
12
+ }
13
+
14
+ @ Test
15
+ public void testFactorial () {
16
+ Factorial2 factorial2 = new Factorial2 ();
17
+
18
+ Assert .assertEquals (120 , factorial2 .getFactorial (5 ));
19
+ }
20
+ }
You can’t perform that action at this time.
0 commit comments