From d5a11f29a4219601a091b470c0df2498bc1e84a3 Mon Sep 17 00:00:00 2001 From: marcos-x86 Date: Sat, 2 Sep 2017 18:37:42 -0400 Subject: [PATCH 1/4] Package reorganization --- .../fundacionjala/coding/{marcoslara => marcos}/FizzBuzz.java | 2 +- .../coding/{marcoslara => marcos}/FizzBuzzTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename src/main/java/org/fundacionjala/coding/{marcoslara => marcos}/FizzBuzz.java (97%) rename src/test/java/org/fundacionjala/coding/{marcoslara => marcos}/FizzBuzzTest.java (98%) diff --git a/src/main/java/org/fundacionjala/coding/marcoslara/FizzBuzz.java b/src/main/java/org/fundacionjala/coding/marcos/FizzBuzz.java similarity index 97% rename from src/main/java/org/fundacionjala/coding/marcoslara/FizzBuzz.java rename to src/main/java/org/fundacionjala/coding/marcos/FizzBuzz.java index f985669..c673e21 100644 --- a/src/main/java/org/fundacionjala/coding/marcoslara/FizzBuzz.java +++ b/src/main/java/org/fundacionjala/coding/marcos/FizzBuzz.java @@ -1,4 +1,4 @@ -package org.fundacionjala.coding.marcoslara; +package org.fundacionjala.coding.marcos; /** * Created by Marcos on 8/25/2017. diff --git a/src/test/java/org/fundacionjala/coding/marcoslara/FizzBuzzTest.java b/src/test/java/org/fundacionjala/coding/marcos/FizzBuzzTest.java similarity index 98% rename from src/test/java/org/fundacionjala/coding/marcoslara/FizzBuzzTest.java rename to src/test/java/org/fundacionjala/coding/marcos/FizzBuzzTest.java index 767d225..52f9e68 100644 --- a/src/test/java/org/fundacionjala/coding/marcoslara/FizzBuzzTest.java +++ b/src/test/java/org/fundacionjala/coding/marcos/FizzBuzzTest.java @@ -1,4 +1,4 @@ -package org.fundacionjala.coding.marcoslara; +package org.fundacionjala.coding.marcos; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; From eba2f7781d72dc5ac60727f55b4eb962639e7fa8 Mon Sep 17 00:00:00 2001 From: marcos-x86 Date: Sat, 2 Sep 2017 19:37:41 -0400 Subject: [PATCH 2/4] Magnet Particules Kata solution implemented --- .../fundacionjala/coding/marcos/Magnets.java | 41 +++++++++++++ .../coding/marcos/MagnetsTest.java | 61 +++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 src/main/java/org/fundacionjala/coding/marcos/Magnets.java create mode 100644 src/test/java/org/fundacionjala/coding/marcos/MagnetsTest.java diff --git a/src/main/java/org/fundacionjala/coding/marcos/Magnets.java b/src/main/java/org/fundacionjala/coding/marcos/Magnets.java new file mode 100644 index 0000000..c6a8c69 --- /dev/null +++ b/src/main/java/org/fundacionjala/coding/marcos/Magnets.java @@ -0,0 +1,41 @@ +package org.fundacionjala.coding.marcos; + +import java.util.stream.IntStream; + +/** + * Created by Marcos. + */ + +public final class Magnets { + + /** + * Private constructor for the Magnets utility class. + */ + private Magnets() { + + } + + /** + * This method calculates the total force exerted by rows and columns using the + * kata summation equation. + * + * @param maxK max column value. + * @param maxN max row value. + * @return the summation result. + */ + public static double doubles(int maxK, int maxN) { + return IntStream.rangeClosed(1, maxK).mapToDouble(k -> calculateForce(k, maxN)).sum(); + } + + /** + * This method calculates the total force exerted by a row using the Kata + * summation equation. + * + * @param k fixed column value. + * @param maxN max row value. + * @return the summation result. + */ + private static double calculateForce(int k, int maxN) { + return IntStream.rangeClosed(1, maxN).mapToDouble(n -> 1 / (k * Math.pow(n + 1, 2 * k))).sum(); + } +} diff --git a/src/test/java/org/fundacionjala/coding/marcos/MagnetsTest.java b/src/test/java/org/fundacionjala/coding/marcos/MagnetsTest.java new file mode 100644 index 0000000..7c40b89 --- /dev/null +++ b/src/test/java/org/fundacionjala/coding/marcos/MagnetsTest.java @@ -0,0 +1,61 @@ +package org.fundacionjala.coding.marcos; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Modifier; +import java.text.DecimalFormat; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * Created by Marcos. + */ +public class MagnetsTest { + + /** + * Custom assertion method for the kata. + * + * @param act actual result. + * @param exp expected result. + */ + private static void assertFuzzyEquals(double act, double exp) { + boolean inRange = Math.abs(act - exp) <= 1e-6; + if (!inRange) { + DecimalFormat df = new DecimalFormat("#0.000000"); + System.out.println("At 1e-6: Expected must be " + df.format(exp) + ", but got " + df.format(act)); + } + assertEquals(true, inRange); + } + + /** + * This test assert the modifier of the Sequence private constructor class. + * + * @throws NoSuchMethodException throw when no private constructor is defined. + * @throws IllegalAccessException throw when can not access to the constructor. + * @throws InvocationTargetException throw when can not be perform a invocation. + * @throws InstantiationException throw when a instantiation can not be performed. + */ + @Test + public void privateConstructorTest() throws NoSuchMethodException, IllegalAccessException, + InvocationTargetException, InstantiationException { + Constructor constructor = FizzBuzz.class.getDeclaredConstructor(); + assertTrue(Modifier.isPrivate(constructor.getModifiers())); + constructor.setAccessible(true); + constructor.newInstance(); + } + + /** + * Basic test for Magnet particles in boxes kata. + */ + @Test + public void test1() { + System.out.println("Fixed Tests: doubles"); + assertFuzzyEquals(Magnets.doubles(1, 10), 0.5580321939764581); + assertFuzzyEquals(Magnets.doubles(10, 1000), 0.6921486500921933); + assertFuzzyEquals(Magnets.doubles(10, 10000), 0.6930471674194457); + assertFuzzyEquals(Magnets.doubles(20, 10000), 0.6930471955575918); + } +} From 3e3377e4b92bbaee9f56ef45bf0eb8c9cb500ae2 Mon Sep 17 00:00:00 2001 From: marcos-x86 Date: Mon, 4 Sep 2017 02:39:14 -0400 Subject: [PATCH 3/4] Constants added and Test Coverage incremented --- .../java/org/fundacionjala/coding/marcos/Magnets.java | 9 ++++++++- .../org/fundacionjala/coding/marcos/MagnetsTest.java | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/fundacionjala/coding/marcos/Magnets.java b/src/main/java/org/fundacionjala/coding/marcos/Magnets.java index c6a8c69..bd7e835 100644 --- a/src/main/java/org/fundacionjala/coding/marcos/Magnets.java +++ b/src/main/java/org/fundacionjala/coding/marcos/Magnets.java @@ -4,10 +4,17 @@ /** * Created by Marcos. + *

+ * Professor Chambouliard hast just discovered a new type of magnet material. + * He put particles of this material in a box made of small boxes arranged + * in K rows and N columns as a kind of 2D matrix K x N where K and N are positive integers. */ public final class Magnets { + public static final int ONE = 1; + public static final int TWO = 2; + /** * Private constructor for the Magnets utility class. */ @@ -36,6 +43,6 @@ public static double doubles(int maxK, int maxN) { * @return the summation result. */ private static double calculateForce(int k, int maxN) { - return IntStream.rangeClosed(1, maxN).mapToDouble(n -> 1 / (k * Math.pow(n + 1, 2 * k))).sum(); + return IntStream.rangeClosed(ONE, maxN).mapToDouble(n -> ONE / (k * Math.pow(n + ONE, TWO * k))).sum(); } } diff --git a/src/test/java/org/fundacionjala/coding/marcos/MagnetsTest.java b/src/test/java/org/fundacionjala/coding/marcos/MagnetsTest.java index 7c40b89..c34c0de 100644 --- a/src/test/java/org/fundacionjala/coding/marcos/MagnetsTest.java +++ b/src/test/java/org/fundacionjala/coding/marcos/MagnetsTest.java @@ -41,7 +41,7 @@ private static void assertFuzzyEquals(double act, double exp) { @Test public void privateConstructorTest() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { - Constructor constructor = FizzBuzz.class.getDeclaredConstructor(); + Constructor constructor = Magnets.class.getDeclaredConstructor(); assertTrue(Modifier.isPrivate(constructor.getModifiers())); constructor.setAccessible(true); constructor.newInstance(); From 64ed292a7a03c0d24e6823702000fbfd078fd1b1 Mon Sep 17 00:00:00 2001 From: marcos-x86 Date: Mon, 4 Sep 2017 02:46:40 -0400 Subject: [PATCH 4/4] Test for high K value added --- src/test/java/org/fundacionjala/coding/marcos/MagnetsTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/org/fundacionjala/coding/marcos/MagnetsTest.java b/src/test/java/org/fundacionjala/coding/marcos/MagnetsTest.java index c34c0de..40f99d0 100644 --- a/src/test/java/org/fundacionjala/coding/marcos/MagnetsTest.java +++ b/src/test/java/org/fundacionjala/coding/marcos/MagnetsTest.java @@ -57,5 +57,6 @@ public void test1() { assertFuzzyEquals(Magnets.doubles(10, 1000), 0.6921486500921933); assertFuzzyEquals(Magnets.doubles(10, 10000), 0.6930471674194457); assertFuzzyEquals(Magnets.doubles(20, 10000), 0.6930471955575918); + assertFuzzyEquals(Magnets.doubles(30, 10000), 0.6930471955576123); } }