Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 68e98a3

Browse files
committed
Fixed onjava/Range.java, added TestRange.java
1 parent cf109e7 commit 68e98a3

File tree

5 files changed

+48
-23
lines changed

5 files changed

+48
-23
lines changed

concurrent/PSP2.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
4: main
66
5: main
77
6: main
8+
6: ForkJoinPool.commonPool-worker-9
89
7: main
9-
8: main
10+
8: ForkJoinPool.commonPool-worker-9
1011
9: main
11-
10: ForkJoinPool.commonPool-worker-5

housekeeping/Overloading.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static void main(String[] args) {
3232
t.info();
3333
t.info("overloaded method");
3434
}
35-
// Overloaded constructor:
35+
// Calls overloaded constructor:
3636
new Tree();
3737
}
3838
}

interfaces/InterfaceCollision.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ class C2 implements I1, I2 {
1212
@Override
1313
public void f() {}
1414
@Override
15-
public int f(int i) { return 1; } // overloaded
15+
public int f(int i) { return 1; } // Overloaded
1616
}
1717

1818
class C3 extends C implements I2 {
1919
@Override
20-
public int f(int i) { return 1; } // overloaded
20+
public int f(int i) { return 1; } // Overloaded
2121
}
2222

2323
class C4 extends C implements I3 {

onjava/Range.java

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,29 @@
22
// (c)2021 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://OnJava8.com for more book information.
5-
// Array creation methods that can be used without
6-
// qualifiers, using static imports:
5+
// Create arrays initialized with integer values.
76
package onjava;
87

98
public class Range {
10-
// Produce a sequence [0..n)
11-
public static int[] range(int n) {
12-
int[] result = new int[n];
13-
for(int i = 0; i < n; i++)
14-
result[i] = i;
15-
return result;
16-
}
17-
// Produce a sequence [start..end)
18-
public static int[] range(int start, int end) {
19-
int sz = end - start;
20-
int[] result = new int[sz];
21-
for(int i = 0; i < sz; i++)
22-
result[i] = start + i;
23-
return result;
24-
}
259
// Produce sequence [start..end) incrementing by step
2610
public static
2711
int[] range(int start, int end, int step) {
28-
int sz = (end - start)/step;
12+
if (step == 0)
13+
throw new
14+
IllegalArgumentException("Step cannot be zero");
15+
int sz = Math.max(0, step >= 0 ?
16+
(end + step - 1 - start) / step
17+
: (end + step + 1 - start) / step);
2918
int[] result = new int[sz];
3019
for(int i = 0; i < sz; i++)
3120
result[i] = start + (i * step);
3221
return result;
22+
} // Produce a sequence [start..end)
23+
public static int[] range(int start, int end) {
24+
return range(start, end, 1);
25+
}
26+
// Produce a sequence [0..n)
27+
public static int[] range(int n) {
28+
return range(0, n);
3329
}
3430
}

onjava/TestRange.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// onjava/TestRange.java
2+
// (c)2021 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://OnJava8.com for more book information.
5+
// Basic test of Range.java
6+
import static onjava.Range.*;
7+
import java.util.Arrays;
8+
9+
public class TestRange {
10+
private static void show(int[] rng) {
11+
System.out.println(Arrays.toString(rng));
12+
}
13+
public static void main(String[] args) {
14+
show(range(10, 21, 3));
15+
show(range(21, 10, -3));
16+
show(range(-5, 5, -3));
17+
show(range(-5, 5, 3));
18+
show(range(10, 21));
19+
show(range(10));
20+
}
21+
}
22+
/* Output:
23+
[10, 13, 16, 19]
24+
[21, 18, 15, 12]
25+
[]
26+
[-5, -2, 1, 4]
27+
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
28+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
29+
*/

0 commit comments

Comments
 (0)