chap3
This commit is contained in:
32
bac2/modalDev/tp1/app/.classpath
Normal file
32
bac2/modalDev/tp1/app/.classpath
Normal file
@ -0,0 +1,32 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" output="bin/main" path="src/main/java">
|
||||
<attributes>
|
||||
<attribute name="gradle_scope" value="main"/>
|
||||
<attribute name="gradle_used_by_scope" value="main,test"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" output="bin/main" path="src/main/resources">
|
||||
<attributes>
|
||||
<attribute name="gradle_scope" value="main"/>
|
||||
<attribute name="gradle_used_by_scope" value="main,test"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" output="bin/test" path="src/test/java">
|
||||
<attributes>
|
||||
<attribute name="gradle_scope" value="test"/>
|
||||
<attribute name="gradle_used_by_scope" value="test"/>
|
||||
<attribute name="test" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" output="bin/test" path="src/test/resources">
|
||||
<attributes>
|
||||
<attribute name="gradle_scope" value="test"/>
|
||||
<attribute name="gradle_used_by_scope" value="test"/>
|
||||
<attribute name="test" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17/"/>
|
||||
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>
|
||||
<classpathentry kind="output" path="bin/default"/>
|
||||
</classpath>
|
34
bac2/modalDev/tp1/app/.project
Normal file
34
bac2/modalDev/tp1/app/.project
Normal file
@ -0,0 +1,34 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>app</name>
|
||||
<comment>Project app created by Buildship.</comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
|
||||
</natures>
|
||||
<filteredResources>
|
||||
<filter>
|
||||
<id>1701330804524</id>
|
||||
<name></name>
|
||||
<type>30</type>
|
||||
<matcher>
|
||||
<id>org.eclipse.core.resources.regexFilterMatcher</id>
|
||||
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
|
||||
</matcher>
|
||||
</filter>
|
||||
</filteredResources>
|
||||
</projectDescription>
|
@ -0,0 +1,13 @@
|
||||
arguments=
|
||||
auto.sync=false
|
||||
build.scans.enabled=false
|
||||
connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER)
|
||||
connection.project.dir=
|
||||
eclipse.preferences.version=1
|
||||
gradle.user.home=
|
||||
java.home=
|
||||
jvm.arguments=
|
||||
offline.mode=false
|
||||
override.workspace.settings=false
|
||||
show.console.view=false
|
||||
show.executions.view=false
|
@ -0,0 +1,32 @@
|
||||
package tp1.ex2_1;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Suite de tests pour vérifier la méthode sqrt dans la classe Sqrt
|
||||
*/
|
||||
public class SolveEquationTest {
|
||||
@Test
|
||||
public void test1() {
|
||||
SolveEquation se = new SolveEquation(1.0, -2.0, -3.0);
|
||||
assertEquals(se.getNbSolutions(), 2);
|
||||
assertTrue(se.getSolutions().getE1()
|
||||
.equals(se.getSolutions().getE2()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test2() {
|
||||
double epsilon = 0.0001;
|
||||
SolveEquation se = new SolveEquation(1.0, 0.0, 0.0);
|
||||
assertEquals(se.getSolutions().getE1(),
|
||||
se.getSolutions().getE2(), epsilon);
|
||||
assertEquals(se.getNbSolutions(), 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test3() {
|
||||
SolveEquation se = new SolveEquation(5.0, 0.0, 3.0);
|
||||
assertEquals(se.getSolutions().getE1(), se.getSolutions().getE2());
|
||||
}
|
||||
}
|
45
bac2/modalDev/tp1/app/build.gradle.kts
Normal file
45
bac2/modalDev/tp1/app/build.gradle.kts
Normal file
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* This file was generated by the Gradle 'init' task.
|
||||
*
|
||||
* This generated file contains a sample Java application project to get you started.
|
||||
* For more details on building Java & JVM projects, please refer to https://docs.gradle.org/8.4/userguide/building_java_projects.html in the Gradle documentation.
|
||||
*/
|
||||
|
||||
plugins {
|
||||
// Apply the application plugin to add support for building a CLI application in Java.
|
||||
application
|
||||
}
|
||||
|
||||
repositories {
|
||||
// Use Maven Central for resolving dependencies.
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
// Use JUnit Jupiter for testing.
|
||||
testImplementation("org.junit.jupiter:junit-jupiter:5.9.3")
|
||||
|
||||
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
|
||||
|
||||
// This dependency is used by the application.
|
||||
implementation("com.google.guava:guava:32.1.1-jre")
|
||||
|
||||
implementation("org.mockito:mockito-core:5.7.0")
|
||||
}
|
||||
|
||||
// Apply a specific Java toolchain to ease working on different environments.
|
||||
java {
|
||||
toolchain {
|
||||
languageVersion.set(JavaLanguageVersion.of(17))
|
||||
}
|
||||
}
|
||||
|
||||
application {
|
||||
// Define the main class for the application.
|
||||
mainClass.set("tp1.App")
|
||||
}
|
||||
|
||||
tasks.named<Test>("test") {
|
||||
// Use JUnit Platform for unit tests.
|
||||
useJUnitPlatform()
|
||||
}
|
15
bac2/modalDev/tp1/app/src/main/java/tp1/App.java
Normal file
15
bac2/modalDev/tp1/app/src/main/java/tp1/App.java
Normal file
@ -0,0 +1,15 @@
|
||||
/*
|
||||
* This Java source file was generated by the Gradle 'init' task.
|
||||
*/
|
||||
package tp1;
|
||||
|
||||
public class App {
|
||||
// for the sake of showing an example of testing
|
||||
public String getGreeting() {
|
||||
return "Hello World!";
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(new App().getGreeting());
|
||||
}
|
||||
}
|
27
bac2/modalDev/tp1/app/src/main/java/tp1/ex2_1/Pair.java
Normal file
27
bac2/modalDev/tp1/app/src/main/java/tp1/ex2_1/Pair.java
Normal file
@ -0,0 +1,27 @@
|
||||
package tp1.ex2_1;
|
||||
|
||||
public class Pair<T> {
|
||||
private T e1, e2;
|
||||
|
||||
public Pair(T e1, T e2) {
|
||||
this.e1 = e1;
|
||||
this.e2 = e2;
|
||||
}
|
||||
|
||||
public T getE1() {
|
||||
return e1;
|
||||
}
|
||||
|
||||
public T getE2() {
|
||||
return e2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj instanceof Pair) {
|
||||
Pair<?> p = (Pair<?>) obj;
|
||||
return e1.equals(p.e1) && e2.equals(p.e2);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
package tp1.ex2_1;
|
||||
|
||||
public class SolveEquation {
|
||||
private double a, b, c;
|
||||
|
||||
public SolveEquation(double a, double b, double c) {
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
this.c = c;
|
||||
}
|
||||
|
||||
/*
|
||||
* Gives the number of solutions of the equation .
|
||||
*
|
||||
* @return the number of solution .
|
||||
*
|
||||
* @throws ArithmeticException if there
|
||||
* is an infinite number of solutions .
|
||||
*/
|
||||
public int getNbSolutions() throws ArithmeticException {
|
||||
double delta = b * b - 4 * a * c;
|
||||
if (delta < 0)
|
||||
throw new ArithmeticException("No real solution");
|
||||
else if (delta == 0)
|
||||
return 1;
|
||||
else
|
||||
return 2;
|
||||
}
|
||||
|
||||
/*
|
||||
* * Gives the equation ’s solutions for x .
|
||||
*
|
||||
* @return the 2 solutions of the equation . If there is
|
||||
* only 1 solution , the 2 members of the pair are equal .
|
||||
*
|
||||
* @throws ArithmeticException if there is no real
|
||||
* solution for x , or if there is an infinite number
|
||||
* of solutions .
|
||||
*/
|
||||
public Pair<Double> getSolutions() throws ArithmeticException {
|
||||
double delta = b * b - 4 * a * c;
|
||||
if (delta < 0)
|
||||
throw new ArithmeticException("No real solution");
|
||||
else if (delta == 0) {
|
||||
double x = -b / (2 * a);
|
||||
return new Pair<Double>(x, x);
|
||||
}
|
||||
else
|
||||
return new Pair<Double>(
|
||||
(-b - Math.sqrt(delta)) / (2 * a),
|
||||
(-b + Math.sqrt(delta)) / (2 * a));
|
||||
}
|
||||
}
|
40
bac2/modalDev/tp1/app/src/main/java/tp1/ex2_2/Sqrt.java
Normal file
40
bac2/modalDev/tp1/app/src/main/java/tp1/ex2_2/Sqrt.java
Normal file
@ -0,0 +1,40 @@
|
||||
package tp1.ex2_2;
|
||||
|
||||
import java.lang.Math;
|
||||
|
||||
/**
|
||||
* Calcule la racine carrée d'un nombre positif
|
||||
*/
|
||||
public class Sqrt
|
||||
{
|
||||
public static final double EPSILON = 1e-8;
|
||||
|
||||
/**
|
||||
* Cacule la racine carrée d'un nombre positif en utilisant la méhtode
|
||||
* de Newton.
|
||||
* Il s'agit de calculer la racine de l'équation x^2-b=0 avec b
|
||||
* le nombre dont on veut la racine carrée.
|
||||
* La méthode de Newton consiste à utiliser la formule suivante :
|
||||
* x_i = x_(i-1) - f(x_(i-1)) / f'(x_(i-1)).
|
||||
* @param val La valeur dont on cherche la racine carrée (val >= 0)
|
||||
* @return La racine carrée de val avec une précision de 1e-12.
|
||||
* @throws IllegalArgumentException Si val < 0
|
||||
*/
|
||||
public static double sqrt(double val) throws IllegalArgumentException
|
||||
{
|
||||
if(val < 0)
|
||||
throw new IllegalArgumentException("ne peut pas être négatif");
|
||||
if(val == 0 || val == 1)
|
||||
return val;
|
||||
|
||||
double curr = nextIteration(val, val);
|
||||
while(Math.abs(val - Math.pow(curr, 2)) > EPSILON){
|
||||
curr = nextIteration(val, curr);
|
||||
}
|
||||
return curr;
|
||||
}
|
||||
|
||||
private static double nextIteration(double val, double curr){
|
||||
return (curr + (val / curr))/2;
|
||||
}
|
||||
}
|
19
bac2/modalDev/tp1/app/src/main/java/tp1/ex2_3/Fibonacci.java
Normal file
19
bac2/modalDev/tp1/app/src/main/java/tp1/ex2_3/Fibonacci.java
Normal file
@ -0,0 +1,19 @@
|
||||
package tp1.ex2_3;
|
||||
|
||||
public class Fibonacci {
|
||||
public static int fibo(int n) throws IllegalArgumentException {
|
||||
if (n < 0) {
|
||||
throw new IllegalArgumentException();
|
||||
} else {
|
||||
int f_n_1 = 1;
|
||||
int f_n = 0;
|
||||
int tmp;
|
||||
for (int i = 0; i < n; i++) {
|
||||
tmp = f_n;
|
||||
f_n = f_n + f_n_1;
|
||||
f_n_1 = tmp;
|
||||
}
|
||||
return f_n;
|
||||
}
|
||||
}
|
||||
}
|
14
bac2/modalDev/tp1/app/src/test/java/tp1/AppTest.java
Normal file
14
bac2/modalDev/tp1/app/src/test/java/tp1/AppTest.java
Normal file
@ -0,0 +1,14 @@
|
||||
/*
|
||||
* This Java source file was generated by the Gradle 'init' task.
|
||||
*/
|
||||
package tp1;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class AppTest {
|
||||
@Test void appHasAGreeting() {
|
||||
App classUnderTest = new App();
|
||||
assertNotNull(classUnderTest.getGreeting(), "app should have a greeting");
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package tp1.ex2_1;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Suite de tests pour vérifier la méthode sqrt dans la classe Sqrt
|
||||
*/
|
||||
public class SolveEquationTest {
|
||||
@Test
|
||||
public void test1() {
|
||||
SolveEquation se = new SolveEquation(1.0, -2.0, -3.0);
|
||||
assertEquals(se.getNbSolutions(), 2);
|
||||
assertFalse(se.getSolutions().getE1()
|
||||
.equals(se.getSolutions().getE2()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test2() {
|
||||
double epsilon = 0.0001;
|
||||
SolveEquation se = new SolveEquation(1.0, 0.0, 0.0);
|
||||
assertEquals(se.getSolutions().getE1(),
|
||||
se.getSolutions().getE2(), epsilon);
|
||||
assertEquals(se.getNbSolutions(), 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test3() {
|
||||
SolveEquation se = new SolveEquation(5.0, 0.0, 3.0);
|
||||
assertThrows(ArithmeticException.class, () -> se.getSolutions(), "No real solution");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test4(){
|
||||
SolveEquation se = new SolveEquation(3, 0, 0);
|
||||
assertEquals(se.getNbSolutions(), 1);
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package tp1.ex2_1;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Suite de tests pour vérifier la méthode sqrt dans la classe Sqrt
|
||||
*/
|
||||
public class SolveEquationTest {
|
||||
@Test
|
||||
public void test1() {
|
||||
SolveEquation se = new SolveEquation(1.0, -2.0, -3.0);
|
||||
assertEquals(se.getNbSolutions(), 2);
|
||||
assertTrue(se.getSolutions().getE1()
|
||||
.equals(se.getSolutions().getE2()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test2() {
|
||||
double epsilon = 0.0001;
|
||||
SolveEquation se = new SolveEquation(1.0, 0.0, 0.0);
|
||||
assertEquals(se.getSolutions().getE1(),
|
||||
se.getSolutions().getE2(), epsilon);
|
||||
assertEquals(se.getNbSolutions(), 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test3() {
|
||||
SolveEquation se = new SolveEquation(5.0, 0.0, 3.0);
|
||||
assertEquals(se.getSolutions().getE1(), se.getSolutions().getE2());
|
||||
}
|
||||
}
|
49
bac2/modalDev/tp1/app/src/test/java/tp1/ex2_2/SqrtTest.java
Normal file
49
bac2/modalDev/tp1/app/src/test/java/tp1/ex2_2/SqrtTest.java
Normal file
@ -0,0 +1,49 @@
|
||||
package tp1.ex2_2;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
||||
/**
|
||||
* Suite de tests pour vérifier la méthode sqrt dans la classe Sqrt
|
||||
*/
|
||||
public class SqrtTest {
|
||||
@Test
|
||||
public void testSortie(){
|
||||
assertTrue(Sqrt.sqrt(42) >= 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testprecision(){
|
||||
assertEquals(Math.sqrt(16), Sqrt.sqrt(16), Sqrt.EPSILON);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidInput(){
|
||||
double t1 = Sqrt.sqrt(0.4);
|
||||
assertTrue(t1 > 0 && t1 < 1);
|
||||
|
||||
double t2 = Sqrt.sqrt(21);
|
||||
assertTrue(t2 > 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBorne(){
|
||||
assertEquals(0, Sqrt.sqrt(0));
|
||||
assertEquals(1, Sqrt.sqrt(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBig(){
|
||||
double val = assertTimeoutPreemptively(Duration.ofSeconds(1), () -> Sqrt.sqrt(1e16));
|
||||
assertEquals(Math.sqrt(1e16), val, Sqrt.EPSILON);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalid(){
|
||||
assertThrows(IllegalArgumentException.class, () -> Sqrt.sqrt(-42));
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package tp1.ex2_3;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class FibonacciTest {
|
||||
@Test
|
||||
void test1() {
|
||||
assertEquals(2, Fibonacci.fibo(2));
|
||||
}
|
||||
|
||||
@Test void test2() {
|
||||
assertTrue(Fibonacci.fibo(-2) >= 0);
|
||||
}
|
||||
|
||||
@Test void test3() {
|
||||
assertTrue(Fibonacci.fibo(4) == 3);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user