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/abel/BouncingBall.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
58 changes: 58 additions & 0 deletions src/test/java/org/fundacionjala/coding/abel/BouncingBallTest.java
Original file line number Diff line number Diff line change
@@ -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<BouncingBall> 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));
}
}