Completato esercizio p6.9 con Sequence.
Implementata interfaccia Sequence nella classe PrimeGenerator
This commit is contained in:
@@ -5,13 +5,15 @@
|
||||
*/
|
||||
public class PrimeGenerator implements Sequence<Integer> {
|
||||
private int max;
|
||||
private int i = 1;
|
||||
private int attuale;
|
||||
|
||||
/**
|
||||
* Costruttore parametrico completo
|
||||
* @param max Numero massimo che deve essere stampato
|
||||
*/
|
||||
public PrimeGenerator(int max) {
|
||||
this.max = max;
|
||||
attuale = 2;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -19,23 +21,25 @@ public class PrimeGenerator implements Sequence<Integer> {
|
||||
* @return Numero successivo nella sequenza
|
||||
*/
|
||||
public Integer next() {
|
||||
int counter = 0;
|
||||
int j = 1;
|
||||
while (true) {
|
||||
boolean primo = true;
|
||||
|
||||
while(j <= i && counter <= 2)
|
||||
{
|
||||
counter = 0;
|
||||
if(i % j == 0)
|
||||
{
|
||||
counter++;
|
||||
for (int i = 2; i < attuale; i++) {
|
||||
if (attuale % i == 0) {
|
||||
primo = false;
|
||||
}
|
||||
j++;
|
||||
}
|
||||
|
||||
return Integer.MIN_VALUE;
|
||||
if (primo) {
|
||||
int prossimo = attuale;
|
||||
attuale++;
|
||||
return prossimo;
|
||||
}
|
||||
attuale++;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean hasNext() {
|
||||
return true;
|
||||
return attuale < max;
|
||||
}
|
||||
}
|
@@ -1,5 +1,9 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Classe di test per PrimeGenerator
|
||||
* @author radaelli11353
|
||||
*/
|
||||
public class Test {
|
||||
public static void main(String[] args) {
|
||||
Scanner in = new Scanner(System.in);
|
||||
@@ -8,8 +12,7 @@ public class Test {
|
||||
PrimeGenerator primi = new PrimeGenerator(max);
|
||||
|
||||
while(primi.hasNext()) {
|
||||
int numero = primi.next();
|
||||
if(numero != 0) System.out.println(numero);
|
||||
System.out.println(primi.next());
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user