DEV3.2 TP'S
This commit is contained in:
33
DEV/DEV3.2/TP01/Exercise1/GenericList.java
Normal file
33
DEV/DEV3.2/TP01/Exercise1/GenericList.java
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
class GenericList {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
ArrayList<Integer> integerList = new ArrayList<>(); // Peut recevoir des Integer
|
||||||
|
integerList.add(1);
|
||||||
|
integerList.add(2);
|
||||||
|
integerList.add(3);
|
||||||
|
|
||||||
|
ArrayList<Float> floatList = new ArrayList<>(); // Peut recevoir des Float
|
||||||
|
floatList.add(1.1f);
|
||||||
|
floatList.add(1.2f);
|
||||||
|
floatList.add(1.3f);
|
||||||
|
|
||||||
|
long longNumber = 1000000000L;
|
||||||
|
float floatNumber = 5.3f;
|
||||||
|
int integerNumber = 10;
|
||||||
|
ArrayList<Number> numberList = new ArrayList<>(); // Peut recevoir des Float, Integer et Long
|
||||||
|
numberList.add(longNumber);
|
||||||
|
numberList.add(floatNumber);
|
||||||
|
numberList.add(integerNumber);
|
||||||
|
|
||||||
|
System.out.println(integerList.toString());
|
||||||
|
System.out.println(floatList.toString());
|
||||||
|
System.out.println(numberList.toString());
|
||||||
|
|
||||||
|
// La méthode addAll() copie le contenu d'une collection dans la collection courante.
|
||||||
|
// Le type de la collection doit être de type E ou extends E.
|
||||||
|
// Ici, les class Float, Integer et Long extends de Number, donc on peut faire les opérations suivantes :
|
||||||
|
// - numberList.addAll(integerList);
|
||||||
|
// - numberList.addAll(floatList);
|
||||||
|
}
|
||||||
|
}
|
||||||
10
DEV/DEV3.2/TP01/Exercise2/ArrayMethods.java
Normal file
10
DEV/DEV3.2/TP01/Exercise2/ArrayMethods.java
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public class ArrayMethods {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println(args.toString()); // a)
|
||||||
|
System.out.println(Arrays.copyOf(args, 5)); // b)
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
27
DEV/DEV3.2/TP02/Exercise1/Recursion.java
Normal file
27
DEV/DEV3.2/TP02/Exercise1/Recursion.java
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
public class Recursion {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
if(args.length > 0) {
|
||||||
|
displayFactorial(Integer.parseInt(args[0]));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Usage: java Recursion <number>");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void displayFactorial(int n) {
|
||||||
|
System.out.println(n + "! = " + factorial(n, 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int factorial(int n, int count) {
|
||||||
|
count++;
|
||||||
|
System.out.println("n = " + n + " | count = " + count);
|
||||||
|
|
||||||
|
if(n == 0) {
|
||||||
|
//Thread.dumpStack();
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return n * factorial(n - 1, count);
|
||||||
|
}
|
||||||
|
}
|
||||||
14
DEV/DEV3.2/TP02/Exercise1/answer.txt
Normal file
14
DEV/DEV3.2/TP02/Exercise1/answer.txt
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
Avec utilisation de dumpStack() :
|
||||||
|
On test pour factoriel de 3, on y voit bien 4 appels :
|
||||||
|
- 3 qui appellent le cas réussit
|
||||||
|
- 1 qui appelle le cas de base
|
||||||
|
|
||||||
|
nathanpasdutout@Macbook-Air-Nathan Exercise1 % java Recursion 3
|
||||||
|
java.lang.Exception: Stack trace
|
||||||
|
at java.base/java.lang.Thread.dumpStack(Thread.java:2209)
|
||||||
|
at Recursion.factorial(Recursion.java:18)
|
||||||
|
at Recursion.factorial(Recursion.java:22)
|
||||||
|
at Recursion.factorial(Recursion.java:22)
|
||||||
|
at Recursion.factorial(Recursion.java:22)
|
||||||
|
at Recursion.displayFactorial(Recursion.java:13)
|
||||||
|
at Recursion.main(Recursion.java:5)
|
||||||
35
DEV/DEV3.2/TP02/Exercise2/ArrayRecursion.java
Normal file
35
DEV/DEV3.2/TP02/Exercise2/ArrayRecursion.java
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
public class ArrayRecursion {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
displayNumberOfPaireNumber(convertCommandArray(args));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int[] convertCommandArray(String[] args) {
|
||||||
|
return convertArray(args, 0, new int[args.length]);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int[] convertArray(String[] args, int index, int[] integerArray) {
|
||||||
|
if(index >= args.length) {
|
||||||
|
return integerArray;
|
||||||
|
}
|
||||||
|
|
||||||
|
integerArray[index] = Integer.parseInt(args[index]);
|
||||||
|
return convertArray(args, index + 1, integerArray);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void displayNumberOfPaireNumber(int[] array) {
|
||||||
|
System.out.println("Nombre de nombres paires : " + getNumberOfPaireNumber(array, 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int getNumberOfPaireNumber(int[] array, int index) {
|
||||||
|
if(index >= array.length) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(array[index] % 2 == 0) {
|
||||||
|
return getNumberOfPaireNumber(array, index + 1) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return getNumberOfPaireNumber(array, index + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user