forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request eugenp#7995 from rodrigolgraciano/BAEL-2943
BAEL-2943
- Loading branch information
Showing
4 changed files
with
20 additions
and
47 deletions.
There are no files selected for viewing
12 changes: 8 additions & 4 deletions
12
java-numbers/src/main/java/com/baeldung/nth/root/calculator/NthRootCalculator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
package com.baeldung.nth.root.calculator; | ||
|
||
public class NthRootCalculator | ||
{ | ||
public Double calculate(Double base, Double n) { | ||
return Math.pow(Math.E, Math.log(base)/n); | ||
public class NthRootCalculator { | ||
|
||
public double calculateWithRound(double base, double n) { | ||
return Math.round(calculate(base, n)); | ||
} | ||
|
||
public double calculate(double base, double n) { | ||
return Math.pow(base, 1.0 / n); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 11 additions & 8 deletions
19
java-numbers/src/test/java/com/baeldung/nth/root/calculator/NthRootCalculatorUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,22 @@ | ||
package com.baeldung.nth.root.calculator; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class NthRootCalculatorUnitTest { | ||
|
||
private NthRootCalculator nthRootCalculator = new NthRootCalculator(); | ||
|
||
@Test | ||
public void whenBaseIs125AndNIs3_thenNthRootIs5() { | ||
Double result = nthRootCalculator.calculate(125.0, 3.0); | ||
assertEquals(result, (Double) 5.0d); | ||
public void whenBaseIs125AndNIs3_thenNthIs5() { | ||
double nth = nthRootCalculator.calculateWithRound(125,3); | ||
assertEquals(5, nth, 0); | ||
} | ||
|
||
@Test | ||
public void whenBaseIs625AndNIs4_thenNthIs5() { | ||
double nth = nthRootCalculator.calculate(625,4); | ||
assertEquals(5, nth, 0.00001); | ||
} | ||
} |
34 changes: 0 additions & 34 deletions
34
java-numbers/src/test/java/com/baeldung/nth/root/main/MainUnitTest.java
This file was deleted.
Oops, something went wrong.