samples/composition/src-api2.0/org/apidesign/math/Arithmetica.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Feb 2009 17:30:06 +0100
changeset 321 06bf3a32eaa0
parent 210 samples/composition/src-api2.0/api/Arithmetica.java@acf2c31e22d4
permissions -rw-r--r--
Moving code to org.apidesign.math package
     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 }