samples/composition/src-api2.0/org/apidesign/math/Arithmetica.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 30 Oct 2014 21:30:10 +0100
changeset 409 40cabcdcd2be
parent 210 acf2c31e22d4
permissions -rw-r--r--
Updating to NBMs from NetBeans 8.0.1 as some of them are required to run on JDK8
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
}