author | Jaroslav Tulach <jaroslav.tulach@apidesign.org> |
Fri, 25 Mar 2016 07:34:02 +0100 | |
changeset 411 | 9eb6379b97f0 |
parent 210 | acf2c31e22d4 |
permissions | -rw-r--r-- |
jtulach@321 | 1 |
package org.apidesign.math; |
jtulach@17 | 2 |
|
jtulach@20 | 3 |
/** Class to simplify arithmetical operations, improved version to compute |
jtulach@20 | 4 |
* the sum for ranges. |
jtulach@17 | 5 |
* |
jtulach@17 | 6 |
* @author Jaroslav Tulach <jtulach@netbeans.org> |
jtulach@20 | 7 |
* @version 2.0 |
jtulach@17 | 8 |
*/ |
jtulach@17 | 9 |
public class Arithmetica { |
jtulach@17 | 10 |
public int sumTwo(int one, int second) { |
jtulach@17 | 11 |
return one + second; |
jtulach@17 | 12 |
} |
jtulach@17 | 13 |
|
jtulach@17 | 14 |
public int sumAll(int... numbers) { |
jtulach@187 | 15 |
if (numbers.length == 0) { |
jtulach@187 | 16 |
return 0; |
jtulach@187 | 17 |
} |
jtulach@17 | 18 |
int sum = numbers[0]; |
jtulach@17 | 19 |
for (int i = 1; i < numbers.length; i++) { |
jtulach@17 | 20 |
sum = sumTwo(sum, numbers[i]); |
jtulach@17 | 21 |
} |
jtulach@17 | 22 |
return sum; |
jtulach@17 | 23 |
} |
jtulach@17 | 24 |
|
jtulach@23 | 25 |
// BEGIN: design.composition.arith2.0 |
jtulach@17 | 26 |
public int sumRange(int from, int to) { |
jtulach@210 | 27 |
return (from + to) * (Math.abs(to - from) + 1) / 2; |
jtulach@17 | 28 |
} |
jtulach@23 | 29 |
// END: design.composition.arith2.0 |
jtulach@17 | 30 |
} |