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.enum
11 public class Arithmetica {
12 private final Version version;
13 public enum Version { VERSION_1_0, VERSION_2_0 }
15 public Arithmetica() {
16 this(Version.VERSION_1_0);
18 public Arithmetica(Version version) {
19 this.version = version;
22 public int sumRange(int from, int to) {
25 return sumRange1(from, to);
27 return sumRange2(from, to);
29 throw new IllegalStateException();
32 // FINISH: design.composition.arith2.0.enum
34 public int sumTwo(int one, int second) {
38 public int sumAll(int... numbers) {
40 for (int i = 1; i < numbers.length; i++) {
41 sum = sumTwo(sum, numbers[i]);
47 private int sumRange1(int from, int to) {
49 int[] array = new int[len + 1];
50 for (int i = 0; i <= len; i++) {
56 private int sumRange2(int from, int to) {
57 return (from + to) * (to - from + 1) / 2;