Aggiunta soluzione prof a ExpGenerator
Soluzione sbagliata o trascrizione sbagliata? Ultimo numero stampato è sempre 1.0
This commit is contained in:
parent
ed085aaf36
commit
f3380f8888
29
6.x/p6.12/Sol Prof/ExpGenerator.java
Normal file
29
6.x/p6.12/Sol Prof/ExpGenerator.java
Normal file
@ -0,0 +1,29 @@
|
||||
public class ExpGenerator implements Sequence<Double>{
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
23
6.x/p6.12/Sol Prof/ExpTester.java
Normal file
23
6.x/p6.12/Sol Prof/ExpTester.java
Normal file
@ -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);
|
||||
}
|
||||
}
|
4
6.x/p6.12/Sol Prof/Sequence.java
Normal file
4
6.x/p6.12/Sol Prof/Sequence.java
Normal file
@ -0,0 +1,4 @@
|
||||
public interface Sequence<T> {
|
||||
public boolean hasNext();
|
||||
public T next();
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user