diff --git a/src/main/java/org/fundacionjala/coding/abel/BouncingBall.java b/src/main/java/org/fundacionjala/coding/abel/BouncingBall.java new file mode 100644 index 0000000..d0725ca --- /dev/null +++ b/src/main/java/org/fundacionjala/coding/abel/BouncingBall.java @@ -0,0 +1,48 @@ +package org.fundacionjala.coding.abel; + +/** + * A child plays with a ball on the nth floor of a big building. The height of this floor is known: + * (float parameter "h" in meters, h > 0) . + * He lets out the ball. The ball rebounds for example to two-thirds: + * (float parameter "bounce", 0 < bounce < 1) + * of its height. + * His mother looks out of a window that is 1.5 meters from the ground: + * (float parameters window < h). + * How many times will the mother see the ball either falling or bouncing in front of the window + * (return a positive integer unless conditions are not fulfilled in which case return -1) ? + * Note + * You will admit that the ball can only be seen if the height of the rebouncing ball is stricty greater + * than the window parameter. + * Example: + * h = 3, bounce = 0.66, window = 1.5, result is 3 + * h = 3, bounce = 1, window = 1.5, result is -1 + */ +public final class BouncingBall { + + /** + * Private Constructor. + */ + private BouncingBall() { + + } + + /** + * Calculates how many times the ball is seen throw the window. + * + * @param h Initial Height of the ball. + * @param bounce Bounce rate. + * @param window Windows's Height. + * @return How many times the ball is seen. + */ + public static int bouncingBall(double h, double bounce, double window) { + if (!(h > 0 && 0 < bounce && bounce < 1 && window < h)) { + return -1; + } + int count = 1; + while (h * bounce >= window) { + count += 2; + h = h * bounce; + } + return count; + } +} diff --git a/src/test/java/org/fundacionjala/coding/abel/BouncingBallTest.java b/src/test/java/org/fundacionjala/coding/abel/BouncingBallTest.java new file mode 100644 index 0000000..6cc1e9e --- /dev/null +++ b/src/test/java/org/fundacionjala/coding/abel/BouncingBallTest.java @@ -0,0 +1,58 @@ +package org.fundacionjala.coding.abel; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Modifier; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * Test class for Bouncing Ball. + */ +public class BouncingBallTest { + + /** + * Test for Private Constructors. + * + * @throws NoSuchMethodException Thrown when a particular method cannot be found. + * @throws IllegalAccessException Thrown when an application tries to reflectively create an instance. + * @throws InvocationTargetException thrown by an invoked method or constructor. + * @throws InstantiationException Thrown when an application tries to create an instance of a class + * using the {@code newInstance} method in class. + */ + @Test + public void privateConstructorTest() throws NoSuchMethodException, IllegalAccessException, + InvocationTargetException, InstantiationException { + Constructor constructor = BouncingBall.class.getDeclaredConstructor(); + assertTrue(Modifier.isPrivate(constructor.getModifiers())); + constructor.setAccessible(true); + constructor.newInstance(); + } + + /** + * First Test. + */ + @Test + public void test1() { + assertEquals(3, BouncingBall.bouncingBall(3.0, 0.66, 1.5)); + } + + /** + * Second Test. + */ + @Test + public void test2() { + assertEquals(15, BouncingBall.bouncingBall(30.0, 0.66, 1.5)); + } + + /** + * Third Test. + */ + @Test + public void test3() { + assertEquals(-1, BouncingBall.bouncingBall(1.0, 0.66, 2.00)); + } +}