Skip to content
Snippets Groups Projects
Commit 010686e6 authored by Robin VAN DE MERGHEL's avatar Robin VAN DE MERGHEL :computer:
Browse files

New course on computers for OOP

parent 4d6b1ee8
No related branches found
No related tags found
No related merge requests found
......@@ -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
# 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
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() {
this.size = 0;
this.keys = new ArrayList<K>();
this.values = new ArrayList<V>();
class Pair {
private K key;
private V value;
public Pair(K key, V value) {
this.key = key;
this.value = value;
}
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 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 int size() {
public V get(K key) {
// getEntry
int hash = key.hashCode() % size;
return this.size;
if (Objects.isNull(table[hash])) {
return null;
}
for (Pair pair : table[hash]) {
if (pair.getKey().equals(key)) {
return pair.getValue();
}
}
public boolean containsKey(K key) {
return null;
}
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) {
return this.values.contains(value);
for (ArrayList<Pair> list : table) {
for (Pair pair : list) {
if (pair.getValue().equals(value)) {
return true;
}
}
}
// 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 false;
}
return -1;
public void delete(K key) {
int hash = key.hashCode() % size;
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();
}
......
......@@ -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;
}
......
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);
......@@ -54,60 +55,45 @@ public class Main {
// 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;
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 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);
for (int i = 0; i < 100; i++) {
liste.ajouter(new Etudiant(nomAlea, prenomAlea, ageAlea, numeroEtudiantAlea, noteAlea));
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);
}
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,226 +121,109 @@ 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
// On calcule la moyenne des notes des étudiants (on regénère les étudiants à chaque fois)
System.out.println("Temps d'exécution pour remplir la liste : " + (fin - debut) + "ms");
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");
tempsTotal = 0;
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));
}
System.out.println("Temps d'exécution pour remplir le HashMap : " + (fin - debut) + "ms");
// On récupère un élément du tableau
debut = System.currentTimeMillis();
for (int j = 0; j < 100; j++) {
for (int i = 0; i < n; i++) {
map.get(j).getNote();
Etudiant a = testTableau[50];
}
fin = System.currentTimeMillis();
tempsTotal += fin - debut;
}
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);
}
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();
for (int j = 0; j < 100; j++) {
for (int i = 0; i < n; i++) {
tableau[j].getNote();
Etudiant a = testListe.get(50);
}
fin = System.currentTimeMillis();
tempsTotal += fin - debut;
}
System.out.println("Temps de calcul de la moyenne pour le tableau: " + (double) tempsTotal / 1000 + "s");
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);
liste.ajouter(new Etudiant(nomAlea, prenomAlea, ageAlea, numeroEtudiantAlea, noteAlea));
Etudiant a = testMap.get(50);
}
debut = System.currentTimeMillis();
liste.contient(new Etudiant("Nom50", "Prenom50", 50, 50, 50));
fin = System.currentTimeMillis();
tempsTotal += fin - debut;
}
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);
// Si l'étudiant est target
if (testTableau[j].equals(target)) {
map.put(j, new Etudiant(nomAlea, prenomAlea, ageAlea, numeroEtudiantAlea, noteAlea));
Etudiant a = testTableau[j];
}
debut = System.currentTimeMillis();
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);
fin = System.currentTimeMillis();
}
System.out.println("Temps d'exécution pour rechercher un élément du tableau : " + (fin - debut) + "ms");
// On recherche un élément de la liste
debut = System.currentTimeMillis();
for (int j = 0; j < 100; j++) {
boolean found = testListe.contient(target);
if (tableau[j].equals(new Etudiant("Nom50", "Prenom50", 50, 50, 50))) {
fin = System.currentTimeMillis();
break;
System.out.println("Temps d'exécution pour rechercher un élément de la liste : " + (fin - debut) + "ms");
}
// On recherche un élément du HashMap
debut = System.currentTimeMillis();
}
found = testMap.containsValue(target);
fin = System.currentTimeMillis();
tempsTotal += fin - debut;
}
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");
}
......
class Joueur {
}
\ No newline at end of file
/**
* 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
Subproject commit 87615c02ff66d6836e9ceb22ee3e0cac7460e8b7
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment