diff options
Diffstat (limited to 'semestre 4/java/tme/tp2')
| -rw-r--r-- | semestre 4/java/tme/tp2/AdresseWeb.java | 23 | ||||
| -rw-r--r-- | semestre 4/java/tme/tp2/Complexe.java | 29 | ||||
| -rw-r--r-- | semestre 4/java/tme/tp2/Coureur.java | 53 | ||||
| -rw-r--r-- | semestre 4/java/tme/tp2/TestAdresseWeb.java | 7 | ||||
| -rw-r--r-- | semestre 4/java/tme/tp2/TestComplexe.java | 12 | ||||
| -rw-r--r-- | semestre 4/java/tme/tp2/TestCoureur.java | 16 |
6 files changed, 140 insertions, 0 deletions
diff --git a/semestre 4/java/tme/tp2/AdresseWeb.java b/semestre 4/java/tme/tp2/AdresseWeb.java new file mode 100644 index 0000000..409b61c --- /dev/null +++ b/semestre 4/java/tme/tp2/AdresseWeb.java @@ -0,0 +1,23 @@ +public class AdresseWeb { + private final String protocole; + private final String domaine; + private final String chemin; + + public AdresseWeb(String proto, String dom, String path) { + protocole = proto; + domaine = dom; + chemin = path; + } + + public AdresseWeb(String dom, String path) { + this("http", dom, path); + } + + public AdresseWeb(String dom) { + this("http", dom, ""); + } + + public String toString() { + return protocole + "://www." + domaine + chemin; + } +} diff --git a/semestre 4/java/tme/tp2/Complexe.java b/semestre 4/java/tme/tp2/Complexe.java new file mode 100644 index 0000000..afd6cee --- /dev/null +++ b/semestre 4/java/tme/tp2/Complexe.java @@ -0,0 +1,29 @@ +public final class Complexe { + public final double reelle; + public final double imag; + + public Complexe(double reelle, double imag) { + this.reelle = reelle; + this.imag = imag; + } + + public Complexe() { + this(Math.random() * 4 - 2, Math.random() * 4 - 2); + } + + public String toString() { + return String.format("%f + %fi", reelle, imag); + } + + public boolean estReel() { + return imag == 0; + } + + public Complexe addition(Complexe c) { + return new Complexe(reelle + c.reelle, imag + c.imag); + } + + public Complexe multiplication(Complexe c) { + return new Complexe(reelle * c.reelle - imag * c.imag, reelle * c.reelle + imag * c.imag); + } +} diff --git a/semestre 4/java/tme/tp2/Coureur.java b/semestre 4/java/tme/tp2/Coureur.java new file mode 100644 index 0000000..1e8ed99 --- /dev/null +++ b/semestre 4/java/tme/tp2/Coureur.java @@ -0,0 +1,53 @@ +public final class Coureur { + private int numDossard; + private double tempsAu100; + private boolean possedeTemoin; + + public Coureur(int num) { + numDossard = num; + possedeTemoin = false; + tempsAu100 = Math.random() * (16-12) + 12; + } + + public Coureur() { + this((int) (Math.random() * 999 + 1)); + } + + public int getNumDossard() { + return numDossard; + } + + public double getTempsAu100() { + return tempsAu100; + } + + public boolean getPossedeTemoin() { + return possedeTemoin; + } + + public void setNumDossard(int num) { + numDossard = num; + } + + public void setTempsAu100(double temps) { + tempsAu100 = temps; + } + + public void setPossedeTemoin(boolean has) { + possedeTemoin = has; + } + + public String toString() { + return String.format("Coureur %d tempsAu100 : %.1f au 100m possedeTemoin %s", numDossard, tempsAu100, possedeTemoin ? "oui" : "non"); + } + + public void passeTemoin(Coureur c) { + System.out.println("moi, coureur "+numDossard+", je passe le témoin au coureur "+c.numDossard); + c.possedeTemoin = true; + possedeTemoin = false; + } + + public void courir() { + System.out.println("e suis le coureur "+numDossard+" et je cours"); + } +} diff --git a/semestre 4/java/tme/tp2/TestAdresseWeb.java b/semestre 4/java/tme/tp2/TestAdresseWeb.java new file mode 100644 index 0000000..8bb3833 --- /dev/null +++ b/semestre 4/java/tme/tp2/TestAdresseWeb.java @@ -0,0 +1,7 @@ +public class TestAdresseWeb { + public static void main(String[] args) { + System.out.println(new AdresseWeb("hey")); + System.out.println(new AdresseWeb("hey", "/")); + System.out.println(new AdresseWeb("https", "hey", "/")); + } +} diff --git a/semestre 4/java/tme/tp2/TestComplexe.java b/semestre 4/java/tme/tp2/TestComplexe.java new file mode 100644 index 0000000..1be9c8e --- /dev/null +++ b/semestre 4/java/tme/tp2/TestComplexe.java @@ -0,0 +1,12 @@ +public final class TestComplexe { + public static void main(String[] args) { + final Complexe[] cs = {new Complexe(), new Complexe(), new Complexe()}; + for (var c : cs) System.out.println("real " + c.estReel()); + for (var c1 : cs) { + for (var c2 : cs) { + System.out.println("(" + c1 + ") + (" + c2 + ") = " + c1.addition(c2)); + System.out.println("(" + c1 + ") * (" + c2 + ") = " + c1.multiplication(c2)); + } + } + } +} diff --git a/semestre 4/java/tme/tp2/TestCoureur.java b/semestre 4/java/tme/tp2/TestCoureur.java new file mode 100644 index 0000000..284fc21 --- /dev/null +++ b/semestre 4/java/tme/tp2/TestCoureur.java @@ -0,0 +1,16 @@ +public class TestCoureur { + public static void main(String[] args) { + final var c1 = new Coureur(); + final var c2 = new Coureur(); + final var c3 = new Coureur(); + final var c4 = new Coureur(); + c1.courir(); + c1.passeTemoin(c2); + c2.courir(); + c2.passeTemoin(c3); + c3.courir(); + c3.passeTemoin(c4); + c4.courir(); + System.out.println("temps : " + (c1.getTempsAu100() + c2.getTempsAu100() + c3.getTempsAu100() + c4.getTempsAu100())); + } +} |
