From 8395202955b052b86baad0f4f42b1720f7d967f5 Mon Sep 17 00:00:00 2001 From: Ethan Robert <ethan.rbrt90@gmail.com> Date: Fri, 14 Feb 2025 15:14:19 +0100 Subject: [PATCH] =?UTF-8?q?Ajout=20des=20TPs=20pr=C3=A9c=C3=A9dents?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TP01/Exercice1.java | 37 ++++++++++++++ TP01/Exercice2.java | 44 +++++++++++++++++ TP02/.DS_Store | Bin 0 -> 6148 bytes TP02/src/Competitor.java | 50 +++++++++++++++++++ TP02/src/Dice.java | 84 ++++++++++++++++++++++++++++++++ TP02/src/DicePlayer.java | 50 +++++++++++++++++++ TP02/src/NDicePlayerGame.java | 64 ++++++++++++++++++++++++ TP02/src/Program.java | 33 +++++++++++++ TP02/src/TwoDicePlayerGame.java | 45 +++++++++++++++++ TP02/src/UseCompetitor.java | 13 +++++ one | 0 second | 0 12 files changed, 420 insertions(+) create mode 100644 TP01/Exercice1.java create mode 100644 TP01/Exercice2.java create mode 100644 TP02/.DS_Store create mode 100644 TP02/src/Competitor.java create mode 100644 TP02/src/Dice.java create mode 100644 TP02/src/DicePlayer.java create mode 100644 TP02/src/NDicePlayerGame.java create mode 100644 TP02/src/Program.java create mode 100644 TP02/src/TwoDicePlayerGame.java create mode 100644 TP02/src/UseCompetitor.java delete mode 100644 one delete mode 100644 second diff --git a/TP01/Exercice1.java b/TP01/Exercice1.java new file mode 100644 index 0000000..7e7c1d9 --- /dev/null +++ b/TP01/Exercice1.java @@ -0,0 +1,37 @@ +public class Exercice1 { + + public static void main(String[] args) { + + final char SEP = ';'; + final char SUB = ' '; + String[] lignesCSV = new String[] { + "2-207-3040-3;Arthur;Dent;42", + "2-207-3040-3;Ford;Prefect;42", + "2-207-3040-3;Zaphod;Beeblebrox;42", + "2-207-3040-3;Tricia;McMillan;42" + }; + + for (int i = 0; i < lignesCSV.length; i++) { + + int deb = -1, fin = -1, idx = 0; + String maChaine = lignesCSV[i]; + + while (idx < maChaine.length() && maChaine.charAt(idx) != SEP) idx++; + deb = idx; + + idx = maChaine.length() - 1; + while (idx >= 0 && maChaine.charAt(idx) != SEP) idx -= 1; + fin = idx; + + String aTraiter = maChaine.substring(deb, fin); + String res = ""; + + for (idx = 0; idx < aTraiter.length(); idx++) { + if (aTraiter.charAt(idx) == SEP) res += SUB; + else res += aTraiter.charAt(idx); + } + System.out.println("Bonjour" + res); + } + + } +} \ No newline at end of file diff --git a/TP01/Exercice2.java b/TP01/Exercice2.java new file mode 100644 index 0000000..538d3fe --- /dev/null +++ b/TP01/Exercice2.java @@ -0,0 +1,44 @@ +import java.util.Random; + + +class NetworkIP { + int[] data = new int[4]; + + public NetworkIP(int a, int b, int c, int d) { + // Check if data is valid, then assign it + if (a >= 0 && a <= 255 && b >= 0 && b <= 255 && c >= 0 && c <= 255 && d >= 0 && d <= 255) { + data[0] = a; + data[1] = b; + data[2] = c; + data[3] = d; + } else { + System.out.println("Invalid IP address"); + } + } + + public String strDomaine() { + String result = ""; + for (int i = 0; i < 4; i++) { + result += data[i]; + if (i < 3) { + result += "."; + } + } + return result; + } +} + +class UseNetworkIP { + + public static void main(String[] args) { + NetworkIP[] addresses = new NetworkIP[5]; + + for (int i = 0; i < 5; i++) { + Random rand = new Random(); + addresses[i] = new NetworkIP(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)); + System.out.println(addresses[i].strDomaine()); + } + + } + +} \ No newline at end of file diff --git a/TP02/.DS_Store b/TP02/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..695b1deb96f04820fa50f62602de3c9e0d69f257 GIT binary patch literal 6148 zcmZQzU|@7AO)+F(5MW?n;9!8zEL;p&0Z1N%F(jFwB4Qx(KxP;QC+FuDKt)HX(GVC7 zfx#XEpuEe>ki?M5ke8E=k$<5wx%n<GNjdpRpu*zP-F)TXRmU6|7#QHPDR^WHGO(DM z!;s04gw;N<46=P+A0XKWl||TxDT~#<B8F6kVyyOoWpa`H_wYHAeNb71eVDRX?JH&| zVn|LZFD`(F+K!}xoXp}91A}XfOw25-Z0sED9NZkS!5R7G!6k_$rNvH(MbRK$NPd1! z5{#Xg6qcD<9xou`oS#>cn3<Pa1X2>5nUb26SQHbUnU|7Z?v!7emr@MY3zgvD<lu}K zkf^RUHL=uDFt#wM)lsOnG&0apFflf(t>xqpRo1r-iqFo;&CBnCBu_?02+hC?rD0Sz zIF`Y_K@Gi-O$Zs}(9>OSY?cg59bkRA`H0ZNltm4_4<LJ)8Nm9=f{XHU^7GOmbqfOv zLkTDj8A?#y0n&hwLw3oMC-8WL>qT}8HhEOnfXyDx1T@w;xQDYRNBuS$0;3^7RtSK~ z2L)*P>j0%2AT&sdfsug$+y!7{U|@k+zzFIGK-7S=g4BStf@qLd21XDIEDzSozzEgK z2=0b3Fff9<Bp@2Boq>@7tet_80j!+?R#h=Vv@<Y5ZDxe_P#8fy8j$%A?F@_%?J(z! z(xV|T8UoM|V202Fp!(mHfdN<lAEIiM91Vfd5Ezyrz{uhf?BWEfG;w$jRM&#)(*&qI zsP+d{$BdwQ8ln%R1T4!08Bmk}b0Bgct)OZjTop4iKx*>Qh5#&tM(NQIpnnJe02}(t Aj{pDw literal 0 HcmV?d00001 diff --git a/TP02/src/Competitor.java b/TP02/src/Competitor.java new file mode 100644 index 0000000..6b60d87 --- /dev/null +++ b/TP02/src/Competitor.java @@ -0,0 +1,50 @@ +public class Competitor { + private String numberSign; + private int score; + private int time; + + public Competitor (int number, int score, int min, int sec) { + if ((number > 0 && number <= 100) && (score >= 0 && score <= 50) && (sec >= 0 && sec <= 60) && (min >= 0 && min <= 60)) { + this.numberSign = "No" + number; + } + this.score = score; + this.time = min * 60 + sec; + } + + public String toString () { + StringBuilder result = new StringBuilder("("); + if (this.numberSign != null) { + result.append(this.numberSign); + } else { + result.append("-invalide-"); + } + result.append(", "); + result.append(this.score); + result.append(" points, "); + result.append(this.time); + result.append("s)"); + + return result.toString(); + } + + public boolean isValid() { + return this.numberSign != null; + } + + public boolean equals(Competitor other) { + if (this == other) { + return true; + } + if (other == null) { + return false; + } + return (this.numberSign.equals(other.numberSign) && this.score == other.score); + } + + public boolean isFaster(Competitor other) { + if (other == null) { + return true; + } + return (this.time < other.time); + } +} diff --git a/TP02/src/Dice.java b/TP02/src/Dice.java new file mode 100644 index 0000000..734cafe --- /dev/null +++ b/TP02/src/Dice.java @@ -0,0 +1,84 @@ +import java.util.Random; +import java.util.Scanner; + +public class Dice { + private int numberSides; + private Random rand; + private int value; + + public Dice (int sides) { + if (sides > 0) { + this.numberSides = sides; + } else { + this.numberSides = 1; + } + this.rand = new Random(); + this.roll(); + } + + public void roll() { + // Get the next value of the dice6 + this.value = this.rand.nextInt(this.numberSides) + 1; + } + + /* + + * GETTERS * + + */ + public int getValue() { + return this.value; + } + + public Random getRandom() { + return this.rand; + } + + public int getSides() { + return this.numberSides; + } + + public String toString() { + return "" + this.getValue(); + } +} + + +class Exercice2 { + + private static void UseDice() { + Dice dice6 = new Dice(6); + System.out.println("Valeur initiale du dé à 6 faces: " + dice6.getValue()); + for (int i = 0; i < 20; i++) { + dice6.roll(); + System.out.println("Lancer n°" + (i+1) + ", " + dice6.getValue()); + } + + Dice dice10 = new Dice(10); + System.out.println("\nValeur initiale du dé à 10 faces: " + dice10.getValue()); + for (int i = 0; i < 20; i++) { + dice10.roll(); + System.out.println("Lancer n°" + (i+1) + ", " + dice10.getValue()); + } + } + + private static void OneDicePlayerGame() { + + System.out.print("Merci d'entrer votre nom: "); + Scanner scanner = new Scanner(System.in); + String username = scanner.nextLine(); + scanner.close(); + + DicePlayer player = new DicePlayer(username); + Dice dice = new Dice(6); + + player.playUntil(dice, 20); + System.out.println("Vous avez effectué " + player.getDiceRolls() + " lancers, pour un total de " + player.getTotalValue() + " points. Bravo."); + } + + public static void main (String[] args) { + // Main function. + OneDicePlayerGame(); + } + +} diff --git a/TP02/src/DicePlayer.java b/TP02/src/DicePlayer.java new file mode 100644 index 0000000..07f8c27 --- /dev/null +++ b/TP02/src/DicePlayer.java @@ -0,0 +1,50 @@ +class DicePlayer { + private String name; + private int nbDiceRolls; + private int totalValue; + + public DicePlayer(String name) { + this.name = name; + this.nbDiceRolls = 0; + this.totalValue = 0; + } + + public String toString() { + return this.name + ": " + this.totalValue + " points en " + this.nbDiceRolls + " coups."; + } + + public void play (Dice aDice) { + aDice.roll(); + this.nbDiceRolls++; + this.totalValue += aDice.getValue(); + } + + public void playUntil (Dice aDice, int objective) { + do { + this.play(aDice); + } while (this.totalValue < objective); + } + + public boolean isWinner (DicePlayer other) { + if (this.nbDiceRolls < other.nbDiceRolls) { + return true; + } else if (this.nbDiceRolls == other.nbDiceRolls) { + return this.totalValue > this.totalValue; + } + return false; + } + + /* GETTERS */ + public String getName() { + return this.name; + } + + public int getDiceRolls() { + return this.nbDiceRolls; + } + + public int getTotalValue() { + return this.totalValue; + } + +} diff --git a/TP02/src/NDicePlayerGame.java b/TP02/src/NDicePlayerGame.java new file mode 100644 index 0000000..7af17c4 --- /dev/null +++ b/TP02/src/NDicePlayerGame.java @@ -0,0 +1,64 @@ +public class NDicePlayerGame { + + private final DicePlayer[] players; + + public NDicePlayerGame (int nPlayers) { + this.players = new DicePlayer[nPlayers]; + + for (int i = 0; i < nPlayers; i++) { + this.players[i] = new DicePlayer("" + (i+1)); + } + } + + public void play (Dice aDice, int objective) { + for (int i = 0; i < this.players.length; i++) { + this.players[i].playUntil(aDice, objective); + } + } + + public DicePlayer[] getWinners () { + + DicePlayer[] winners = new DicePlayer[this.players.length]; + int winnersIndex = 0; + + int minValue = this.players[0].getDiceRolls(); + + for (int i = 1; i < this.players.length; i++) { + if (this.players[i].getDiceRolls() < minValue) { + minValue = this.players[i].getDiceRolls(); + } + } + + // Get every player that made (minValue) rolls + for (int i = 0; i < this.players.length; i++) { + if (this.players[i].getDiceRolls() == minValue) { + winners[winnersIndex] = this.players[i]; + winnersIndex++; + } + } + + return winners; + + } + + public static void main(String[] args) { + + int nPlayers = Integer.parseInt(args[0]); + NDicePlayerGame game = new NDicePlayerGame(nPlayers); + + Dice dice = new Dice(6); + + game.play(dice, 20); + + DicePlayer[] gameWinners = game.getWinners(); + + System.out.println("Les vainqueurs:"); + for (int i = 0; i < gameWinners.length; i++) { + if (gameWinners[i] != null) { + System.out.println(gameWinners[i]); + } + } + + } + +} diff --git a/TP02/src/Program.java b/TP02/src/Program.java new file mode 100644 index 0000000..c40158d --- /dev/null +++ b/TP02/src/Program.java @@ -0,0 +1,33 @@ +public class Program { + public static void main (String[] args) { + + Competitor[] registeredCompetitors = new Competitor[100]; + + registeredCompetitors[0] = new Competitor(1, 45, 15, 20); + registeredCompetitors[1] = new Competitor(2, 32, 12, 45); + registeredCompetitors[2] = new Competitor(5, 12, 13, 59); + registeredCompetitors[3] = new Competitor(12, 12, 15, 70); + registeredCompetitors[4] = new Competitor(32, 75, 15, 20); + + for (int i = 0; i < registeredCompetitors.length; i++) { + if (registeredCompetitors[i] != null) { + if (registeredCompetitors[i].isValid()) { + System.out.println(registeredCompetitors[i]); + } + } + } + + System.out.println("Compétiteurs plus rapides :"); + + Competitor testCompetitor = new Competitor(7, 42, 13, 50); + + for (int i = 0; i < registeredCompetitors.length; i++) { + if (registeredCompetitors[i] != null) { + if (registeredCompetitors[i].isFaster(testCompetitor)) { + System.out.println(registeredCompetitors[i]); + } + } + } + + } +} \ No newline at end of file diff --git a/TP02/src/TwoDicePlayerGame.java b/TP02/src/TwoDicePlayerGame.java new file mode 100644 index 0000000..52c1f08 --- /dev/null +++ b/TP02/src/TwoDicePlayerGame.java @@ -0,0 +1,45 @@ +import java.util.Scanner; + +public class TwoDicePlayerGame { + + private DicePlayer player1; + private DicePlayer player2; + + public TwoDicePlayerGame(DicePlayer player1, DicePlayer player2) { + this.player1 = player1; + this.player2 = player2; + } + + public void play (Dice aDice, int objective) { + player1.playUntil(aDice, objective); + player2.playUntil(aDice, objective); + } + + public DicePlayer getWinner() { + if (this.player1.isWinner(this.player2)) { + return this.player1; + } + return this.player2; + } + + public static void main (String[] args) { + + Scanner scanner = new Scanner(System.in); + + System.out.print("Joueur 1, entrez votre nom: "); + String player1Name = scanner.nextLine(); + + System.out.print("Joueur 2, entrez votre nom: "); + String player2Name = scanner.nextLine(); + + scanner.close(); + + TwoDicePlayerGame game = new TwoDicePlayerGame(new DicePlayer(player1Name), new DicePlayer(player2Name)); + Dice dice = new Dice(6); + game.play(dice, 20); + + System.out.println("Félicitations à " + game.getWinner().getName() + " qui a gagné!"); + + } + +} diff --git a/TP02/src/UseCompetitor.java b/TP02/src/UseCompetitor.java new file mode 100644 index 0000000..c812b06 --- /dev/null +++ b/TP02/src/UseCompetitor.java @@ -0,0 +1,13 @@ +public class UseCompetitor { + public static void main(String[] args) { + Competitor alice = new Competitor(1, 45, 15, 20); + Competitor bruno = new Competitor(1, 45, 15, 20); + Competitor clement = new Competitor(2, 32, 12, 45); + Competitor dora = new Competitor(2, 34, 12, 45); + System.out.println("Alice:" + alice + " Bruno:" + bruno + "->" + alice.equals(bruno)); + System.out.println("Alice:" + alice + " null:" + null + "->" + alice.equals(null)); + System.out.println("Alice:" + alice + " Alice:" + alice + "->" + alice.equals(alice)); + System.out.println("Alice:" + alice + " Clement:" + clement + "->" + alice.equals(clement)); + System.out.println("Clement:" + clement + " Dora:" + dora + "->" + clement.equals(dora)); + } +} \ No newline at end of file diff --git a/one b/one deleted file mode 100644 index e69de29..0000000 diff --git a/second b/second deleted file mode 100644 index e69de29..0000000 -- GitLab