diff --git a/6.x/p6.12/Sol Prof/ExpGenerator.java b/6.x/p6.12/Sol Prof/ExpGenerator.java new file mode 100644 index 0000000..19f8f59 --- /dev/null +++ b/6.x/p6.12/Sol Prof/ExpGenerator.java @@ -0,0 +1,29 @@ +public class ExpGenerator implements Sequence{ + private double x, nuovo, old, potenza, fattoriale; + private int n; + public static final double EPSILON = 1E-6; + + public ExpGenerator(double x) { + this.x = x; + nuovo = 1; + old = 0; + potenza = 1; + fattoriale = 1; + n = 0; + } + + public boolean hasNext() { + return Math.abs(nuovo-old) >= EPSILON; + } + + public Double next() { + if(!hasNext()) throw new IllegalArgumentException(); + old = nuovo; + n++; + potenza *= x; + fattoriale *= n; + nuovo = old + potenza/fattoriale; + return old; + } + +} \ No newline at end of file diff --git a/6.x/p6.12/Sol Prof/ExpTester.java b/6.x/p6.12/Sol Prof/ExpTester.java new file mode 100644 index 0000000..d9ffab4 --- /dev/null +++ b/6.x/p6.12/Sol Prof/ExpTester.java @@ -0,0 +1,23 @@ +import java.util.Scanner; + +/** + Classe che esegue il test dell'uso della funzione ExpApprossimator + @author radaelli11353 +*/ +public class ExpTester { + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + + System.out.print("Inserisci il valore di x"); + double x = in.nextDouble(); + + ExpGenerator gen = new ExpGenerator(x); + + double sum = 1; + while(gen.hasNext()) { + System.out.println(gen.next()); + } + + System.out.println(sum); + } +} \ No newline at end of file diff --git a/6.x/p6.12/Sol Prof/Sequence.java b/6.x/p6.12/Sol Prof/Sequence.java new file mode 100644 index 0000000..4f11dd1 --- /dev/null +++ b/6.x/p6.12/Sol Prof/Sequence.java @@ -0,0 +1,4 @@ +public interface Sequence { + public boolean hasNext(); + public T next(); +} \ No newline at end of file