samples/composition/src-api2.0/org/apidesign/math/Arithmetica.java
changeset 411 9eb6379b97f0
parent 210 acf2c31e22d4
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/composition/src-api2.0/org/apidesign/math/Arithmetica.java	Fri Mar 25 07:34:02 2016 +0100
     1.3 @@ -0,0 +1,30 @@
     1.4 +package org.apidesign.math;
     1.5 +
     1.6 +/** Class to simplify arithmetical operations, improved version to compute
     1.7 + * the sum for ranges.
     1.8 + *
     1.9 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.10 + * @version 2.0
    1.11 + */
    1.12 +public class Arithmetica {
    1.13 +    public int sumTwo(int one, int second) {
    1.14 +        return one + second;
    1.15 +    }
    1.16 +    
    1.17 +    public int sumAll(int... numbers) {
    1.18 +        if (numbers.length == 0) {
    1.19 +            return 0;
    1.20 +        }
    1.21 +        int sum = numbers[0];
    1.22 +        for (int i = 1; i < numbers.length; i++) {
    1.23 +            sum = sumTwo(sum, numbers[i]);
    1.24 +        }
    1.25 +        return sum;
    1.26 +    }
    1.27 +    
    1.28 +// BEGIN: design.composition.arith2.0
    1.29 +    public int sumRange(int from, int to) {
    1.30 +        return (from + to) * (Math.abs(to - from) + 1) / 2;
    1.31 +    }
    1.32 +// END: design.composition.arith2.0
    1.33 +}