sortaSum challenge java

Hi,

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 tests
LVL 7
Who is Participating?
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.

Commented:
``````public int sortaSum(int a, int b) {
int sum = a+ b;

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

return sum;
}
``````
0

Experts Exchange Solution brought to you by

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

Commented:
public int sortaSum(int a, int b) {
return 10<=(a+=b) && a<= 19?20:a;
}
0
Commented:
For one thing, I would improve the readability of it by making consistent spacing and use of braces.

For 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
0
Senior Developer/ArchitectCommented:
Looking across a number of your questions, we've seen the same basic problem in various forms: determine if some value (in this case a sum of two ints) is in a given range and if so return a boolean or a different value. What I would propose is something like a Range class that provides this behavior and can be used to eliminate the duplication:
``````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;
}

}
``````
``````    private static Range range = new Range(10, 19);

public static int sortaSum(int a, int b) {
return range.normalize(a + b, 20);
}
``````
1
Commented:
@Jim Cakalic excellent solution.
However the author is limited to using the codingbat website which probably won't let you create new class structures etc.
0
Senior Developer/ArchitectCommented:
Too true.
0
Author Commented:
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;
}
``````
0
Author Commented:
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
0
Author Commented:
in the else.
If you choose not to do that, you could also propagate the ! into the expression to get c<10 || c>20

0
Commented:
You can change this:

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"
0
Author Commented:
public int sortaSum(int a, int b) {
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
0
Commented:
That's correct. If you return inside a THEN it never goes past the END IF so the ELSE is not needed
0
Author Commented:
what is THEN and  END IF ?
Are they java keywords?
0
Author Commented:
i see even though code flow do not go to IF then it goes to else and there is a return in there.

That is reason second one is correct
0
Commented:
> what is THEN and  END IF ?
Are they java keywords?

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.
0
Author Commented:
``````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
0
Commented:
That is correct
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
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.