File tree Expand file tree Collapse file tree 4 files changed +20
-47
lines changed
main/java/com/baeldung/nth/root
test/java/com/baeldung/nth/root Expand file tree Collapse file tree 4 files changed +20
-47
lines changed Original file line number Diff line number Diff line change 1
1
package com .baeldung .nth .root .calculator ;
2
2
3
- public class NthRootCalculator
4
- {
5
- public Double calculate (Double base , Double n ) {
6
- return Math .pow (Math .E , Math .log (base )/n );
3
+ public class NthRootCalculator {
4
+
5
+ public double calculateWithRound (double base , double n ) {
6
+ return Math .round (calculate (base , n ));
7
+ }
8
+
9
+ public double calculate (double base , double n ) {
10
+ return Math .pow (base , 1.0 / n );
7
11
}
8
12
}
Original file line number Diff line number Diff line change @@ -7,7 +7,7 @@ public static void main(String[] args) {
7
7
NthRootCalculator calculator = new NthRootCalculator ();
8
8
Double base = Double .parseDouble (args [0 ]);
9
9
Double n = Double .parseDouble (args [1 ]);
10
- Double result = calculator .calculate (base , n );
10
+ Double result = calculator .calculateWithRound (base , n );
11
11
System .out .println ("The " + n + " root of " + base + " equals to " + result + "." );
12
12
}
13
13
}
Original file line number Diff line number Diff line change 1
1
package com .baeldung .nth .root .calculator ;
2
2
3
- import static org .junit .Assert .assertEquals ;
4
-
5
3
import org .junit .Test ;
6
- import org .junit .runner .RunWith ;
7
- import org .mockito .InjectMocks ;
8
- import org .mockito .junit .MockitoJUnitRunner ;
4
+
5
+ import static org .junit .Assert .assertEquals ;
9
6
10
7
public class NthRootCalculatorUnitTest {
11
8
12
9
private NthRootCalculator nthRootCalculator = new NthRootCalculator ();
13
10
14
11
@ Test
15
- public void whenBaseIs125AndNIs3_thenNthRootIs5 () {
16
- Double result = nthRootCalculator .calculate (125.0 , 3.0 );
17
- assertEquals (result , (Double ) 5.0d );
12
+ public void whenBaseIs125AndNIs3_thenNthIs5 () {
13
+ double nth = nthRootCalculator .calculateWithRound (125 ,3 );
14
+ assertEquals (5 , nth , 0 );
15
+ }
16
+
17
+ @ Test
18
+ public void whenBaseIs625AndNIs4_thenNthIs5 () {
19
+ double nth = nthRootCalculator .calculate (625 ,4 );
20
+ assertEquals (5 , nth , 0.00001 );
18
21
}
19
22
}
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments