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

2nd course of OOP in class

parent 7ec121b5
No related branches found
No related tags found
No related merge requests found
File added
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
......@@ -69,6 +69,7 @@ class Instant {
this.getSecondes());
}
@Override
public String toString() {
return this.stringify();
}
......@@ -108,7 +109,6 @@ class Instant {
}
public int compareTo(Instant instant) {
int secondes1 = this.getHeures() * 3600 + this.getMinutes() * 60 + this.getSecondes();
......@@ -118,6 +118,7 @@ class Instant {
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
......@@ -138,8 +139,10 @@ class Instant {
}
@Override
public int hashCode() {
return this.stringify().hashCode();
return Integer.hashCode(this.getHeures() * 3600 + this.getMinutes() * 60 + this.getSecondes());
}
......
......@@ -60,8 +60,50 @@ class Instant2 {
this.getSecondes());
}
@Override
public String toString() {
return this.stringify();
}
public int compareTo(Instant2 instant) {
if (this.secondes < instant.secondes) {
return -1;
} else if (this.secondes > instant.secondes) {
return 1;
} else {
return 0;
}
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (obj == this) {
return true;
}
if (!(obj instanceof Instant2)) {
return false;
}
Instant2 instant = (Instant2) obj;
return this.compareTo(instant) == 0;
}
@Override
public int hashCode() { // On utilise le hashcode de la classe Integer
return Integer.hashCode(this.secondes);
}
}
\ No newline at end of file
......@@ -59,8 +59,49 @@ class Instant3 {
this.getSecondes());
}
public int compareTo(Instant3 instant) {
if (this.secondes < instant.secondes) {
return -1;
} else if (this.secondes > instant.secondes) {
return 1;
} else {
return 0;
}
}
@Override
public String toString() {
return this.stringify();
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (obj == this) {
return true;
}
if (!(obj instanceof Instant3)) {
return false;
}
Instant3 instant = (Instant3) obj;
return this.compareTo(instant) == 0;
}
@Override
public int hashCode() { // On utilise la table de hachage de Integer
return Integer.hashCode(this.secondes);
}
}
\ No newline at end of file
import java.util.Random;
/**
* main
......@@ -9,6 +10,7 @@ class Main {
*/
public static void main(String[] args) {
// ------------------------- Information utiles -----------------
// [ VOUS POUVEZ DÉCOMMENTER LES LIGNES CI-DESSOUS POUR TESTER VOTRE CODE AVEC `CTRL /` DANS VISUAL STUDIO CODE ]
......@@ -40,18 +42,19 @@ class Main {
// ------------------------- Question 4 -------------------------
// On compare deux instants
Instant i1 = new Instant(12, 30, 0);
Instant i2 = new Instant(10, 30, 0);
// Instant i1 = new Instant(12, 30, 0);
// Instant i2 = new Instant(10, 30, 0);
// On affiche le résultat de la comparaison
System.out.println(i1.compareTo(i2));
// // On affiche le résultat de la comparaison
// System.out.println(i1.compareTo(i2));
Instant i3 = new Instant(10, 30, 0);
Instant i4 = new Instant(10, 30, 0);
// Méthodes pour comparer :
System.out.println(i3.compareTo(i4) == 0); // true
System.out.println(i3.equals(i4)); // false (maintenant true car on a redéfini equals)
System.out.println(i3 == i4); // false
// Instant i3 = new Instant(10, 30, 0);
// Instant i4 = new Instant(10, 30, 0);
// // Méthodes pour comparer :
// System.out.println(i3.compareTo(i4) == 0); // true
// System.out.println(i3.equals(i4)); // false (maintenant true car on a redéfini equals)
// System.out.println(i3 == i4); // false
// System.out.println(i3.hashCode() == i4.hashCode()); // true
// On a bien true pour le premier car on compare terme à terme
// On a false pour les deux autres car on compare les références
......@@ -61,6 +64,43 @@ class Main {
// 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];
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]));
}
}
}
......
......@@ -255,7 +255,7 @@ class Instant3 {
</details>
## Exercice 2
## 1.3
On implémente la fonction `minus` qui permet de soustraire deux instants. On peut utiliser soit :
......@@ -322,3 +322,60 @@ Les deux méthodes :
La version avec `this` est préférable car on peut l'utiliser avec un `Instant` déjà existant.
## 1.4
On va créer la méthode `compareTo` :
```java
public int compareTo(Instant instant) {
int secondes1 = this.getHeures() * 3600 + this.getMinutes() * 60 + this.getSecondes();
int secondes2 = instant.getHeures() * 3600 + instant.getMinutes() * 60 + instant.getSecondes();
return Integer.compare(secondes1, secondes2); // Fonction déjà définie qui compare deux entiers
}
```
Elle permet de dire si un `Instant` est plus grand, plus petit ou égal à un autre `Instant`. On peut l'appeler avec `instant1.compareTo(instant2)`.
On peut s'amuser à définir aussi `equals` et `hashCode` :
<details>
<summary>Méthodes `equals` et `hashCode`</summary>
```java
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (obj == this) {
return true;
}
if (!(obj instanceof Instant)) {
return false;
}
Instant instant = (Instant) obj;
return this.compareTo(instant) == 0;
}
@Override
public int hashCode() {
return Integer.hashCode(this.getHeures() * 3600 + this.getMinutes() * 60 + this.getSecondes());
}
```
</details>
- Attention pour equals, si on a deux classes `A` et `A'`, si `A` et `A'` redéfinissent séparément leurs méthodes `equals` et `hashCode`, alors `A.equals(A')` peut retourner `true` mais `A.hashCode() != A'.hashCode()`. Une solution est d'ajouter `final` à la méthode `equals` pour empêcher la redéfinition dans les classes filles.
## 1.5
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment