The challenge
Alex just got a new hula hoop, he loves it but feels discouraged because his little brother is better than him
Write a program where Alex can input (n) how many times the hoop goes round and it will return him an encouraging message 🙂
- If Alex gets 10 or more hoops, return the string “Great, now move on to tricks”.
- If he doesn’t get 10 hoops, return the string “Keep at it until you get it”.
The solution in Java code
Option 1:
public class HelpAlex{
public static String hoopCount(int n){
return n >= 10 ? "Great, now move on to tricks" : "Keep at it until you get it";
}
}
Option 2:
public class HelpAlex{
public static String hoopCount(int n){
if ( n < 0 ) {
return "Error";
} else if (n >= 0 && n < 10) {
return "Keep at it until you get it";
} else {
return "Great, now move on to tricks";
}
}
}
Option 3:
public class HelpAlex{
private static final String GREAT = "Great, now move on to tricks";
private static final String KEEP = "Keep at it until you get it";
public static String hoopCount(int n){
return n >= 10 ? GREAT : KEEP;
}
}
Test cases to validate our solution
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class HoopCountTest {
@Test
public void testHoopCount(){
assertEquals("Great, now move on to tricks", HelpAlex.hoopCount(11));
assertEquals("Keep at it until you get it", HelpAlex.hoopCount(7));
}
}