Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

teenSum java challenge

Hi,
I am trying below challenge

http://codingbat.com/prob/p178728

I tried as  below
public int teenSum(int a, int b) {
int sum =a+b;

if(a==13||a==13||a==14||a==15||a==16||a==17||a==18||a==19||b==13||b==14||b==15||b==16||b==17||b==18||b==19)

{
return 19;
}


return a+b;
  
}

Open in new window


I am getting below result


Expected      Run            
teenSum(3, 4) → 7      7      OK         
teenSum(10, 13) → 19      19      OK         
teenSum(13, 2) → 19      19      OK         
teenSum(3, 19) → 19      19      OK         
teenSum(13, 13) → 19      19      OK         
teenSum(10, 10) → 20      20      OK         
teenSum(6, 14) → 19      19      OK         
teenSum(15, 2) → 19      19      OK         
teenSum(19, 19) → 19      19      OK         
teenSum(19, 20) → 19      19      OK         
teenSum(2, 18) → 19      19      OK         
teenSum(12, 4) → 16      16      OK         
teenSum(2, 20) → 22      22      OK         
teenSum(2, 17) → 19      19      OK         
teenSum(2, 16) → 19      19      OK         
teenSum(6, 7) → 13      13      OK         
other tests
OK         
            
          



how to  improve my approach and design of this challenge. How do i make a graphical venn or some other relevant diagram to design it before writing single line of code to decide best strategy?
 Please advise
SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

How to use mothod approach
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

13<=a&&a<=19

above solution how the values like 14, 15, 16, 17, 18 checked?
Avatar of gudii9

ASKER

i got it. you are checking if in between 13, 19 both includinded for both a and b which is more refined approach than mine
13<=14 is true and 14<=19 is true
Slight modification to gurpsbassi's solution using a method -
public int teenSum(int a, int b) {
  return isLuckyRange(a, b) ? 19 : a + b;
}

private boolean isLuckyRange(int a, int b){
  return (a >= 13 && a <=19) || (b >= 13 && b <= 19);
}