From 2c530f2a58014e8e9479c41dabff9a639e9297a5 Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Mon, 16 Feb 2026 10:16:31 +0100 Subject: =?UTF-8?q?Cours=20du=2002=20au=2013=20f=C3=A9vrier?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- semestre 4/java/tme/tp3/Cabine.java | 22 +++++++++++++ semestre 4/java/tme/tp3/Personne.java | 52 +++++++++++++++++++++++++++++++ semestre 4/java/tme/tp3/Point.java | 38 ++++++++++++++++++++++ semestre 4/java/tme/tp3/Roue.java | 15 +++++++++ semestre 4/java/tme/tp3/TestTracteur.java | 15 +++++++++ semestre 4/java/tme/tp3/TestTriangle.java | 18 +++++++++++ semestre 4/java/tme/tp3/Tracteur.java | 27 ++++++++++++++++ semestre 4/java/tme/tp3/Triangle.java | 30 ++++++++++++++++++ 8 files changed, 217 insertions(+) create mode 100644 semestre 4/java/tme/tp3/Cabine.java create mode 100644 semestre 4/java/tme/tp3/Personne.java create mode 100644 semestre 4/java/tme/tp3/Point.java create mode 100644 semestre 4/java/tme/tp3/Roue.java create mode 100644 semestre 4/java/tme/tp3/TestTracteur.java create mode 100644 semestre 4/java/tme/tp3/TestTriangle.java create mode 100644 semestre 4/java/tme/tp3/Tracteur.java create mode 100644 semestre 4/java/tme/tp3/Triangle.java (limited to 'semestre 4/java/tme/tp3') 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); + } +} -- cgit v1.2.3