Die Klasse, die ich entwickelt habe, aber wie ich jetzt noch weiterkomme, weiss ich leider
nicht, so dass bei dem Aufruf getNext() auf der Konsole 1, 2, 4, 5, 7, 8, 10 angezeigt
wird
public interface Sequence {
int getNext();
}
public class Naturals implements Sequence{
private int value = 0;
Naturals(){
}
public Naturals(int zahl){
this.value = zahl;
}
public int getNext(){
return value++;
}
public static void main(String[] args) {
Sequence s = new ZapMultiples(3, new Naturals());
// Sequence s = new Naturals();
//Sequence s = new Primes();
while(true) {
System.out.println(s.getNext() + " ");
}
}
}
public abstract class Filter implements Sequence{
private Sequence s;
Filter(){
}
protected Filter(Sequence s) {
this.s = s;
}
public int getNext() {
return s.getNext();
}
}
public class ZapMultiples extends Filter {
private int base;
private int zahl = 1;
ZapMultiples(Sequence s) {
super(s);
}
ZapMultiples(int zahl, Sequence s) {
super(s);
this.base = zahl;
}
public int getZahl() {
return base;
}
public void setZahl(int zahl) {
this.base = zahl;
}
public boolean absorbieren(final int zahl) {
return zahl % base != 0;
}
public int getNext() {
int result = 0;
while(zahl > 0) {
zahl++;
}
if(!absorbieren(zahl)) {
result++;
}
return result;
}
}
public class Primes extends Filter {
private int value = 0;
public int getNext() {
final int result = value;
if (value == 2)
value = 3;
else
do
value += 2;
while (value > 0 && !isPrime(value));
return result;
}
public static boolean isPrime(final int n) {
if (n == 2)
return true;
if (n % 2 == 0)
return false;
final int zahl = (int) Math.sqrt(n);
for (int t = 3; t <= zahl; t += 2)
if (n % t == 0)
return false;
return true;
}
}