From f3380f8888ef58425e0596b209528ed55dda3e27 Mon Sep 17 00:00:00 2001 From: Giacomo Radaelli Date: Wed, 11 Jan 2023 17:27:33 +0100 Subject: [PATCH] Aggiunta soluzione prof a ExpGenerator MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Soluzione sbagliata o trascrizione sbagliata? Ultimo numero stampato รจ sempre 1.0 --- 6.x/p6.12/Sol Prof/ExpGenerator.java | 29 ++++++++++++++++++++++++++++ 6.x/p6.12/Sol Prof/ExpTester.java | 23 ++++++++++++++++++++++ 6.x/p6.12/Sol Prof/Sequence.java | 4 ++++ 3 files changed, 56 insertions(+) create mode 100644 6.x/p6.12/Sol Prof/ExpGenerator.java create mode 100644 6.x/p6.12/Sol Prof/ExpTester.java create mode 100644 6.x/p6.12/Sol Prof/Sequence.java 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