diff --git a/DEV 3.2/TP01/Fréquence/Frequence.java b/DEV 3.2/TP01/Fréquence/Frequence.java index e1dff8b..cc9897e 100644 --- a/DEV 3.2/TP01/Fréquence/Frequence.java +++ b/DEV 3.2/TP01/Fréquence/Frequence.java @@ -1,7 +1,5 @@ -import java.util.Collections; import java.util.Hashtable; import java.util.Map; -import java.util.function.BiConsumer; public class Frequence { public T mostFrequent(T[] tab) { diff --git a/DEV 3.2/TP01/Fréquences/Association.java b/DEV 3.2/TP01/Fréquences/Association.java index 9112a67..16f08c4 100644 --- a/DEV 3.2/TP01/Fréquences/Association.java +++ b/DEV 3.2/TP01/Fréquences/Association.java @@ -2,12 +2,22 @@ public class Association { private T element; private int frequency; + private Association suivant; + public Association(T e, int f) { this.element = e; this.frequency = f; } + public Association getSuivant() { + return suivant; + } + + public void setSuivant(Association suivant) { + this.suivant = suivant; + } + public void setElement(T e) { this.element = e; } @@ -24,10 +34,6 @@ public class Association { return this.frequency; } - public Association suivant() { - - } - @Override public String toString() { return "[" + this.element + ", " + this.frequency + "]"; diff --git a/DEV 3.2/TP01/Fréquences/Frequences.java b/DEV 3.2/TP01/Fréquences/Frequences.java index e69de29..9a10422 100644 --- a/DEV 3.2/TP01/Fréquences/Frequences.java +++ b/DEV 3.2/TP01/Fréquences/Frequences.java @@ -0,0 +1,59 @@ +import java.util.Hashtable; +import java.util.Map; + +public class Frequences { + public T mostFrequent(T[] tab) { + Hashtable indexList = new Hashtable(); + + for (T element : tab) { + indexList.put(element, indexList.get(element) == null ? 1 : ((Integer)indexList.get(element)) + 1); + } + + T element = tab[0]; + Integer count = indexList.get(tab[0]); + + for(Map.Entry entry : indexList.entrySet()) { + if (entry.getValue() > count) { + element = entry.getKey(); + count = entry.getValue(); + } + } + + return element; + } + + public Liste getFrequences(T[] tab) { + Hashtable indexList = new Hashtable(); + + for (T element : tab) { + indexList.put(element, indexList.get(element) == null ? 1 : ((Integer)indexList.get(element)) + 1); + } + + Association lastEntry = null; + boolean first = true; + + Liste list = new Liste(); + + for(Map.Entry entry : indexList.entrySet()) { + if (first) { + lastEntry = new Association(entry.getKey(), entry.getValue()); + list.setFirstElement(lastEntry); + first = false; + } else { + Association newEntry = new Association(entry.getKey(), entry.getValue()); + lastEntry.setSuivant(newEntry); + lastEntry = newEntry; + } + } + + return list; + } + + public static void main(String[] args) { + Frequences f = new Frequences(); + + String[] tab = {"test", "test", "test", "12", "12", "12", "12"}; + + System.out.println(f.mostFrequent(tab)); + } +} diff --git a/DEV 3.2/TP01/Fréquences/Liste.java b/DEV 3.2/TP01/Fréquences/Liste.java new file mode 100644 index 0000000..fcab6e9 --- /dev/null +++ b/DEV 3.2/TP01/Fréquences/Liste.java @@ -0,0 +1,41 @@ +public class Liste { + private Association a; + + public Liste() { + } + + public void setFirstElement(Association e) { + this.a = e; + } + + public void add(T element) { + Association b = a; + + boolean found = false; + while (b.getSuivant() != null) { + if (b.getElement().equals(element)) { + b.setFrequency(b.getFrequency() + 1); + found = true; + break; + } else b = b.getSuivant(); + } + + if (!found) { + b.setSuivant(new Association(element, 1)); + } + } + + @Override + public String toString() { + String str = ""; + + Association b = a; + while (b.getSuivant() != null) { + str += b.toString() + ", "; + b = b.getSuivant(); + } + + str = str.substring(0, str.length() - 2); + return str; + } +} diff --git a/DEV 3.2/TP02/Appels/Appels.java b/DEV 3.2/TP02/Appels/Appels.java new file mode 100644 index 0000000..1161f1d --- /dev/null +++ b/DEV 3.2/TP02/Appels/Appels.java @@ -0,0 +1,33 @@ +/** + * Appels + */ +public class Appels { + + private static int factorielle(int n, int indent) { + for (int i = 0; i < indent; i++) System.out.print(" "); + System.out.println("input: "+ n); + + int res; + switch (n) { + case 0: + res = 0; + break; + + case 1: + res = 1; + break; + + default: + res = n * factorielle(n-1, indent+1); + break; + } + + for (int i = 0; i < indent; i++) System.out.print(" "); + System.out.println("output: " + res); + return res; + } + + public static void main(String[] args) { + System.out.println(factorielle(7, 0)); + } +} \ No newline at end of file diff --git a/DEV 3.2/TP02/Tableaux/Tableaux.java b/DEV 3.2/TP02/Tableaux/Tableaux.java new file mode 100644 index 0000000..a86639e --- /dev/null +++ b/DEV 3.2/TP02/Tableaux/Tableaux.java @@ -0,0 +1,48 @@ +import java.util.Arrays; + +public class Tableaux { + + public static int[] getTab(String[] args, int index, int[] tab) { + if (index == 0) { + if (args.length == 0) return new int[0]; + tab = new int[args.length]; + tab[0] = Integer.parseInt(args[0]); + getTab(args, index + 1, tab); + } else if (index < args.length) { + tab[index] = Integer.parseInt(args[index]); + getTab(args, index + 1, tab); + } + + return tab; + } + + public static int maxN(int[] tab, int index) { + if (index < tab.length) { + int n = maxN(tab, index + 1); + return tab[index] > n ? tab[index] : n; + } else return Integer.MIN_VALUE; + } + + public static int getEven(int[] tab, int index, int n) { + if (index == 0) n = 0; + + + if (index < tab.length) { + return (tab[index] % 2 == 0 ? 1 : 0) + getEven(tab, index + 1, n); + } else return 0; + } + + public static void showTab(int[] tab, int index) { + if (index < tab.length) { + showTab(tab, index + 1); + System.out.print(tab[index] + " "); + } if (index == 0) System.out.print("\n"); + } + + public static void main(String[] args) { + int[] tab = getTab(args, 0, null); + System.out.println(getEven(tab, 0, 0)); + System.out.println(maxN(tab, 0)); + showTab(tab, 0); + } +}