Skip to content

Commit 0f18f28

Browse files
authored
Merge pull request eugenp#5307 from grigoriosdimopoulos/BAEL2150
[BAEL2150] Nth root in Java
2 parents d2ef471 + 5b78803 commit 0f18f28

4 files changed

Lines changed: 76 additions & 0 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.nth.root.calculator;
2+
3+
public class NthRootCalculator
4+
{
5+
public Double calculate(Double base, Double n) {
6+
return Math.pow(Math.E, Math.log(base)/n);
7+
}
8+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.nth.root.main;
2+
3+
import com.baeldung.nth.root.calculator.NthRootCalculator;
4+
5+
public class Main {
6+
public static void main(String[] args) {
7+
NthRootCalculator calculator = new NthRootCalculator();
8+
Double base = Double.parseDouble(args[0]);
9+
Double n = Double.parseDouble(args[1]);
10+
Double result = calculator.calculate(base, n);
11+
System.out.println("The " + n + " root of " + base + " equals to " + result + ".");
12+
}
13+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.nth.root.calculator;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.mockito.InjectMocks;
6+
import org.mockito.runners.MockitoJUnitRunner;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
@RunWith(MockitoJUnitRunner.class)
11+
public class NthRootCalculatorUnitTest {
12+
13+
@InjectMocks
14+
private NthRootCalculator nthRootCalculator;
15+
16+
@Test
17+
public void giventThatTheBaseIs125_andTheExpIs3_whenCalculateIsCalled_thenTheResultIsTheCorrectOne() {
18+
Double result = nthRootCalculator.calculate(125.0, 3.0);
19+
assertEquals(result, (Double) 5.0d);
20+
}
21+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.nth.root.main;
2+
3+
import java.io.ByteArrayOutputStream;
4+
import java.io.PrintStream;
5+
6+
import org.junit.After;
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
import org.mockito.InjectMocks;
10+
import static org.junit.Assert.assertEquals;
11+
12+
public class MainUnitTest {
13+
@InjectMocks
14+
private Main main;
15+
16+
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
17+
private final PrintStream originalOut = System.out;
18+
19+
@Before
20+
public void setUpStreams() {
21+
System.setOut(new PrintStream(outContent));
22+
}
23+
24+
@After
25+
public void restoreStreams() {
26+
System.setOut(originalOut);
27+
}
28+
29+
@Test
30+
public void givenThatTheBaseIs125_andTheExpIs3_whenMainIsCalled_thenTheCorrectResultIsPrinted() {
31+
main.main(new String[]{"125.0", "3.0"});
32+
assertEquals("The 3.0 root of 125.0 equals to 5.0.\n", outContent.toString().replaceAll("\r", ""));
33+
}
34+
}

0 commit comments

Comments
 (0)