215k views
3 votes
/* 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.
*/
public int sortaSum(int a, int b) {
int sum = a + b;

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

return sum;
}

1 Answer

4 votes

Final answer:

The question involves creating a Java function that adds two integers and returns the sum unless it falls within the range of 10 to 19, in which case it returns 20 instead. This showcases conditional logic in programming, where certain outputs are modified based on predefined rules.

Step-by-step explanation:

The question pertains to writing a function in Java programming language that takes two integer parameters and returns their sum. However, the function includes a logical condition where if the sum of the two integers (a and b) falls within the range 10 to 19 inclusive, the function instead returns 20. This can be considered a conditional logic problem in programming.

The given Java function sortaSum operates by first calculating the sum of the two arguments passed to it. It then checks if the sum falls within the forbidden range using an 'if' statement. If the sum is between 10 and 19 inclusive, the function returns 20. Otherwise, it returns the actual sum calculated initially.

This type of function could be useful in situations where certain results need to be flagged or replaced because they are undesirable or should trigger a special case, which in this example, is replacing sums within the specified range with 20. It's a simple example of using conditional statements within a function.

User CharlesC
by
8.4k points