BAEL-2943
This commit is contained in:
@@ -1,8 +1,12 @@
|
|||||||
package com.baeldung.nth.root.calculator;
|
package com.baeldung.nth.root.calculator;
|
||||||
|
|
||||||
public class NthRootCalculator
|
public class NthRootCalculator {
|
||||||
{
|
|
||||||
public Double calculate(Double base, Double n) {
|
public double calculateWithRound(double base, double n) {
|
||||||
return Math.pow(Math.E, Math.log(base)/n);
|
return Math.round(calculate(base, n));
|
||||||
|
}
|
||||||
|
|
||||||
|
public double calculate(double base, double n) {
|
||||||
|
return Math.pow(base, 1.0 / n);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ public class Main {
|
|||||||
NthRootCalculator calculator = new NthRootCalculator();
|
NthRootCalculator calculator = new NthRootCalculator();
|
||||||
Double base = Double.parseDouble(args[0]);
|
Double base = Double.parseDouble(args[0]);
|
||||||
Double n = Double.parseDouble(args[1]);
|
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 + ".");
|
System.out.println("The " + n + " root of " + base + " equals to " + result + ".");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,19 +1,22 @@
|
|||||||
package com.baeldung.nth.root.calculator;
|
package com.baeldung.nth.root.calculator;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.mockito.InjectMocks;
|
import static org.junit.Assert.assertEquals;
|
||||||
import org.mockito.junit.MockitoJUnitRunner;
|
|
||||||
|
|
||||||
public class NthRootCalculatorUnitTest {
|
public class NthRootCalculatorUnitTest {
|
||||||
|
|
||||||
private NthRootCalculator nthRootCalculator = new NthRootCalculator();
|
private NthRootCalculator nthRootCalculator = new NthRootCalculator();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenBaseIs125AndNIs3_thenNthRootIs5() {
|
public void whenBaseIs125AndNIs3_thenNthIs5() {
|
||||||
Double result = nthRootCalculator.calculate(125.0, 3.0);
|
double nth = nthRootCalculator.calculateWithRound(125,3);
|
||||||
assertEquals(result, (Double) 5.0d);
|
assertEquals(5, nth, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenBaseIs625AndNIs4_thenNthIs5() {
|
||||||
|
double nth = nthRootCalculator.calculate(625,4);
|
||||||
|
assertEquals(5, nth, 0.00001);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,34 +0,0 @@
|
|||||||
package com.baeldung.nth.root.main;
|
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.io.PrintStream;
|
|
||||||
|
|
||||||
import org.junit.After;
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.mockito.InjectMocks;
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
|
|
||||||
public class MainUnitTest {
|
|
||||||
@InjectMocks
|
|
||||||
private Main main;
|
|
||||||
|
|
||||||
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
|
|
||||||
private final PrintStream originalOut = System.out;
|
|
||||||
|
|
||||||
@Before
|
|
||||||
public void setUpStreams() {
|
|
||||||
System.setOut(new PrintStream(outContent));
|
|
||||||
}
|
|
||||||
|
|
||||||
@After
|
|
||||||
public void restoreStreams() {
|
|
||||||
System.setOut(originalOut);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenThatTheBaseIs125_andTheExpIs3_whenMainIsCalled_thenTheCorrectResultIsPrinted() {
|
|
||||||
main.main(new String[]{"125.0", "3.0"});
|
|
||||||
assertEquals("The 3.0 root of 125.0 equals to 5.0.\n", outContent.toString().replaceAll("\r", ""));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user