Skip to content

Commit 83e74e8

Browse files
Rodrigo GracianoRodrigo Graciano
authored andcommitted
BAEL-2943
1 parent 31b573d commit 83e74e8

File tree

4 files changed

+20
-47
lines changed

4 files changed

+20
-47
lines changed
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package com.baeldung.nth.root.calculator;
22

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);
711
}
812
}

java-numbers/src/main/java/com/baeldung/nth/root/main/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public static void main(String[] args) {
77
NthRootCalculator calculator = new NthRootCalculator();
88
Double base = Double.parseDouble(args[0]);
99
Double n = Double.parseDouble(args[1]);
10-
Double result = calculator.calculate(base, n);
10+
Double result = calculator.calculateWithRound(base, n);
1111
System.out.println("The " + n + " root of " + base + " equals to " + result + ".");
1212
}
1313
}
Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
package com.baeldung.nth.root.calculator;
22

3-
import static org.junit.Assert.assertEquals;
4-
53
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;
96

107
public class NthRootCalculatorUnitTest {
118

129
private NthRootCalculator nthRootCalculator = new NthRootCalculator();
1310

1411
@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);
1821
}
1922
}

java-numbers/src/test/java/com/baeldung/nth/root/main/MainUnitTest.java

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)