Skip to content

How to Keep up the Hoop in Java

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"; } }
Code language: Java (java)

Option 2:

public class HelpAlex{ public static String hoopCount(int n){ if ( n < ) { return "Error"; } else if (n >= && n < 10) { return "Keep at it until you get it"; } else { return "Great, now move on to tricks"; } } }
Code language: Java (java)

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; } }
Code language: Java (java)

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)); } }
Code language: Java (java)

See also  Make a dynamic year dropdown using PHP
Tags:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x