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

OOP couse (TP and TD)

parent 8cdcf17b
No related branches found
No related tags found
No related merge requests found
import java.util.List;
class Instant {
private int heures; // 0 <= heure < 24
......@@ -146,4 +148,53 @@ class Instant {
}
public static Instant getSmallestInstant(Instant recherche, List<Instant> instants) {
Instant instant = null;
if (recherche == null) {
recherche = new Instant(0, 0, 0);
}
for (Instant i : instants) {
if (i.compareTo(recherche) > 0) {
if (instant == null) {
instant = i;
} else {
if (i.compareTo(instant) < 0) {
instant = i;
}
}
}
}
return instant;
}
public static void sort(List<Instant> instants) {
// On utilise getSmallestInstant pour trouver le plus petit
// On le met à la fin de la liste
// On recommence jusqu'à ce qu'il n'y ait plus d'éléments
Instant instant = null;
for (int i = 0; i < instants.size(); i++) {
instant = getSmallestInstant(instant, instants);
if (instant != null) {
instants.remove(instant);
instants.add(instant);
}
}
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
......@@ -13,7 +15,6 @@ class Main {
// ------------------------- Information utiles -----------------
// [ VOUS POUVEZ DÉCOMMENTER LES LIGNES CI-DESSOUS POUR TESTER VOTRE CODE AVEC `CTRL /` DANS VISUAL STUDIO CODE ]
// ------------------------- Question 2 -------------------------
// // Création d'une instance
// Instant2 instant = new Instant2(12, 30, 0);
......@@ -63,11 +64,83 @@ class Main {
// Equals prend en paramètre un objet, non pas un Instant
// PS : On peut avoir A == B et B != A si A et B ne sont pas de la même classe
// ------------------------- Question 5 -------------------------
// On créer un tableau d'instants
Instant[] instants = new Instant[3];
// // On créer un tableau d'instants
// Instant[] instants = new Instant[3];
// Random random = new Random();
// int randomHour;
// int randomMinute;
// int randomSecond;
// // On ajoute des instants
// for (int i = 0; i < instants.length; i++) {
// // On ajoute aléatoirement des instants
// randomHour = random.nextInt(24);
// randomMinute = random.nextInt(60);
// randomSecond = random.nextInt(60);
// instants[i] = new Instant(randomHour, randomMinute, randomSecond);
// }
// // On affiche les instants
// for (Instant instant : instants) {
// System.out.printf("Instant : %s \n", instant);
// }
// // On fait toutes les combinaisons possibles
// for (int i = 0; i < instants.length; i++) {
// for (int j = 0; j < instants.length; j++) {
// // On affiche le résultat de la comparaison
// System.out.printf("Comparaison de %s et %s : %d%n", instants[i], instants[j],
// instants[i].compareTo(instants[j]));
// }
// }
// ------------------------- Question 6 -------------------------
// List<Instant> instants = new ArrayList<>();
// Random random = new Random();
// int randomHour;
// int randomMinute;
// int randomSecond;
// int n = 4;
// // On ajoute des instants
// for (int i = 0; i < n; i++) {
// // On ajoute aléatoirement des instants
// randomHour = random.nextInt(24);
// randomMinute = random.nextInt(60);
// randomSecond = random.nextInt(60);
// instants.add(new Instant(randomHour, randomMinute, randomSecond));
// }
// // On affiche les instants
// for (Instant instant : instants) {
// System.out.printf("Instant : %s \n", instant);
// }
// // On retourne le plus petit instant strictement supérieur à un instant t donné en paramètre
// Instant t = new Instant(12, 30, 0);
// Instant result = Instant.getSmallestInstant(t, instants);
// System.out.printf("Le plus petit instant strictement supérieur à %s est %s", t, result);
// ------------------------- Question 7 -------------------------
List<Instant> instants = new ArrayList<>();
Random random = new Random();
......@@ -75,14 +148,16 @@ class Main {
int randomMinute;
int randomSecond;
int n = 4;
// On ajoute des instants
for (int i = 0; i < instants.length; i++) {
for (int i = 0; i < n; i++) {
// On ajoute aléatoirement des instants
randomHour = random.nextInt(24);
randomMinute = random.nextInt(60);
randomSecond = random.nextInt(60);
instants[i] = new Instant(randomHour, randomMinute, randomSecond);
instants.add(new Instant(randomHour, randomMinute, randomSecond));
}
// On affiche les instants
......@@ -92,15 +167,19 @@ class Main {
}
// On fait toutes les combinaisons possibles
for (int i = 0; i < instants.length; i++) {
for (int j = 0; j < instants.length; j++) {
// On affiche le résultat de la comparaison
System.out.printf("Comparaison de %s et %s : %d\n", instants[i], instants[j],
instants[i].compareTo(instants[j]));
}
}
System.out.println("--------------");
// On trie les instants
Instant.sort(instants);
// On affiche les instants
for (Instant instant : instants) {
System.out.printf("Instant : %s \n", instant);
}
}
......
# TD2
## Exercice 1
### Question 1
<!-- Une voiture de type A est constituée d’un moteur, d’une carrosserie et de quatre roues. La carrosserie est constituée de portes, dont le nombre peut varier entre 2 et 4.
Proposez un diagramme de classes pour les voitures A.
-->
```dot
digraph {
rankdir=LR;
node [shape=rectangle];
edge [arrowhead=normal, arrowtail=diamond];
Voiture[label="Voiture A"]
Voiture -> Moteur[label="1"];
Voiture -> Carrosserie[label="1"];
Voiture -> Roue[label="4"];
Carrosserie -> Porte[label="2..4"];
}
```
### Question 2
<!-- Les voitures de type B sont constituées de deux moteurs (l’un à l’avant et l’autre à l’arrière), d’une carrosserie et de six roues. La carrosserie est constituée de portes, dont le nombre peut varier entre 2 et 4. -->
```dot
digraph {
rankdir=LR;
node [shape=rectangle];
edge [arrowhead=normal, arrowtail=diamond];
Voiture1[label="Voiture A"];
Voiture2[label="Voiture B"];
Voiture1 -> Moteur[label="1"];
Voiture1 -> Carrosserie[label="1"];
Voiture1 -> Roue[label="4"];
Voiture2 -> Moteur[label="2"];
Voiture2 -> Carrosserie[label="1"];
Voiture2 -> Roue[label="6"];
Carrosserie -> Porte[label="2..4"];
}
```
On fait un type abstrait `Voiture`.
```dot
digraph {
rankdir=LR;
node [shape=rectangle];
edge [arrowhead=normal, arrowtail=diamond];
Voiture[label="Voiture"];
Voiture -> Moteur[label="1"];
Voiture -> Carrosserie[label="1"];
Voiture -> Roue[label="4..6"];
Carrosserie -> Porte[label="2..4"];
}
```
public class Main {
public static void main(String[] args) {
}
}
......@@ -14,11 +14,11 @@ public class Main {
Mot mot2 = new Mot(entreeUser2);
System.out.println(mot.estPalindrome());
System.out.println(mot2.estPalindrome());
System.out.println(mot.estContenu(mot2));
System.out.println(mot.trieMot());
System.out.println(mot2.trieMot());
}
......
......@@ -28,7 +28,24 @@ class Mot {
}
public Boolean estPalindrome() {
return this.mot.equals(new StringBuilder(this.mot).reverse().toString());
Boolean isPalindrome = true;
int index = 0;
int length = this.mot.length();
while (isPalindrome && index < length / 2) {
if (this.mot.charAt(index) != this.mot.charAt(length - index - 1)) {
isPalindrome = false;
}
index++;
}
return isPalindrome;
}
public Boolean estContenu(Mot mot) {
......@@ -38,29 +55,25 @@ class Mot {
public String trieMot() {
// Trie par ordre alphabétique le mot
char[] motTrie = this.mot.toCharArray();
for (int i = 0; i < motTrie.length; i++) {
for (int j = i + 1; j < motTrie.length; j++) {
String sortie = "";
if (motTrie[i] > motTrie[j]) {
// On trie toute les lettres du mot
for (int i = 0; i < this.mot.length(); i++) {
char temp = motTrie[i];
motTrie[i] = motTrie[j];
motTrie[j] = temp;
char c = this.mot.charAt(i);
// On cherche la position de la lettre dans le mot
int index = 0;
while (index < sortie.length() && c > sortie.charAt(index)) {
index++;
}
}
// On insère la lettre à la bonne position
sortie = sortie.substring(0, index) + c + sortie.substring(index);
}
// Convertion du tableau de char en String
String motTrieString = new String(motTrie);
return motTrieString;
return sortie;
}
......
# TP 4
## Exercice 2
### Question 1
On peut ajouter des nombres dans un `ArrayList` de la manière suivante :
```java
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
```
On peut récupérer les valeurs entières d'un `ArrayList` de la manière suivante :
```java
int a = list.get(0);
int b = list.get(1);
int c = list.get(2);
```
package TP4.src;
class Liste {
private int[] liste;
private int nbElements;
public Liste() {
this.liste = new int[10];
this.nbElements = 0;
}
public Liste(int taille) {
this.liste = new int[taille];
this.nbElements = 0;
}
public Liste(Liste liste) {
this.liste = new int[liste.liste.length];
this.nbElements = liste.nbElements;
for (int i = 0; i < liste.nbElements; i++) {
this.liste[i] = liste.liste[i];
}
}
public Liste(int[] liste) {
this.liste = new int[liste.length];
this.nbElements = liste.length;
for (int i = 0; i < liste.length; i++) {
this.liste[i] = liste[i];
}
}
public void ajouter(int element) {
if (this.nbElements == this.liste.length) {
int[] listeTemp = new int[this.liste.length * 2];
for (int i = 0; i < this.liste.length; i++) {
listeTemp[i] = this.liste[i];
}
this.liste = listeTemp;
}
this.liste[this.nbElements] = element;
this.nbElements++;
}
public void ajouter(int index, int element) {
if (index < 0 || index > this.nbElements) {
System.out.println("Index invalide");
return;
}
if (this.nbElements == this.liste.length) {
int[] listeTemp = new int[this.liste.length * 2];
for (int i = 0; i < this.liste.length; i++) {
listeTemp[i] = this.liste[i];
}
this.liste = listeTemp;
}
for (int i = this.nbElements; i > index; i--) {
this.liste[i] = this.liste[i - 1];
}
this.liste[index] = element;
this.nbElements++;
}
public void supprimer(int index) {
if (index < 0 || index >= this.nbElements) {
System.out.println("Index invalide");
return;
}
for (int i = index; i < this.nbElements - 1; i++) {
this.liste[i] = this.liste[i + 1];
}
this.nbElements--;
}
public void afficher() {
for (int i = 0; i < this.nbElements; i++) {
System.out.print(this.liste[i] + " ");
}
System.out.println();
}
public int get(int index) {
if (index < 0 || index >= this.nbElements) {
System.out.println("Index invalide");
return -1;
}
return this.liste[index];
}
public void set(int index, int element) {
if (index < 0 || index >= this.nbElements) {
System.out.println("Index invalide");
return;
}
this.liste[index] = element;
}
public int size() {
return this.nbElements;
}
public boolean contient(int element) {
for (int i = 0; i < this.nbElements; i++) {
if (this.liste[i] == element) {
return true;
}
}
return false;
}
public void reverse() {
int[] listeTemp = new int[this.nbElements];
for (int i = 0; i < this.nbElements; i++) {
listeTemp[i] = this.liste[this.nbElements - i - 1];
}
this.liste = listeTemp;
}
}
\ No newline at end of file
package TP4.src;
public class Main {
public static void main(String[] args) {
// ------------------------ Exercice 1 ------------------------
// Matrice matrice1 = new Matrice(3, 3);
// Matrice matrice2 = new Matrice(3, 3);
// matrice1.remplirMatrice();
// matrice2.remplirMatrice();
// matrice1.afficheMatrice();
// System.out.println();
// matrice2.afficheMatrice();
// System.out.println();
// Matrice matriceResultat = matrice1.addition(matrice2);
// matriceResultat.afficheMatrice();
// System.out.println();
// matriceResultat = matrice1.multiplication(matrice2);
// matriceResultat.afficheMatrice();
// System.out.println();
// Matrice triangulaire = Matrice.matriceTriangulaire(3);
// triangulaire.afficheTriangulaire();
// ------------------------ Exercice 2 ------------------------
Liste liste = new Liste();
liste.ajouter(1);
liste.ajouter(2);
liste.ajouter(3);
liste.afficher();
liste.reverse();
liste.afficher();
}
}
package TP4.src;
public class Matrice {
private float[][] matrice;
private int nbLignes;
private int nbColonnes;
public Matrice(int nbLignes, int nbColonnes) {
this.nbLignes = nbLignes;
this.nbColonnes = nbColonnes;
this.matrice = new float[nbLignes][];
}
public Matrice(int taille) {
this.nbLignes = taille;
this.nbColonnes = taille;
this.matrice = new float[taille][];
}
public void afficheMatrice() {
for (int i = 0; i < this.nbLignes; i++) {
for (int j = 0; j < this.nbColonnes; j++) {
System.out.print(this.matrice[i][j] + "\t");
}
System.out.println();
}
}
public void remplirMatrice() {
for (int i = 0; i < this.nbLignes; i++) {
this.matrice[i] = new float[this.nbColonnes];
for (int j = 0; j < this.nbColonnes; j++) {
this.matrice[i][j] = (int) (Math.random() * 10);
}
}
}
public void remplirMatriceZero() {
for (int i = 0; i < this.nbLignes; i++) {
this.matrice[i] = new float[this.nbColonnes];
for (int j = 0; j < this.nbColonnes; j++) {
this.matrice[i][j] = 0;
}
}
}
Matrice addition(Matrice matrice) {
Matrice matriceResultat = new Matrice(this.nbLignes, this.nbColonnes);
matriceResultat.remplirMatriceZero();
for (int i = 0; i < this.nbLignes; i++) {
for (int j = 0; j < this.nbColonnes; j++) {
matriceResultat.matrice[i][j] = this.matrice[i][j] + matrice.matrice[i][j];
}
}
return matriceResultat;
}
Matrice multiplication(Matrice matrice) {
Matrice matriceResultat = new Matrice(this.nbLignes, this.nbColonnes);
matriceResultat.remplirMatriceZero();
for (int i = 0; i < this.nbLignes; i++) {
for (int j = 0; j < this.nbColonnes; j++) {
matriceResultat.matrice[i][j] = this.matrice[i][j] * matrice.matrice[i][j];
}
}
return matriceResultat;
}
Matrice multiplication(float alpha) {
Matrice matriceResultat = new Matrice(this.nbLignes, this.nbColonnes);
matriceResultat.remplirMatriceZero();
for (int i = 0; i < this.nbLignes; i++) {
for (int j = 0; j < this.nbColonnes; j++) {
matriceResultat.matrice[i][j] = this.matrice[i][j] * alpha;
}
}
return matriceResultat;
}
public static Matrice matriceTriangulaire(int taille) {
// On crée un tableau avec taille lignes et le nombre de colonnes est 1, 2, 3, ..., taille
Matrice matriceResultat = new Matrice(taille);
for (int i = 0; i < taille; i++) {
matriceResultat.matrice[i] = new float[i + 1];
for (int j = 0; j < i + 1; j++) {
matriceResultat.matrice[i][j] = 0;
}
}
return matriceResultat;
}
public void afficheTriangulaire() {
for (int i = 0; i < this.nbLignes; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print(this.matrice[i][j] + "\t");
}
System.out.println();
}
}
}
File deleted
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment