Skip to content
Snippets Groups Projects
Commit 5da80c4d authored by BAUER Oscar's avatar BAUER Oscar
Browse files

first testing works

parent f6860d8a
No related branches found
No related tags found
No related merge requests found
Showing
with 75 additions and 231 deletions
public class TestTd16 {
public static void main(String[] args) {
int num = td16.expoFor(3,3);
System.out.println(num);
double numRec = td16.expoRec(3,3);
System.out.println(numRec);
System.out.println(td16.fiboRec(6));
}
}
package interfaces;
public interface Data {
@Override
String toString();
}
package regular;
import interfaces.Data;
public class BinaryData implements Data {
byte[] data;
public BinaryData(byte[] data) {
this.data = data;
}
private static String bytesToHex(byte[] bytes) {
StringBuilder hexString = new StringBuilder();
for (byte b : bytes) {
// Convert each byte to a two-digit hex string
String hex = String.format("%02X", b);
hexString.append(hex);
}
return hexString.toString();
}
@Override
public String toString() {
return bytesToHex(data);
}
}
package regular;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import interfaces.Data;
public class CsvSaver {
Writer writer;
public CsvSaver(Writer writer) {
this.writer = writer;
}
public void write(Data data) {
try {
writer.write(data.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package regular;
import interfaces.Data;
public class StringData implements Data {
private String data;
public StringData(String data) {
this.data = data;
}
@Override
public String toString() {
return data;
}
}
package td14.exo1;
public class BisectionUtils {
/* Fonction f(x) = x * x - 4 */
static double f(double x) {
return x * x - 4;
}
/* Mthode de dichotomie (on suppose que f(a) et f(b) sont de signes opposs) */
static double findZero(double a, double b, double epsilon) {
/* Si f(a) >= 0, on change a et b */
if (f(a) > 0) {
double temp = a;
a = b;
b = temp;
}
/* Itrations jusqu' |a - b| <= epsilon */
while (Math.abs(b - a) > epsilon) {
double mid = (a + b) / 2;
if (f(mid) < 0) {
a = mid;
} else {
b = mid;
}
}
/* Lorsque |a - b| <= epsilon, on retourne une valeur entre a et b */
return (a + b) / 2;
}
}
package td14.exo1;
public class BisectionUtilsTest {
public static void main(String[] args) {
double result = BisectionUtils.findZero(0, 4, 1e-12);
System.out.println(result);
}
}
package td14.exo6;
public class Addition extends BinaryOperation {
public Addition(Expression ex1, Expression ex2) {
super(ex1, ex2);
}
@Override
public double value(double x) {
return left.value(x) + right.value(x);
}
}
package td14.exo6;
public abstract class BinaryOperation implements Expression {
protected Expression left;
protected Expression right;
public BinaryOperation(Expression left, Expression right) {
this.left = left;
this.right = right;
}
@Override
public abstract double value(double x);
}
package td14.exo6;
public class Constant implements Expression {
private double value;
public Constant(double value) {
this.value = value;
}
@Override
public double value(double x) {
return this.value;
}
}
package td14.exo6;
public class Cos extends UnaryOperation {
public Cos(Expression ex) {
super(ex);
}
@Override
public double value(double x) {
return Math.cos(expression.value(x));
}
}
package td14.exo6;
public class Division extends BinaryOperation {
public Division(Expression ex1, Expression ex2) {
super(ex1, ex2);
}
@Override
public double value(double x) {
if(right.value(x) == 0)
return Integer.MIN_VALUE;
return left.value(x) / right.value(x);
}
}
package td14.exo6;
public class Exp extends UnaryOperation {
public Exp(Expression ex) {
super(ex);
}
@Override
public double value(double x) {
return Math.pow(expression.value(x), expression.value(x));
}
}
package td14.exo6;
public interface Expression {
public double value(double x);
}
package td14.exo6;
public class Log extends UnaryOperation {
public Log(Expression ex) {
super(ex);
}
@Override
public double value(double x) {
return Math.log(expression.value(x));
}
}
package td14.exo6;
public class Multiplication extends BinaryOperation{
public Multiplication(Expression ex1, Expression ex2) {
super(ex1, ex2);
}
@Override
public double value(double x) {
return left.value(x) * right.value(x);
}
}
package td14.exo6;
public class Sin extends UnaryOperation {
public Sin(Expression ex) {
super(ex);
}
@Override
public double value(double x) {
return Math.sin(expression.value(x));
}
}
package td14.exo6;
public class Subtraction extends BinaryOperation {
public Subtraction(Expression ex1, Expression ex2) {
super(ex1, ex2);
}
@Override
public double value(double x) {
return left.value(x) - right.value(x);
}
}
package td14.exo6;
public class Testing {
public static void main(String[] args) {
// Représentation de la fonction f(x) = 2 * sin(x) + 3 * cos(x)
Expression f = new Addition(new Multiplication(new Constant(2), new Sin(new Variable())),
new Multiplication(new Constant(3), new Cos(new Variable())));
// Calcul de la valeur de f(x) pour plusieurs valeurs de x
double[] values = { 0, 0.5, 1, 1.5, 2, 2.5 };
for (double x : values) {
System.out.println("f(" + x + ") = " + f.value(x));
}
}
}
package td14.exo6;
public abstract class UnaryOperation implements Expression {
protected Expression expression;
public UnaryOperation(Expression expression) {
this.expression = expression;
}
@Override
public abstract double value(double x);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment