samples/composition/src-api1.0/org/apidesign/math/Arithmetica.java
changeset 409 40cabcdcd2be
parent 210 acf2c31e22d4
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/composition/src-api1.0/org/apidesign/math/Arithmetica.java	Thu Oct 30 21:30:10 2014 +0100
     1.3 @@ -0,0 +1,38 @@
     1.4 +package org.apidesign.math;
     1.5 +
     1.6 +/** Class to simplify arithmetical operations.
     1.7 + *
     1.8 + * @author Jaroslav Tulach <jtulach@netbeans.org>
     1.9 + * @version 1.0
    1.10 + */
    1.11 +// BEGIN: design.composition.arith1.0
    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 +    public int sumRange(int from, int to) {
    1.29 +        int len = to - from;
    1.30 +        if (len < 0) {
    1.31 +            len = -len;
    1.32 +            from = to;
    1.33 +        }
    1.34 +        int[] array = new int[len + 1];
    1.35 +        for (int i = 0; i <= len; i++) {
    1.36 +            array[i] = from + i;
    1.37 +        }
    1.38 +        return sumAll(array);
    1.39 +    }
    1.40 +}
    1.41 +// END: design.composition.arith1.0