3 /** Class to simplify arithmetical operations, improved version to compute
4 * the sum for ranges, but only if one uses the new constructor to indicate
5 * need for new version.
7 * @author Jaroslav Tulach <jtulach@netbeans.org>
10 // BEGIN: design.composition.arith2.0.compat
11 public class Arithmetica {
12 private final int version;
14 public Arithmetica() {
17 public Arithmetica(int version) {
18 this.version = version;
21 public int sumTwo(int one, int second) {
25 public int sumAll(int... numbers) {
27 for (int i = 1; i < numbers.length; i++) {
28 sum = sumTwo(sum, numbers[i]);
33 public int sumRange(int from, int to) {
35 case 1: return sumRange1(from, to);
36 case 2: return sumRange2(from, to);
37 default: throw new IllegalStateException();
41 private int sumRange1(int from, int to) {
43 int[] array = new int[len + 1];
44 for (int i = 0; i <= len; i++) {
50 private int sumRange2(int from, int to) {
51 return (from + to) * (to - from + 1) / 2;
53 // END: design.composition.arith2.0.compat