Java > Logic-2 >noTeenSum (CodingBat Solution)

Problem:

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().


Solution:

public int noTeenSum(int a, int b, int c) {
int RETURN = 0;
  if ( a == 13 || a == 14 || a == 17 || a == 18 || a == 19)
    a= 0;
      if ( b == 13 || b == 14 || b == 17 || b == 18 || b == 19)
    b= 0;
      if ( c == 13 || c == 14 || c == 17 || c == 18 || c == 19)
    c= 0;
    
    return a + b + c;
  
}


No comments :

Post a Comment

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact