diff --git a/concurrent/PSP2.txt b/concurrent/PSP2.txt index 4b372a61..22443388 100644 --- a/concurrent/PSP2.txt +++ b/concurrent/PSP2.txt @@ -5,7 +5,7 @@ 4: main 5: main 6: main +6: ForkJoinPool.commonPool-worker-9 7: main -8: main +8: ForkJoinPool.commonPool-worker-9 9: main -10: ForkJoinPool.commonPool-worker-5 diff --git a/housekeeping/Overloading.java b/housekeeping/Overloading.java index 65f23952..55307f4f 100644 --- a/housekeeping/Overloading.java +++ b/housekeeping/Overloading.java @@ -32,7 +32,7 @@ public class Overloading { t.info(); t.info("overloaded method"); } - // Overloaded constructor: + // Calls overloaded constructor: new Tree(); } } diff --git a/interfaces/InterfaceCollision.java b/interfaces/InterfaceCollision.java index a3a6612b..e00fb8a8 100644 --- a/interfaces/InterfaceCollision.java +++ b/interfaces/InterfaceCollision.java @@ -12,12 +12,12 @@ class C2 implements I1, I2 { @Override public void f() {} @Override - public int f(int i) { return 1; } // overloaded + public int f(int i) { return 1; } // Overloaded } class C3 extends C implements I2 { @Override - public int f(int i) { return 1; } // overloaded + public int f(int i) { return 1; } // Overloaded } class C4 extends C implements I3 { diff --git a/onjava/Range.java b/onjava/Range.java index ee43244f..82041db5 100644 --- a/onjava/Range.java +++ b/onjava/Range.java @@ -2,33 +2,29 @@ // (c)2021 MindView LLC: see Copyright.txt // We make no guarantees that this code is fit for any purpose. // Visit http://OnJava8.com for more book information. -// Array creation methods that can be used without -// qualifiers, using static imports: +// Create arrays initialized with integer values. package onjava; public class Range { - // Produce a sequence [0..n) - public static int[] range(int n) { - int[] result = new int[n]; - for(int i = 0; i < n; i++) - result[i] = i; - return result; - } - // Produce a sequence [start..end) - public static int[] range(int start, int end) { - int sz = end - start; - int[] result = new int[sz]; - for(int i = 0; i < sz; i++) - result[i] = start + i; - return result; - } // Produce sequence [start..end) incrementing by step public static int[] range(int start, int end, int step) { - int sz = (end - start)/step; + if (step == 0) + throw new + IllegalArgumentException("Step cannot be zero"); + int sz = Math.max(0, step >= 0 ? + (end + step - 1 - start) / step + : (end + step + 1 - start) / step); int[] result = new int[sz]; for(int i = 0; i < sz; i++) result[i] = start + (i * step); return result; + } // Produce a sequence [start..end) + public static int[] range(int start, int end) { + return range(start, end, 1); + } + // Produce a sequence [0..n) + public static int[] range(int n) { + return range(0, n); } } diff --git a/onjava/TestRange.java b/onjava/TestRange.java new file mode 100644 index 00000000..cd132d79 --- /dev/null +++ b/onjava/TestRange.java @@ -0,0 +1,29 @@ +// onjava/TestRange.java +// (c)2021 MindView LLC: see Copyright.txt +// We make no guarantees that this code is fit for any purpose. +// Visit http://OnJava8.com for more book information. +// Basic test of Range.java +import static onjava.Range.*; +import java.util.Arrays; + +public class TestRange { + private static void show(int[] rng) { + System.out.println(Arrays.toString(rng)); + } + public static void main(String[] args) { + show(range(10, 21, 3)); + show(range(21, 10, -3)); + show(range(-5, 5, -3)); + show(range(-5, 5, 3)); + show(range(10, 21)); + show(range(10)); + } +} +/* Output: +[10, 13, 16, 19] +[21, 18, 15, 12] +[] +[-5, -2, 1, 4] +[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] +[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] +*/