samples/composition/src-api1.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.
     4  *
     5  * @author Jaroslav Tulach <jtulach@netbeans.org>
     6  * @version 1.0
     7  */
     8 // BEGIN: design.composition.arith1.0
     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     public int sumRange(int from, int to) {
    26         int len = to - from;
    27         if (len < 0) {
    28             len = -len;
    29             from = to;
    30         }
    31         int[] array = new int[len + 1];
    32         for (int i = 0; i <= len; i++) {
    33             array[i] = from + i;
    34         }
    35         return sumAll(array);
    36     }
    37 }
    38 // END: design.composition.arith1.0