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..bd7e835 --- /dev/null +++ b/src/main/java/org/fundacionjala/coding/marcos/Magnets.java @@ -0,0 +1,48 @@ +package org.fundacionjala.coding.marcos; + +import java.util.stream.IntStream; + +/** + * 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. + */ + 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(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 new file mode 100644 index 0000000..40f99d0 --- /dev/null +++ b/src/test/java/org/fundacionjala/coding/marcos/MagnetsTest.java @@ -0,0 +1,62 @@ +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 = Magnets.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); + assertFuzzyEquals(Magnets.doubles(30, 10000), 0.6930471955576123); + } +}