-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGenerators.java
More file actions
39 lines (32 loc) · 954 Bytes
/
Generators.java
File metadata and controls
39 lines (32 loc) · 954 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 generics;
import java.util.*;
import net.mindview.util.*;
import generics.coffee.*;
/**
* RUN:
* javac generics/Generators.java && java generics.Generators
* OUTPUT:
* Cappuchino 0
* Latte 1
* Cappuchino 2
* Mocha 3
* 1 1 2 3 5 8 13 21 34 55 89 144
*/
public class Generators {
public static <T> Collection<T> fill(Collection<T> coll, Generator<T> gen, int n) {
for (int i = 0; i < n; i++) {
coll.add(gen.next());
}
return coll;
}
public static void main(String[] args) {
Collection<Coffee> coffee = fill(new ArrayList<Coffee>(), new CoffeeGenerator(), 4);
for (Coffee c : coffee) {
System.out.println(c);
}
Collection<Integer> fnumbers = fill(new ArrayList<Integer>(), new Fibonacci(), 12);
for (int i : fnumbers) {
System.out.print(i + " ");
}
}
}