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

noTeenSum(1, 2, 3) → 6
noTeenSum(2, 13, 1) → 3
noTeenSum(2, 1, 14) → 3


Solution:

public int noTeenSum(int a, int b, int c) {
  return fixTeen(a) + fixTeen(b) + fixTeen(c);
}
public int fixTeen(int n) {
    if ( (n >=13 && n < 15) || (n > 16 && n <= 19) )
      return 0;
    else 
      return n;
  }


10 comments :

  1. public int noTeenSum(int a, int b, int c) {
    return fixTeen(a)+ fixTeen(b)+fixTeen(c);
    }
    public int fixTeen(int n){
    if(n>=13 && n<=19){
    if(n==15||n==16){
    return n;
    }
    return 0;
    }else{
    return n;
    }
    }

    ReplyDelete
  2. public int noTeenSum(int a, int b, int c) {


    return fixTeen(a)+fixTeen(b)+fixTeen(c);


    }
    public int fixTeen(int n){

    if(n>=13&&n<=19&&n!=15&&n!=16){

    n=0;


    }
    else{
    n=n;
    }

    return n;
    }







    ReplyDelete
  3. public int noTeenSum(int a, int b, int c) {
    return fixTeen(a) + fixTeen(b) + fixTeen(c);
    }
    // helper function
    public int fixTeen(int n) {
    if (n >= 13 && n <= 19 && n != 15 && n !=16) {
    n = 0;}
    return n;
    }

    ReplyDelete
  4. public int noTeenSum(int a, int b, int c) {
    return fixTeen(a) + fixTeen(b) + fixTeen(c);
    }

    // helper function
    public int fixTeen(int n) {
    if (n >= 13 && n <= 19 && n != 15 && n !=16) {
    n = 0;

    }
    else return n;
    return n;
    }

    ReplyDelete
  5. public int noTeenSum(int a, int b, int c) {

    return fixTeen(a) + fixTeen(b) + fixTeen(c);
    }
    public int fixTeen(int n) {

    return (n <= 19 && n >= 13 && n != 15 && n != 16) ? 0 : n;
    }

    ReplyDelete
  6. public int noTeenSum(int a, int b, int c) {
    int temp = a;
    boolean a1 = false;
    boolean b1 = false;
    for (int i = 0; i < 3; i++) {
    if (temp > 12 && temp < 20 && temp != 15 && temp != 16) {
    temp = 0;
    }
    if (!a1) {
    a = temp;
    a1 = true;
    temp = b;
    } else if (!b1) {
    b = temp;
    temp = c;
    b1 = true;
    } else {
    c = temp;

    }
    }


    return a + b + c;
    }

    ReplyDelete
  7. public int noTeenSum(int a, int b, int c) {

    int sum = 0;
    int[] nums = {a,b,c};


    for (int x = 0; x < nums.length; x++){
    if (nums[x] >= 11 && nums[x] <= 19){
    nums[x] = fixTeen(nums[x]);
    }
    sum += nums[x];
    }

    return sum;
    }
    public int fixTeen(int n){
    if (n == 15 || n == 16){
    return n;
    }
    else
    return 0;
    }

    ReplyDelete
  8. public int noTeenSum(int... nums) {
    int sum=0;
    for(int n:nums){
    if(!(n >= 13 && n <= 19 && n != 15 && n !=16))
    sum+=n;
    }
    return sum;
    }

    ReplyDelete
  9. function fixTeen(val) {
    if (val == 15) {
    return val;
    } else if (val == 16) {
    return val;
    } else if (val >= 13 && val <= 19) {
    return 0;
    } else {
    return val;
    }
    }
    function noTeenSum(a, b, c) {
    let sum = 0;
    let arr = [];
    arr.push(a);
    arr.push(b);
    arr.push(c);
    console.log(arr);

    for (let i = 0; i < arr.length; i++) {
    let value = arr[i];
    let res = fixTeen(value);
    sum = sum + res;
    }
    console.log(sum);
    }

    let a = 1;
    let b = 15;
    let c = 2;
    noTeenSum(a, b, c);

    ReplyDelete
  10. public int noTeenSum(int a, int b, int c) {
    return fixTeen(a) + fixTeen(b) + fixTeen(c);
    }

    public int fixTeen(int n) {
    return (n < 13 || n>19 || n == 15 || n == 16) ? n:0;
    }

    ya feel meh

    ReplyDelete

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