Wednesday, February 21, 2024
HomeSoftware EngineeringThe way to Sustain the Hoop in Java

The way to Sustain the Hoop in Java


The problem

Alex simply received a brand new hula hoop, he loves it however feels discouraged as a result of his little brother is best than him

Write a program the place Alex can enter (n) what number of instances the ring goes spherical and it’ll return him an encouraging message 🙂

  • If Alex will get 10 or extra hoops, return the string “Nice, now transfer on to tips”.
  • If he doesn’t get 10 hoops, return the string “Maintain at it till you get it”.

The answer in Java code

Choice 1:

public class HelpAlex{
  public static String hoopCount(int n){
     return n >= 10 ? "Nice, now transfer on to tips" : "Maintain at it till you get it";
  }
}

Choice 2:

public class HelpAlex{
  public static String hoopCount(int n){
    
    if ( n < 0 ) {
      return "Error";
    } else if (n >= 0 && n < 10) {
      return  "Maintain at it till you get it";
    } else {
      return "Nice, now transfer on to tips";
    }
  
  }
}

Choice 3:

public class HelpAlex{
  non-public static closing String GREAT = "Nice, now transfer on to tips";
  non-public static closing String KEEP = "Maintain at it till you get it";
  
  public static String hoopCount(int n){
   return n >= 10 ? GREAT : KEEP;
  }
}

Take a look at instances to validate our answer

import org.junit.Take a look at;
import static org.junit.Assert.assertEquals;

public class HoopCountTest {
    @Take a look at
    public void testHoopCount(){
        assertEquals("Nice, now transfer on to tips", HelpAlex.hoopCount(11));
        assertEquals("Maintain at it till you get it", HelpAlex.hoopCount(7));        
    }
}
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments