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;

}

Open in new window


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;

}
	
}

Open in new window

I passed all tests
How to improve my code. Please advise
LVL 7
gudii9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

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

Open in new window

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 trial
ozoCommented:
public int sortaSum(int a, int b) {
  return 10<=(a+=b) && a<= 19?20:a;
}
rspahitzCommented:
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
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Jim CakalicSenior 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;
    }
    
}

Open in new window

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);
    }

Open in new window

gurpsbassiCommented:
@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.
Jim CakalicSenior Developer/ArchitectCommented:
Too true.
gudii9Author 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;
}

Open in new window

gudii9Author 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;
}

Open in new window


above worked fine too
gudii9Author 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

i was not clear on this. please advise
rspahitzCommented:
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"
gudii9Author 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
rspahitzCommented:
That's correct. If you return inside a THEN it never goes past the END IF so the ELSE is not needed
gudii9Author Commented:
what is THEN and  END IF ?
Are they java keywords?
please advise
gudii9Author 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
rspahitzCommented:
> what is THEN and  END IF ?
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.
gudii9Author Commented:
public int sortaSum(int a, int b) {
  int sum = a+ b;
  
  if(sum >= 10 && sum <= 19){
    return 20;
  }
  
  return sum;
}

Open in new window


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;
}

Open in new window



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
rspahitzCommented:
That is correct
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.