Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Hi,

I am working on below challenge

http://codingbat.com/prob/p183071

i wrote as below and passed all tests

How to improve my code. Please advise

I am working on below challenge

http://codingbat.com/prob/p183071

i wrote as below and passed all tests

```
public int sortaSum(int a, int b) {
int c;
c=a+b;
if(!(c>=10&&c<=19)){
return c=a+b;
}
else
return 20;
}
```

```
public class sss {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(sortaSum(1,14));
}
/*Given 2 ints, a and b, return their sum.
* However, sums in the range 10..19 inclusive,
* are forbidden, so in that case just return 20.
sortaSum(3, 4) → 7
sortaSum(9, 4) → 20
sortaSum(10, 11) → 2*/
public static int sortaSum(int a, int b) {
int c;
c=a+b;
if(!(c>=10&&c<=19)){
return c=a+b;
}
else
return 20;
}
}
```

I passed all testsHow to improve my code. Please advise

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with premium.
Start your 7-day free trial.

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trialFor example:

int c; should be indented for readability.

your two returns are at the same level so their leading spaces should be that same (looks like one is preceded by 2 spaces and the other with a tab so they may look the same in some editors but not others)

People often add spacing around the operators such as c = a + b and c >= 10 && ...

For consistency, you should either take away the braces at lines 5 and 7, or add them before and after line 9. Also, traditionally, if you use braces they appear on the same line as the else: } else {. however, that is personal preference. Just be consistent.

Finally, the ! operator is a negative. Traditionally, people think about the positive first then the negative. you can remove the ! and swap your two returns so the return of 20 comes in the true condition and the calculation is in the else.

If you choose not to do that, you could also propagate the ! into the expression to get c<10 || c>20

finally, unless a,b and c have specific meanings, giving them better names to describe their use would be beneficial.

I hope this helps get an A

```
public class Range {
private int min;
private int max;
public Range(int min, int max) {
this.min = min;
this.max = max;
}
/**
* Determine if the value is within the represented range inclusive of the bounds.
*
* @param val the value to test
* @return true if val is >= min and <= max otherwise false.
*/
public boolean within(int val) {
return val >= min && val <= max;
}
/**
* Normalize the value if within the represented range (see within).
*
* @param val the value to test
* @param norm the norm value
* @return norm if within(val) otherwise val
*/
public int normalize(int val, int norm) {
return within(val) ? norm : val;
}
}
```

Using this, your implementation becomes:
```
private static Range range = new Range(10, 19);
public static int sortaSum(int a, int b) {
return range.normalize(a + b, 20);
}
```

However the author is limited to using the codingbat website which probably won't let you create new class structures etc.

public int sortaSum(int a, int b) {

int sum = a+ b;

if(sum >= 10 && sum <= 19){

return 20;

}

return sum;

}

how above different from below

```
public int sortaSum(int a, int b) {
int sum = a+ b;
if(sum >= 10 && sum <= 19){
return 20;
}
else
return sum;
}
```

Finally, the ! operator is a negative. Traditionally, people think about the positive first then the negative. you can remove the ! and swap your two returns so the return of 20 comes in the true condition and the calculation is in the else.

```
public int sortaSum(int a, int b) {
int c;
c=a+b;
if((c>=10&&c<=19)){
return 20;
}
else
return c=a+b;
}
```

above worked fine too

in the else.

If you choose not to do that, you could also propagate the ! into the expression to get c<10 || c>20

i was not clear on this. please advise

if(!(c>=10&&c<=19)){

to the equivalent without the negation:

if (c < 10 || c > 19) {

because "not between 10 and 19 (inclusive)" is the same as "below 10 or above 19"

int sum = a+ b;

if(sum >= 10 && sum <= 19){

return 20;

}

return sum;

}

there is no difference between above and below right where i have extra else

public int sortaSum(int a, int b) {

int sum = a+ b;

if(sum >= 10 && sum <= 19){

return 20;

}

else

return sum;

}

Both passed all tests

That is reason second one is correct

Are they java keywords?

please advise

I used these words taken from VB to represent the true clause and the very end of the IF group. It's much easier than saying " the { at the end of the IF and the } at the end of the IF group". Sorry for the confusion.

```
public int sortaSum(int a, int b) {
int sum = a+ b;
if(sum >= 10 && sum <= 19){
return 20;
}
return sum;
}
```

in the above case flow through comes to bottom most line ie return if it does not enter the if block which is clear.

Above almost same as below right

```
if(sum >= 10 && sum <= 19){
return 20;
}
else
return sum;
}
```

compilation error would have come if code is with else if as below

public int sortaSum(int a, int b) {

int sum = a+ b;

if(sum >= 10 && sum <= 19){

return 20;

}

else if(condition){

return 20;

}

//return sum;

}

since above there is a chance of exiting the method without return without entering if without entering else if

Java EE

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with premium.
Start your 7-day free trial.

Open in new window