Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/main/java/org/fundacionjala/coding/marcos/Magnets.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.fundacionjala.coding.marcos;

import java.util.stream.IntStream;

/**
* Created by Marcos.
* <p>
* 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();
}
}
62 changes: 62 additions & 0 deletions src/test/java/org/fundacionjala/coding/marcos/MagnetsTest.java
Original file line number Diff line number Diff line change
@@ -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<Magnets> 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);
}
}