Saturday, October 11, 2014

noTeenSum

Ques:
Given 3 int values, a b c, return their sum. However, if any of the values is a teen -- in the range 13..19 inclusive -- then that value counts as 0, except 15 and 16 do not count as a teens. Write a separate helper "public int fixTeen(int n) {"that takes in an int value and returns that value fixed for the teen rule. In this way, you avoid repeating the teen code 3 times (i.e. "decomposition"). Define the helper below and at the same indent level as the main noTeenSum()

Ans:public int noTeenSum(int a, int b, int c) {
//  public static int noTeenSum(int a, int b, int c){
      
       return fixteen(a)+fixteen(b)+fixteen(c);
   }
    public int fixteen(int age){
        int helper=0;
        if(age>=13&&age<=19){
            if(age==15||age==16){
                helper=age;
       //         continue a;
            }else{helper=0;}
        }else helper=age;
          return helper;
    }

No comments:

Post a Comment