-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAdaptedRandomDoubles.java
More file actions
39 lines (30 loc) · 964 Bytes
/
AdaptedRandomDoubles.java
File metadata and controls
39 lines (30 loc) · 964 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package interfaces;
import java.nio.*;
import java.util.*;
/**
* RUN:
* javac interfaces/AdaptedRandomDoubles.java && java interfaces.AdaptedRandomDoubles
* OUTPUT:
* 0.7271157860730044 0.5309454508634242 0.16020656493302599 0.18847866977771732
* 0.5166020801268457 0.2678662084200585 0.2613610344283964
*/
public class AdaptedRandomDoubles extends RandomDoubles implements Readable {
private int count;
public AdaptedRandomDoubles(int count) {
this.count = count;
}
public int read(CharBuffer cb) {
if (count-- == 0) {
return -1;
}
String result = Double.toString(next()) + " ";
cb.append(result);
return result.length();
}
public static void main(String[] args) {
Scanner s = new Scanner(new AdaptedRandomDoubles(7));
while (s.hasNextDouble()) {
System.out.print(s.nextDouble() + " ");
}
}
}