JAVA
java
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
// Created by Tibor Santa
abstract class Pipe { /* abstract, no method bodies */
private Pipe feedingPipe; /* the feeding object */
protected Pipe(Pipe feedingPipe) { /* constructor */
this.feedingPipe = feedingPipe;
}
protected Pipe getFeedingPipe() { /* concrete method but not a public concern, only open to the subclasses */
return feedingPipe;
}
abstract boolean hasNextInteger(); /* abstract method, the implementation depends on the type of pipe */
abstract Integer nextInteger();
}
/* Provides a stream of numbers. must keep track of which number to provide next
* ------ Testing with if, if the Array is full, return null */
final class Feeder extends Pipe {
private final int[] integers; /* final */
private int nextPosition = 0;
public Feeder(int[] integers) {
super(null); /* because abstract superclass constructor must be called */
this.integers = integers;
}
public boolean hasNextInteger() {
return nextPosition < integers.length; /* if the array still has unused elements */
}
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run