From 5e523fda0b40fecbab82b1a1977899c3971395f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= Date: Mon, 29 Nov 2021 15:17:27 +0100 Subject: [PATCH] chapter 8 done --- .../C081_Enum/App.java | 3 + .../C082_Tableaux/Exercice1.java | 4 +- .../C083_Operations_sur_tableaux/App.java | 11 +++- .../Exercice2.java | 56 +++++++++++++++++++ .../Exercice3.java | 29 ++++++++++ 5 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 src/C08_Structures_de_donnees/C083_Operations_sur_tableaux/Exercice2.java create mode 100644 src/C08_Structures_de_donnees/C083_Operations_sur_tableaux/Exercice3.java diff --git a/src/C08_Structures_de_donnees/C081_Enum/App.java b/src/C08_Structures_de_donnees/C081_Enum/App.java index 37d8487..d5b06ec 100644 --- a/src/C08_Structures_de_donnees/C081_Enum/App.java +++ b/src/C08_Structures_de_donnees/C081_Enum/App.java @@ -1,5 +1,8 @@ package C08_Structures_de_donnees.C081_Enum; +/** + * @author Rémi Heredero + */ enum Glace {VANILLE, CHOCOLAT, FRAISE, MANGUE}; // enum Cat {MINOU, CHATONS, CONNAIS_PAS_D_AUTRES, TOTO}; public class App { diff --git a/src/C08_Structures_de_donnees/C082_Tableaux/Exercice1.java b/src/C08_Structures_de_donnees/C082_Tableaux/Exercice1.java index 073f5b6..01460db 100644 --- a/src/C08_Structures_de_donnees/C082_Tableaux/Exercice1.java +++ b/src/C08_Structures_de_donnees/C082_Tableaux/Exercice1.java @@ -1,5 +1,8 @@ package C08_Structures_de_donnees.C082_Tableaux; +/** + * @author Rémi Heredero + */ public class Exercice1 { public static void main(String[] args) { @@ -14,6 +17,5 @@ public class Exercice1 { for (String string : bar) { System.out.println(string); } - } } diff --git a/src/C08_Structures_de_donnees/C083_Operations_sur_tableaux/App.java b/src/C08_Structures_de_donnees/C083_Operations_sur_tableaux/App.java index 5c844b4..926e732 100644 --- a/src/C08_Structures_de_donnees/C083_Operations_sur_tableaux/App.java +++ b/src/C08_Structures_de_donnees/C083_Operations_sur_tableaux/App.java @@ -1,7 +1,16 @@ package C08_Structures_de_donnees.C083_Operations_sur_tableaux; +/** + * @author Rémi Heredero + */ public class App { public static void main(String[] args) throws Exception { - System.out.println("Hello, World!"); + int[][] foo = new int[3][3]; + for (int[] is : foo) { + for (int is2 : is) { + is2 = 1; + System.out.println(is2); + } + } } } diff --git a/src/C08_Structures_de_donnees/C083_Operations_sur_tableaux/Exercice2.java b/src/C08_Structures_de_donnees/C083_Operations_sur_tableaux/Exercice2.java new file mode 100644 index 0000000..b443ea9 --- /dev/null +++ b/src/C08_Structures_de_donnees/C083_Operations_sur_tableaux/Exercice2.java @@ -0,0 +1,56 @@ +package C08_Structures_de_donnees.C083_Operations_sur_tableaux; +/** + * @author Rémi Heredero + * @Klagarge + */ +public class Exercice2 { + public static void main(String[] args) { + + int[] ex2_1 = {2, 5, 3, 1}; + int grand = 0; + for (int i : ex2_1) { + grand = i>grand ? i:grand; + } + System.out.println("Exercice 2.1: " + grand); + + String[] ex2_2 = {"hello", "je suis", "edmund"}; + int e = 0; + for (String s : ex2_2) { + for (int i = 0; i < s.length(); i++) { + if (s.charAt(i) == 'e') { + e++; + } + } + } + System.out.println("Exercice 2.2: " + e); + + int[] v1 = {1,2,3}; + int[] v2 = {4,3,2}; + int [] sum = add(v1, v2); + for (int i : sum) { + System.out.println(i); + } + + + + } + + /** + * @author Rémi Heredero + * @param a tableau d'entrée a + * @param b tableau d'entrée b + * @return somme des deux tableaux d'entrée + */ + public static int[] add(int[] a, int[] b) { + if (a.length != b.length) { + return null; + } + + int[] tmp = new int[a.length]; + + for (int i = 0; i < tmp.length; i++) { + tmp[i] = a[i] + b[i]; + } + return tmp; + } +} \ No newline at end of file diff --git a/src/C08_Structures_de_donnees/C083_Operations_sur_tableaux/Exercice3.java b/src/C08_Structures_de_donnees/C083_Operations_sur_tableaux/Exercice3.java new file mode 100644 index 0000000..fa51223 --- /dev/null +++ b/src/C08_Structures_de_donnees/C083_Operations_sur_tableaux/Exercice3.java @@ -0,0 +1,29 @@ +package C08_Structures_de_donnees.C083_Operations_sur_tableaux; + +/** + * @author Rémi Heredero + * @Klagarge + */ +public class Exercice3 { + public static void main(String[] args) { + + double[] a = {1.2, 3.2, 4.4, 5.5}; + double[] b = {1.2, 2.1, 3.3, 2.5, 4.5}; + + int Ra = isSorted(a); // returns -1, the array is sorted + int Rb = isSorted(b); // returns 3, as 2.5 is < than 3.3 + + System.out.println(Ra); + System.out.println(Rb); + + } + + public static int isSorted(double[] a) { + for (int i = 1; i < a.length; i++) { + if (a[i] < a[i-1]) { + return i; + } + } + return -1; + } +}