aboutsummaryrefslogtreecommitdiff
path: root/semestre 4/java/tme/tp3
diff options
context:
space:
mode:
authorAnhgelus Morhtuuzh <william@herges.fr>2026-02-16 10:16:31 +0100
committerAnhgelus Morhtuuzh <william@herges.fr>2026-02-16 10:16:31 +0100
commit2c530f2a58014e8e9479c41dabff9a639e9297a5 (patch)
treefe6f6890d983ff436d80e4194abf5545658f51d6 /semestre 4/java/tme/tp3
parentba6692d9b508b448eafd7bc71faa4ae3f3ac7199 (diff)
Cours du 02 au 13 févrierHEADmain
Diffstat (limited to 'semestre 4/java/tme/tp3')
-rw-r--r--semestre 4/java/tme/tp3/Cabine.java22
-rw-r--r--semestre 4/java/tme/tp3/Personne.java52
-rw-r--r--semestre 4/java/tme/tp3/Point.java38
-rw-r--r--semestre 4/java/tme/tp3/Roue.java15
-rw-r--r--semestre 4/java/tme/tp3/TestTracteur.java15
-rw-r--r--semestre 4/java/tme/tp3/TestTriangle.java18
-rw-r--r--semestre 4/java/tme/tp3/Tracteur.java27
-rw-r--r--semestre 4/java/tme/tp3/Triangle.java30
8 files changed, 217 insertions, 0 deletions
diff --git a/semestre 4/java/tme/tp3/Cabine.java b/semestre 4/java/tme/tp3/Cabine.java
new file mode 100644
index 0000000..2b65abe
--- /dev/null
+++ b/semestre 4/java/tme/tp3/Cabine.java
@@ -0,0 +1,22 @@
+public class Cabine {
+ private int volume;
+ private String couleur;
+
+ public Cabine(int vol, String col) {
+ volume = vol;
+ couleur = col;
+ }
+
+ public Cabine(Cabine cb) {
+ volume = cb.volume;
+ couleur = cb.couleur;
+ }
+
+ public String toString() {
+ return String.format("Cabine{%d, %s}", volume, couleur);
+ }
+
+ public void setCouleur(String col) {
+ couleur = col;
+ }
+}
diff --git a/semestre 4/java/tme/tp3/Personne.java b/semestre 4/java/tme/tp3/Personne.java
new file mode 100644
index 0000000..664bf60
--- /dev/null
+++ b/semestre 4/java/tme/tp3/Personne.java
@@ -0,0 +1,52 @@
+public class Personne {
+ public final String name;
+ private Personne conjoint;
+
+ public Personne(String n) {
+ name = n;
+ }
+
+ public Personne() {
+ this("Pers" + (char) ((int) (Math.random() * 26) + 'A'));
+ }
+
+ public String toString() {
+ return String.format("%s, %s", name, conjoint == null ? "célibataire" : "marié(e)");
+ }
+
+ public void epouser(Personne p) {
+ if (p.conjoint != null || conjoint != null || p == this) {
+ System.out.println(String.format(
+ "Le mariage de %s avec %s est impossible", this, p
+ ));
+ return;
+ }
+ System.out.println(String.format(
+ "%s se marie avec %s", this, p
+ ));
+ conjoint = p;
+ p.conjoint = this;
+ }
+
+ public void divorcer() {
+ if (conjoint == null) return;
+ System.out.println(String.format(
+ "%s divorce avec %s", this, conjoint
+ ));
+ conjoint.conjoint = null;
+ conjoint = null;
+ }
+
+ public static void main(String[] args) {
+ final var p1 = new Personne();
+ final var p2 = new Personne();
+ final var p3 = new Personne();
+
+ p1.epouser(p2);
+ p1.epouser(p3);
+ p3.epouser(p1);
+ p3.epouser(p3);
+ p1.divorcer();
+ p3.divorcer();
+ }
+}
diff --git a/semestre 4/java/tme/tp3/Point.java b/semestre 4/java/tme/tp3/Point.java
new file mode 100644
index 0000000..1fdf9fa
--- /dev/null
+++ b/semestre 4/java/tme/tp3/Point.java
@@ -0,0 +1,38 @@
+public class Point {
+ private int posX, posY;
+
+ public Point(int x, int y) {
+ posX = x;
+ posY = y;
+ }
+
+ public Point() {
+ posX = (int) (Math.random()*10);
+ posY = (int) (Math.random()*10);
+ }
+
+ public Point(Point p) {
+ posX = p.posX;
+ posY = p.posY;
+ }
+
+ public String toString() {
+ return String.format("(%d,%d)", posX, posY);
+ }
+
+ public double distance(Point p) {
+ return Math.sqrt(
+ (p.posX - posX)*(p.posX - posX) + (p.posY - posY)*(p.posY - posY)
+ );
+ }
+
+ public void deplaceToi(int x, int y) {
+ posX = x;
+ posY = y;
+ }
+
+ public boolean equals(Object o) {
+ if (!(o instanceof final Point p)) return false;
+ return posX == p.posX && posY == p.posY;
+ }
+}
diff --git a/semestre 4/java/tme/tp3/Roue.java b/semestre 4/java/tme/tp3/Roue.java
new file mode 100644
index 0000000..bcae13e
--- /dev/null
+++ b/semestre 4/java/tme/tp3/Roue.java
@@ -0,0 +1,15 @@
+public class Roue {
+ private int diametre;
+
+ public Roue(int d) {
+ diametre = d;
+ }
+
+ public Roue() {
+ diametre = 60;
+ }
+
+ public String toString() {
+ return String.format("Roue{%d}", diametre);
+ }
+}
diff --git a/semestre 4/java/tme/tp3/TestTracteur.java b/semestre 4/java/tme/tp3/TestTracteur.java
new file mode 100644
index 0000000..6dd4c75
--- /dev/null
+++ b/semestre 4/java/tme/tp3/TestTracteur.java
@@ -0,0 +1,15 @@
+public class TestTracteur {
+ public static void main(String[] args) {
+ final var r1 = new Roue();
+ final var r2 = new Roue();
+ final var gr1 = new Roue(120);
+ final var gr2 = new Roue(120);
+ final var cb = new Cabine(3, "bleue");
+
+ final var t1 = new Tracteur(cb, r1, r2, gr1, gr2);
+ System.out.println(t1);
+ final var t2 = new Tracteur(t1);
+ t2.peindre("rouge");
+ System.out.println(t2);
+ }
+}
diff --git a/semestre 4/java/tme/tp3/TestTriangle.java b/semestre 4/java/tme/tp3/TestTriangle.java
new file mode 100644
index 0000000..61aacaa
--- /dev/null
+++ b/semestre 4/java/tme/tp3/TestTriangle.java
@@ -0,0 +1,18 @@
+public class TestTriangle {
+ public static void main(String[] args) {
+ final var p1 = new Point();
+ final var p2 = new Point();
+ System.out.println(p1);
+ System.out.println(p2);
+
+ final var p3 = new Point();
+ final var t = new Triangle(p1, p2, p3);
+ System.out.println(t);
+ System.out.println(t.getPerimetre());
+
+ final var t2 = new Triangle(t);
+ p1.deplaceToi(0,0);
+ System.out.println(t);
+ System.out.println(t2);
+ }
+}
diff --git a/semestre 4/java/tme/tp3/Tracteur.java b/semestre 4/java/tme/tp3/Tracteur.java
new file mode 100644
index 0000000..07d152d
--- /dev/null
+++ b/semestre 4/java/tme/tp3/Tracteur.java
@@ -0,0 +1,27 @@
+public class Tracteur {
+ private Cabine cabine;
+ private Roue r1;
+ private Roue r2;
+ private Roue gr1;
+ private Roue gr2;
+
+ public Tracteur(Cabine cabine, Roue r1, Roue r2, Roue gr1, Roue gr2) {
+ this.cabine = cabine;
+ this.r1 = r1;
+ this.r2 = r2;
+ this.gr1 = gr1;
+ this.gr2 = gr2;
+ }
+
+ public Tracteur(Tracteur t) {
+ this(new Cabine(t.cabine), t.r1, t.r2, t.gr1, t.gr2);
+ }
+
+ public void peindre(String col) {
+ cabine.setCouleur(col);
+ }
+
+ public String toString() {
+ return String.format("Tracteur{%s, %s, %s, %s, %s}", cabine, r1, r2, gr1, gr2);
+ }
+}
diff --git a/semestre 4/java/tme/tp3/Triangle.java b/semestre 4/java/tme/tp3/Triangle.java
new file mode 100644
index 0000000..d54835c
--- /dev/null
+++ b/semestre 4/java/tme/tp3/Triangle.java
@@ -0,0 +1,30 @@
+public class Triangle {
+ private Point p1, p2, p3;
+
+ public Triangle(Point p1, Point p2, Point p3) {
+ this.p1 = p1;
+ this.p2 = p2;
+ this.p3 = p3;
+ }
+
+ public Triangle() {
+ this(new Point(), new Point(), new Point());
+ }
+
+ public Triangle(Triangle t) {
+ this(new Point(t.p1), new Point(t.p2), new Point(t.p3));
+ }
+
+ public String toString() {
+ return String.format("{%s,%s,%s}", p1, p2, p3);
+ }
+
+ public double getPerimetre() {
+ return p1.distance(p2) + p2.distance(p3) + p3.distance(p1);
+ }
+
+ public boolean equals(Object o) {
+ if (!(o instanceof Triangle t)) return false;
+ return p1.equals(t.p1) && p2.equals(t.p2) && p3.equals(t.p3);
+ }
+}