Fixed onjava/Range.java, added TestRange.java

This commit is contained in:
Bruce Eckel 2021-01-31 17:50:14 -07:00
parent cf109e7860
commit 68e98a3075
5 changed files with 48 additions and 23 deletions

View File

@ -5,7 +5,7 @@
4: main 4: main
5: main 5: main
6: main 6: main
6: ForkJoinPool.commonPool-worker-9
7: main 7: main
8: main 8: ForkJoinPool.commonPool-worker-9
9: main 9: main
10: ForkJoinPool.commonPool-worker-5

View File

@ -32,7 +32,7 @@ public class Overloading {
t.info(); t.info();
t.info("overloaded method"); t.info("overloaded method");
} }
// Overloaded constructor: // Calls overloaded constructor:
new Tree(); new Tree();
} }
} }

View File

@ -12,12 +12,12 @@ class C2 implements I1, I2 {
@Override @Override
public void f() {} public void f() {}
@Override @Override
public int f(int i) { return 1; } // overloaded public int f(int i) { return 1; } // Overloaded
} }
class C3 extends C implements I2 { class C3 extends C implements I2 {
@Override @Override
public int f(int i) { return 1; } // overloaded public int f(int i) { return 1; } // Overloaded
} }
class C4 extends C implements I3 { class C4 extends C implements I3 {

View File

@ -2,33 +2,29 @@
// (c)2021 MindView LLC: see Copyright.txt // (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// Array creation methods that can be used without // Create arrays initialized with integer values.
// qualifiers, using static imports:
package onjava; package onjava;
public class Range { 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 // Produce sequence [start..end) incrementing by step
public static public static
int[] range(int start, int end, int step) { 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]; int[] result = new int[sz];
for(int i = 0; i < sz; i++) for(int i = 0; i < sz; i++)
result[i] = start + (i * step); result[i] = start + (i * step);
return result; 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);
} }
} }

29
onjava/TestRange.java Normal file
View File

@ -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]
*/