samples/composition/src-api2.0-property/api/Arithmetica.java
changeset 148 e762b177d4b0
child 149 eb52f31b18f4
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/composition/src-api2.0-property/api/Arithmetica.java	Sat Jun 14 09:57:56 2008 +0200
     1.3 @@ -0,0 +1,45 @@
     1.4 +package api;
     1.5 +
     1.6 +/** Class to simplify arithmetical operations, improved version to compute
     1.7 + * the sum for ranges, but only if the virtual machine is configured to
     1.8 + * run in incompatible mode.
     1.9 + *
    1.10 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.11 + * @version 2.0
    1.12 + */
    1.13 +// BEGIN: design.composition.arith2.0.property
    1.14 +public class Arithmetica {
    1.15 +    public int sumTwo(int one, int second) {
    1.16 +        return one + second;
    1.17 +    }
    1.18 +    
    1.19 +    public int sumAll(int... numbers) {
    1.20 +        int sum = numbers[0];
    1.21 +        for (int i = 1; i < numbers.length; i++) {
    1.22 +            sum = sumTwo(sum, numbers[i]);
    1.23 +        }
    1.24 +        return sum;
    1.25 +    }
    1.26 +    
    1.27 +    public int sumRange(int from, int to) {
    1.28 +        if (Boolean.getBoolean("arithmetica.v2")) {
    1.29 +            return sumRange2(from, to);
    1.30 +        } else {
    1.31 +            return sumRange1(from, to);
    1.32 +        }
    1.33 +    }
    1.34 +
    1.35 +    private int sumRange1(int from, int to) {
    1.36 +        int len = to - from;
    1.37 +        int[] array = new int[len + 1];
    1.38 +        for (int i = 0; i <= len; i++) {
    1.39 +            array[i] = from + i;
    1.40 +        }
    1.41 +        return sumAll(array);
    1.42 +    }
    1.43 +    
    1.44 +    private int sumRange2(int from, int to) {
    1.45 +        return (from + to) * (to - from + 1) / 2;
    1.46 +    }
    1.47 +// END: design.composition.arith2.0.property
    1.48 +}