diff --git a/.gitmodules b/.gitmodules index 5c80bfe63e33a51dd6a613ffa532408aa7a60ec7..64fdd556422b073c9ece19a8bc57ac332f30c8a1 100644 --- a/.gitmodules +++ b/.gitmodules @@ -13,3 +13,6 @@ [submodule "ZITF"] path = ZITF url = git@gitlab.isima.fr:rovandemer/zitf.git +[submodule "Related Personal Projects/pandoc-filter"] + path = Related Personal Projects/pandoc-filter + url = git@gitlab.isima.fr:rovandemer/students-pandoc-filters.git diff --git a/POO/TD/TD3/pdn.md b/POO/TD/TD3/pdn.md new file mode 100644 index 0000000000000000000000000000000000000000..c3bfdd5214b75f8fcdaf81eaa0d286994ddfac1c --- /dev/null +++ b/POO/TD/TD3/pdn.md @@ -0,0 +1,236 @@ +# Exercice 1 (Villes et pays) + +## Question 1 + +<!-- + +On va faire un diagramme pour représenter les classe City, Capital, et Country + +-> Un pays est constituté d'un ensemble de villes, mais d'exactement une capitale + +--> + + +```plantuml +@startuml +class Country { + -name : String + -capital : Capital + -cities : City[] + -addCities(Enumeration<City>) : boolean +} +class Capital { + -name : String + -country : Country +} +class City { + -name : String + -country : Country +} +' Capital hérite de City, on marque le subset +Capital <|-- City + +' Un pays est constitué d'un ensemble de villes, mais d'exactement une capitale +Country *-- City +Country "1" - "1" Capital : Capital {subsets City} + +@enduml +``` + +## Question 2 + +On implémente les classes : + +```java +public class Country { + private String name; + private Capital capital; + private ArrayList<City> cities; + + public Country(String name, Capital capital, City[] cities) { + this.name = name; + this.capital = capital; + this.cities = cities; + + // On vérifie que toutes les villes ne sont pas une instance de Capital + for (City city : cities) { + if (city instanceof Capital) { + throw new IllegalArgumentException("A country can't have a Capital as a city"); + } + } + + } + + public String getName() { + return name; + } + + public Capital getCapital() { + return capital; + } + + public City getCity(int index) { + return cities.get(index); + } +} + +public class Capital extends City { + public Capital(String name, Country country) { + super(name, country); + } +} + +public class City { + private String name; + private Country country; + + public City(String name, Country country) { + this.name = name; + this.country = country; + } + + public String getName() { + return name; + } + + public Country getCountry() { + return country; + } +} +``` + + +## Question 3 + +On fait en sorte qu'un pays n'aie qu'une seule capitale avec : + +```java +// On vérifie que toutes les villes ne sont pas une instance de Capital +for (City city : cities) { + if (city instanceof Capital) { + throw new IllegalArgumentException("A country can't have a Capital as a city"); + } +} +``` + +## Question 4 + +Le code final est : + +```java +class Country implements Enumeration<City> { + private String name; + private Capital capital; + private HashSet<City> cities; + + public Country(String name, Capital capital) { + this.name = name; + this.capital = capital; + this.cities = new HashSet<City>(); + this.addCity(capital); + } + + public addCity(City city) { + if (city instanceof Capital) { + throw new IllegalArgumentException("A country can't have a Capital as a city"); + } + + this.cities.add(city); + } + + @Override + public boolean hasMoreElements() { + return cities.size() > 0; + } + + @Override + public City nextElement() { + return cities.remove(0); + } +} +``` + +# Exercice 2 (Héritage Multiple) + +## Question 1 + +```plantuml +@startuml +class Person { + -name : String + -animals : Animal[] + -addAnimal(Animal) : boolean +} +class Animal { + -name : String + -run() : void + -cry() : void +} +class Dog { + -name : String + -run() : void + -cry() : void +} +class Cat { + -name : String + -run() : void + -cry() : void +} +class Horse { + -name : String + -run() : void + -cry() : void +} +' Un animal peut courir et crier +Animal <|-- Dog +Animal <|-- Cat +Animal <|-- Horse +' Une personne peut posséder des animaux domestiques +Person *-- Animal + +@enduml +``` + +## Question 2 + +<!-- Une personne peut posséder un véhicule. Le véhicule est en +général une auto ou une moto. Un véhicule a une capacité (deux personne pour la +moto, quatre personnes pour la voiture), peut avancer (à condition d’avoir au moins +un occupant), et une personne peut y entrer (tant qu’il reste de la place) ou en sortir. --> + +```plantuml +@startuml +class Person { + -name : String + -animals : Animal[] + -addAnimal(Animal) : boolean +} +class Vehicle { + -capacity : int + -occupants : Person[] + -move() : void + -enter(Person) : boolean + -exit(Person) : boolean +} +' Une auto ou moto hérite de Vehicle +class Car { + -capacity : int + -occupants : Person[] + -move() : void + -enter(Person) : boolean + -exit(Person) : boolean +} +class Motorcycle { + -capacity : int + -occupants : Person[] + -move() : void + -enter(Person) : boolean + -exit(Person) : boolean +} +' Un véhicule peut avancer et accueillir des personnes +Vehicle <|-- Car +Vehicle <|-- Motorcycle +' Une personne peut posséder un véhicule +Person "1" -- "1" Vehicle +@enduml +``` \ No newline at end of file diff --git a/POO/TP/TP4/src/HashMap.java b/POO/TP/TP4/src/HashMap.java index 69d1fc489211e416f91d3fb4b3f42f7ef6349ac8..ccaae3affb33c23d3f8f2daba8496bc90dd92763 100644 --- a/POO/TP/TP4/src/HashMap.java +++ b/POO/TP/TP4/src/HashMap.java @@ -1,107 +1,107 @@ package TP4.src; import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Objects; public class HashMap<K, V> { + // On stock les éléments dans un tableau de listes private int size; - private ArrayList<K> keys; - private ArrayList<V> values; + private ArrayList<Pair>[] table; - public HashMap() { + class Pair { + private K key; + private V value; - this.size = 0; - this.keys = new ArrayList<K>(); - this.values = new ArrayList<V>(); - - } - - public void put(K key, V value) { - - if (this.keys.contains(key)) { - - int index = this.keys.indexOf(key); - this.values.set(index, value); - - } else { - - this.keys.add(key); - this.values.add(value); - this.size++; + public Pair(K key, V value) { + this.key = key; + this.value = value; + } + public K getKey() { + return key; } + public V getValue() { + return value; + } } - - public V get(K key) { - - if (this.keys.contains(key)) { - - int index = this.keys.indexOf(key); - return this.values.get(index); - } else { - - return null; + @SuppressWarnings("unchecked") + public HashMap(int size) { + this.size = size; + table = new ArrayList[size]; + for (int i = 0; i < size; i++) { + table[i] = new ArrayList<Pair>(); } - } - public void remove(K key) { + public void put(K key, V value) { - if (this.keys.contains(key)) { + if (key == null) { + throw new IllegalArgumentException("Key cannot be null"); + } - int index = this.keys.indexOf(key); - this.keys.remove(index); - this.values.remove(index); - this.size--; + int hash = key.hashCode() % size; + if (Objects.isNull(table[hash])) { + table[hash] = new ArrayList<Pair>(); } + table[hash].add(new Pair(key, value)); } + + public V get(K key) { + // getEntry + int hash = key.hashCode() % size; - public int size() { + if (Objects.isNull(table[hash])) { + return null; + } - return this.size; + for (Pair pair : table[hash]) { + if (pair.getKey().equals(key)) { + return pair.getValue(); + } + } + return null; } - public boolean containsKey(K key) { - - return this.keys.contains(key); + public boolean contains(K key) { + int hash = key.hashCode() % size; + return !(Objects.isNull(table[hash]) || Objects.isNull(get(key))); } public boolean containsValue(V value) { + for (ArrayList<Pair> list : table) { + for (Pair pair : list) { + if (pair.getValue().equals(value)) { + return true; + } + } + } - return this.values.contains(value); - + return false; } + + public void delete(K key) { + int hash = key.hashCode() % size; - // Si c'est un int, on autorise le get - public int getObject(K key) { - - // On vérifie le type - if (this.values.get(0) instanceof Integer) { - - // On vérifie si la clé existe - if (this.keys.contains(key)) { - - int index = this.keys.indexOf(key); - return (int) this.values.get(index); - - } else { - - return -1; - - } + if (Objects.isNull(table[hash])) { + throw new IllegalArgumentException("Key not found"); + } - } else { + table[hash].remove(key.hashCode() % size); + } - return -1; + public int size() { - } + return this.size(); } diff --git a/POO/TP/TP4/src/Liste.java b/POO/TP/TP4/src/Liste.java index 635c18b1ab57389477bcae6e9ae4f55ff9b962e1..875a3c53c6457dd12a444f4de8e5d1c9ea929c61 100644 --- a/POO/TP/TP4/src/Liste.java +++ b/POO/TP/TP4/src/Liste.java @@ -60,7 +60,6 @@ class Liste<T> { public void ajouter(int index, T element) { if (index < 0 || index > this.nbElements) { - System.out.println("Index invalide"); return; } @@ -93,7 +92,6 @@ class Liste<T> { if (index < 0 || index >= this.nbElements) { - System.out.println("Index invalide"); return; } @@ -138,7 +136,6 @@ class Liste<T> { if (index < 0 || index >= this.nbElements) { - System.out.println("Index invalide"); return null; } @@ -151,7 +148,6 @@ class Liste<T> { if (index < 0 || index >= this.nbElements) { - System.out.println("Index invalide"); return; } diff --git a/POO/TP/TP4/src/Main.java b/POO/TP/TP4/src/Main.java index d13432e61183829a848e6591d14f82264040895a..6cf3682d5addb6acac2ae42fa6b6c3ae9430e4d1 100644 --- a/POO/TP/TP4/src/Main.java +++ b/POO/TP/TP4/src/Main.java @@ -1,5 +1,6 @@ package TP4.src; +import java.util.ArrayList; import java.util.Random; public class Main { @@ -42,7 +43,7 @@ public class Main { // liste.afficher(); // ------------------------ Exercice 3 ------------------------ - // HashMap<String, Integer> map = new HashMap<String, Integer>(); + // HashMap<String, Integer> map = new HashMap<String, Integer>(10); // map.put("a", 1); // map.put("b", 2); @@ -52,62 +53,47 @@ public class Main { // ------------------------ Exercice 4 ------------------------ // On va faire une Liste, un HashMap et un tableau - // On va faire n insertions de 100 Étudiants dans les 3 structures, et comparer le temps d'exécution - - long n = 1000000; - - Liste<Etudiant> liste; - HashMap<Integer, Etudiant> map; - Etudiant[] tableau; - - long debut, fin; + // On va faire n insertions de 100 Étudiants dans les 3 structures, et comparer le temps d'exécution + + + + long n = 10000000; - String nomAlea, prenomAlea; - int ageAlea, numeroEtudiantAlea, noteAlea; + // On créer un tableau de 100 étudiants + Etudiant[] tableau = new Etudiant[100]; Random random = new Random(); - // On remplit la liste - debut = System.currentTimeMillis(); - - for (int i = 0; i < n; i++) { - - liste = new Liste<Etudiant>(); + for (int i = 0; i < 100; i++) { - for (int j = 0; j < 100; j++) { - - nomAlea = "Nom" + random.nextInt(100); - prenomAlea = "Prenom" + random.nextInt(100); - ageAlea = random.nextInt(100); - numeroEtudiantAlea = random.nextInt(100); - noteAlea = random.nextInt(100); + String nomAlea = "Nom" + random.nextInt(100); + String prenomAlea = "Prenom" + random.nextInt(100); + int ageAlea = random.nextInt(100); + int numeroEtudiantAlea = random.nextInt(100); + int noteAlea = random.nextInt(100); - liste.ajouter(new Etudiant(nomAlea, prenomAlea, ageAlea, numeroEtudiantAlea, noteAlea)); - - } + tableau[i] = new Etudiant(nomAlea, prenomAlea, ageAlea, numeroEtudiantAlea, noteAlea); } - fin = System.currentTimeMillis(); + // Maintenant on va faire les tests + Etudiant[] testTableau = new Etudiant[100]; + Liste<Etudiant> testListe = new Liste<Etudiant>(); + HashMap<Integer, Etudiant> testMap = new HashMap<Integer, Etudiant>(100); - System.out.println("Temps d'insertion pour la liste: " + (double) (fin - debut) / 1000 + "s"); + long debut, fin; - // On remplit le HashMap + // On remplit le tableau debut = System.currentTimeMillis(); for (int i = 0; i < n; i++) { - map = new HashMap<Integer, Etudiant>(); - for (int j = 0; j < 100; j++) { + testTableau = new Etudiant[100]; - nomAlea = "Nom" + random.nextInt(100); - prenomAlea = "Prenom" + random.nextInt(100); - ageAlea = random.nextInt(100); - numeroEtudiantAlea = random.nextInt(100); - noteAlea = random.nextInt(100); + for (int j = 0; j < 100; j++) { - map.put(j, new Etudiant(nomAlea, prenomAlea, ageAlea, numeroEtudiantAlea, noteAlea)); + testTableau[j] = tableau[j]; } @@ -115,24 +101,19 @@ public class Main { fin = System.currentTimeMillis(); - System.out.println("Temps d'insertion pour le HashMap: " + (double) (fin - debut) / 1000 + "s"); + System.out.println("Temps d'exécution pour remplir le tableau : " + (fin - debut) + "ms"); - // On remplit le tableau + // On remplit la liste debut = System.currentTimeMillis(); for (int i = 0; i < n; i++) { - tableau = new Etudiant[100]; - for (int j = 0; j < 100; j++) { + testListe = new Liste<Etudiant>(); - nomAlea = "Nom" + random.nextInt(100); - prenomAlea = "Prenom" + random.nextInt(100); - ageAlea = random.nextInt(100); - numeroEtudiantAlea = random.nextInt(100); - noteAlea = random.nextInt(100); + for (int j = 0; j < 100; j++) { - tableau[j] = new Etudiant(nomAlea, prenomAlea, ageAlea, numeroEtudiantAlea, noteAlea); + testListe.ajouter(tableau[j]); } @@ -140,227 +121,110 @@ public class Main { fin = System.currentTimeMillis(); - System.out.println("Temps d'insertion pour le tableau: " + (double) (fin - debut) / 1000 + "s"); - - - - - - long tempsTotal; // On va calculer le temps QUE pour le calcul de la moyenne + System.out.println("Temps d'exécution pour remplir la liste : " + (fin - debut) + "ms"); - - - // On calcule la moyenne des notes des étudiants (on regénère les étudiants à chaque fois) - - tempsTotal = 0; + // On remplit le HashMap + debut = System.currentTimeMillis(); for (int i = 0; i < n; i++) { - liste = new Liste<Etudiant>(); - - for (int j = 0; j < 100; j++) { - - nomAlea = "Nom" + random.nextInt(100); - prenomAlea = "Prenom" + random.nextInt(100); - ageAlea = random.nextInt(100); - numeroEtudiantAlea = random.nextInt(100); - noteAlea = random.nextInt(100); - liste.ajouter(new Etudiant(nomAlea, prenomAlea, ageAlea, numeroEtudiantAlea, noteAlea)); - - } - - debut = System.currentTimeMillis(); + testMap = new HashMap<Integer, Etudiant>(100); for (int j = 0; j < 100; j++) { - liste.get(j).getNote(); + testMap.put(tableau[j].getNumeroEtudiant(), tableau[j]); } - fin = System.currentTimeMillis(); - - tempsTotal += fin - debut; - } + fin = System.currentTimeMillis(); - System.out.println("Temps de calcul de la moyenne pour la liste: " + (double) tempsTotal / 1000 + "s"); + System.out.println("Temps d'exécution pour remplir le HashMap : " + (fin - debut) + "ms"); - tempsTotal = 0; + // On récupère un élément du tableau + debut = System.currentTimeMillis(); for (int i = 0; i < n; i++) { - map = new HashMap<Integer, Etudiant>(); - - for (int j = 0; j < 100; j++) { - - nomAlea = "Nom" + random.nextInt(100); - prenomAlea = "Prenom" + random.nextInt(100); - ageAlea = random.nextInt(100); - numeroEtudiantAlea = random.nextInt(100); - noteAlea = random.nextInt(100); - - map.put(j, new Etudiant(nomAlea, prenomAlea, ageAlea, numeroEtudiantAlea, noteAlea)); - - } - - - debut = System.currentTimeMillis(); - - for (int j = 0; j < 100; j++) { - - map.get(j).getNote(); - - } - - fin = System.currentTimeMillis(); - - tempsTotal += fin - debut; + Etudiant a = testTableau[50]; } - System.out.println("Temps de calcul de la moyenne pour le HashMap: " + (double) tempsTotal / 1000 + "s"); - - tempsTotal = 0; - - for (int i = 0; i < n; i++) { - - tableau = new Etudiant[100]; - - for (int j = 0; j < 100; j++) { - - nomAlea = "Nom" + random.nextInt(100); - prenomAlea = "Prenom" + random.nextInt(100); - ageAlea = random.nextInt(100); - numeroEtudiantAlea = random.nextInt(100); - noteAlea = random.nextInt(100); - - tableau[j] = new Etudiant(nomAlea, prenomAlea, ageAlea, numeroEtudiantAlea, noteAlea); - - } - - debut = System.currentTimeMillis(); - - for (int j = 0; j < 100; j++) { + fin = System.currentTimeMillis(); - tableau[j].getNote(); + System.out.println("Temps d'exécution pour récupérer un élément du tableau : " + (fin - debut) + "ms"); - } + // On récupère un élément de la liste + debut = System.currentTimeMillis(); - fin = System.currentTimeMillis(); + for (int i = 0; i < n; i++) { - tempsTotal += fin - debut; + Etudiant a = testListe.get(50); } - System.out.println("Temps de calcul de la moyenne pour le tableau: " + (double) tempsTotal / 1000 + "s"); - + fin = System.currentTimeMillis(); + System.out.println("Temps d'exécution pour récupérer un élément de la liste : " + (fin - debut) + "ms"); - // Maintenant on va rechercher un étudiant dans la liste - tempsTotal = 0; + // On récupère un élément du HashMap + debut = System.currentTimeMillis(); for (int i = 0; i < n; i++) { - liste = new Liste<Etudiant>(); - - for (int j = 0; j < 100; j++) { - - nomAlea = "Nom" + random.nextInt(100); - prenomAlea = "Prenom" + random.nextInt(100); - ageAlea = random.nextInt(100); - numeroEtudiantAlea = random.nextInt(100); - noteAlea = random.nextInt(100); + Etudiant a = testMap.get(50); - liste.ajouter(new Etudiant(nomAlea, prenomAlea, ageAlea, numeroEtudiantAlea, noteAlea)); - - } - - debut = System.currentTimeMillis(); - - liste.contient(new Etudiant("Nom50", "Prenom50", 50, 50, 50)); - - fin = System.currentTimeMillis(); + } - tempsTotal += fin - debut; + fin = System.currentTimeMillis(); - } + System.out.println("Temps d'exécution pour récupérer un élément du HashMap : " + (fin - debut) + "ms"); - System.out.println("Temps de recherche pour la liste: " + (double) tempsTotal / 1000 + "s"); + Etudiant target = new Etudiant("Nom50", "Prenom50", 50, 50, 50); - // Maintenant on va rechercher un étudiant dans le HashMap - tempsTotal = 0; + // On recherche un élément du tableau + debut = System.currentTimeMillis(); for (int i = 0; i < n; i++) { - map = new HashMap<Integer, Etudiant>(); - for (int j = 0; j < 100; j++) { - nomAlea = "Nom" + random.nextInt(100); - prenomAlea = "Prenom" + random.nextInt(100); - ageAlea = random.nextInt(100); - numeroEtudiantAlea = random.nextInt(100); - noteAlea = random.nextInt(100); - - map.put(j, new Etudiant(nomAlea, prenomAlea, ageAlea, numeroEtudiantAlea, noteAlea)); + // Si l'étudiant est target + if (testTableau[j].equals(target)) { - } - - debut = System.currentTimeMillis(); + Etudiant a = testTableau[j]; - map.containsValue(new Etudiant("Nom50", "Prenom50", 50, 50, 50)); - - fin = System.currentTimeMillis(); + } - tempsTotal += fin - debut; + } } - System.out.println("Temps de recherche pour le HashMap: " + (double) tempsTotal / 1000 + "s"); - - // Maintenant on va rechercher un étudiant dans le tableau - tempsTotal = 0; - - for (int i = 0; i < n; i++) { - - tableau = new Etudiant[100]; - - for (int j = 0; j < 100; j++) { - - nomAlea = "Nom" + random.nextInt(100); - prenomAlea = "Prenom" + random.nextInt(100); - ageAlea = random.nextInt(100); - numeroEtudiantAlea = random.nextInt(100); - noteAlea = random.nextInt(100); - - tableau[j] = new Etudiant(nomAlea, prenomAlea, ageAlea, numeroEtudiantAlea, noteAlea); - - } - - debut = System.currentTimeMillis(); + fin = System.currentTimeMillis(); - for (int j = 0; j < 100; j++) { + System.out.println("Temps d'exécution pour rechercher un élément du tableau : " + (fin - debut) + "ms"); - if (tableau[j].equals(new Etudiant("Nom50", "Prenom50", 50, 50, 50))) { + // On recherche un élément de la liste + debut = System.currentTimeMillis(); - break; + boolean found = testListe.contient(target); - } + fin = System.currentTimeMillis(); - } + System.out.println("Temps d'exécution pour rechercher un élément de la liste : " + (fin - debut) + "ms"); - fin = System.currentTimeMillis(); + // On recherche un élément du HashMap + debut = System.currentTimeMillis(); - tempsTotal += fin - debut; + found = testMap.containsValue(target); - } + fin = System.currentTimeMillis(); - System.out.println("Temps de recherche pour le tableau: " + (double) tempsTotal / 1000 + "s"); + System.out.println("Temps d'exécution pour rechercher un élément du HashMap : " + (fin - debut) + "ms"); - } diff --git a/POO/annales/Joueur.java b/POO/annales/Joueur.java new file mode 100644 index 0000000000000000000000000000000000000000..939bea99ef7ac64d2c7e84d9c52ae768600976ad --- /dev/null +++ b/POO/annales/Joueur.java @@ -0,0 +1,3 @@ +class Joueur { + +} \ No newline at end of file diff --git a/POO/annales/animal.java b/POO/annales/animal.java new file mode 100644 index 0000000000000000000000000000000000000000..fc2870b092b94190c6848b382e3f68a39d003386 --- /dev/null +++ b/POO/annales/animal.java @@ -0,0 +1,28 @@ +/** + * test + */ +public interface animal { + + public void crier(); + + public void manger(); + + public void dormir(); + +} + +public class chat implements animal { + + public void crier() { + System.out.println("Miaou"); + } + + public void manger() { + System.out.println("Je mange du poisson"); + } + + public void dormir() { + System.out.println("Je dors dans un panier"); + } + +} \ No newline at end of file diff --git a/Related Personal Projects/pandoc-filter b/Related Personal Projects/pandoc-filter new file mode 160000 index 0000000000000000000000000000000000000000..87615c02ff66d6836e9ceb22ee3e0cac7460e8b7 --- /dev/null +++ b/Related Personal Projects/pandoc-filter @@ -0,0 +1 @@ +Subproject commit 87615c02ff66d6836e9ceb22ee3e0cac7460e8b7