From 5b78803c1fe11f524c3a776a93a60a13052c7ee1 Mon Sep 17 00:00:00 2001 From: Grigorios Dimopoulos Date: Sat, 22 Sep 2018 17:37:09 +0300 Subject: [PATCH] [BAEL2150] Code for the related article, about nth root in java. --- .../root/calculator/NthRootCalculator.java | 8 +++++ .../java/com/baeldung/nth/root/main/Main.java | 13 +++++++ .../calculator/NthRootCalculatorUnitTest.java | 21 ++++++++++++ .../baeldung/nth/root/main/MainUnitTest.java | 34 +++++++++++++++++++ 4 files changed, 76 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/nth/root/calculator/NthRootCalculator.java create mode 100644 core-java/src/main/java/com/baeldung/nth/root/main/Main.java create mode 100644 core-java/src/test/java/com/baeldung/nth/root/calculator/NthRootCalculatorUnitTest.java create mode 100644 core-java/src/test/java/com/baeldung/nth/root/main/MainUnitTest.java diff --git a/core-java/src/main/java/com/baeldung/nth/root/calculator/NthRootCalculator.java b/core-java/src/main/java/com/baeldung/nth/root/calculator/NthRootCalculator.java new file mode 100644 index 0000000000..217f1e06de --- /dev/null +++ b/core-java/src/main/java/com/baeldung/nth/root/calculator/NthRootCalculator.java @@ -0,0 +1,8 @@ +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); + } +} diff --git a/core-java/src/main/java/com/baeldung/nth/root/main/Main.java b/core-java/src/main/java/com/baeldung/nth/root/main/Main.java new file mode 100644 index 0000000000..3fcd36812f --- /dev/null +++ b/core-java/src/main/java/com/baeldung/nth/root/main/Main.java @@ -0,0 +1,13 @@ +package com.baeldung.nth.root.main; + +import com.baeldung.nth.root.calculator.NthRootCalculator; + +public class Main { + 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); + System.out.println("The " + n + " root of " + base + " equals to " + result + "."); + } +} diff --git a/core-java/src/test/java/com/baeldung/nth/root/calculator/NthRootCalculatorUnitTest.java b/core-java/src/test/java/com/baeldung/nth/root/calculator/NthRootCalculatorUnitTest.java new file mode 100644 index 0000000000..388bceef49 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/nth/root/calculator/NthRootCalculatorUnitTest.java @@ -0,0 +1,21 @@ +package com.baeldung.nth.root.calculator; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.runners.MockitoJUnitRunner; + +import static org.junit.Assert.assertEquals; + +@RunWith(MockitoJUnitRunner.class) +public class NthRootCalculatorUnitTest { + + @InjectMocks + private NthRootCalculator nthRootCalculator; + + @Test + public void giventThatTheBaseIs125_andTheExpIs3_whenCalculateIsCalled_thenTheResultIsTheCorrectOne() { + Double result = nthRootCalculator.calculate(125.0, 3.0); + assertEquals(result, (Double) 5.0d); + } +} diff --git a/core-java/src/test/java/com/baeldung/nth/root/main/MainUnitTest.java b/core-java/src/test/java/com/baeldung/nth/root/main/MainUnitTest.java new file mode 100644 index 0000000000..a2fd839ba4 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/nth/root/main/MainUnitTest.java @@ -0,0 +1,34 @@ +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", "")); + } +}