Skip to content

Commit

Permalink
Merge pull request eugenp#7995 from rodrigolgraciano/BAEL-2943
Browse files Browse the repository at this point in the history
BAEL-2943
  • Loading branch information
maibin authored Oct 12, 2019
2 parents e08d6d1 + 83e74e8 commit 4d4dbcd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 47 deletions.
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public static void main(String[] args) {
NthRootCalculator calculator = new NthRootCalculator();
Double base = Double.parseDouble(args[0]);
Double n = Double.parseDouble(args[1]);
Double result = calculator.calculate(base, n);
Double result = calculator.calculateWithRound(base, n);
System.out.println("The " + n + " root of " + base + " equals to " + result + ".");
}
}
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);
}
}

This file was deleted.

0 comments on commit 4d4dbcd

Please sign in to comment.