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