From dc16cd2e7aa8cd830478b932f424db4b2a87c3ba Mon Sep 17 00:00:00 2001 From: Giacomo Radaelli Date: Sun, 8 Jan 2023 16:57:54 +0100 Subject: [PATCH] Completato esercizio p6.12 con Sequence --- 6.x/p6.12/ExpApprossimator.java | 37 +++++++++++++++++++++++++++++++++ 6.x/p6.12/ExpTester.java | 23 ++++++++++++++++++++ 6.x/p6.12/Sequence.java | 4 ++++ 3 files changed, 64 insertions(+) create mode 100644 6.x/p6.12/ExpApprossimator.java create mode 100644 6.x/p6.12/ExpTester.java create mode 100644 6.x/p6.12/Sequence.java diff --git a/6.x/p6.12/ExpApprossimator.java b/6.x/p6.12/ExpApprossimator.java new file mode 100644 index 0000000..5d943c5 --- /dev/null +++ b/6.x/p6.12/ExpApprossimator.java @@ -0,0 +1,37 @@ +/** + Metodo che calcola e^x + @author radaelli11353 + */ +public class ExpApprossimator implements Sequence { + private double term; + private double x; + private int n; + + /** + Costruttore parametrico completo + @param x Valore a cui elevare e + */ + public ExpApprossimator(double x) { + term = 1; + this.x = x; + n = 1; + } + + /** + Metodo che calcola il valore successivo nella sequenza + @return Addendo successivo nella sequenza + */ + public Double next() { + term = term*x/n; + n++; + return term; + } + + /** + Metodo che verifica se la precisione 1E-9 (arbitraria) รจ stata raggiunta + @return Vero o falso in base alle approssimazioni eseguite + */ + public boolean hasNext() { + return term > 1E-9; + } +} \ No newline at end of file diff --git a/6.x/p6.12/ExpTester.java b/6.x/p6.12/ExpTester.java new file mode 100644 index 0000000..3921da1 --- /dev/null +++ b/6.x/p6.12/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(); + + ExpApprossimator gen = new ExpApprossimator(x); + + double sum = 1; + while(gen.hasNext()) { + sum += gen.next(); + } + + System.out.println(sum); + } +} \ No newline at end of file diff --git a/6.x/p6.12/Sequence.java b/6.x/p6.12/Sequence.java new file mode 100644 index 0000000..4f11dd1 --- /dev/null +++ b/6.x/p6.12/Sequence.java @@ -0,0 +1,4 @@ +public interface Sequence { + public boolean hasNext(); + public T next(); +} \ No newline at end of file