0% ont trouvé ce document utile (0 vote)
69 vues22 pages

TD de Java Corriger

Télécharger au format pdf ou txt
Télécharger au format pdf ou txt
Télécharger au format pdf ou txt
Vous êtes sur la page 1/ 22

TD DE JAVA

EXERCICE 1

// Classe Robot

public class Robot {

// Attributs

private int orientation; // 1 = Nord, 2 = Est, 3 = Sud, 4 = Ouest

private int abs;

private int ord;

private static int count = 0;

private int num;

// Constructeur

public Robot(int orientation, int abs, int ord) {

this.orientation = orientation;

this.abs = abs;

this.ord = ord;

count++;

num = count;

// Méthode Avancer

public void Avancer() {

switch (orientation) {

case 1:

ord++;

break;

case 2:

abs++;

break;
case 3:

ord--;

break;

case 4:

abs--;

break;

// Méthode Tourner

public void Tourner() {

orientation = (orientation % 4) + 1;

// Méthode Affiche

public void Affiche() {

System.out.println("Robot " + num + " - Orientation : " + orientation + " - Position : (" + abs + ", " +
ord + ")");

// Méthode pour accéder à l'attribut statique count

public static int getCount() {

return count;

// Classe de test Test_Robot

public class Test_Robot {

public static void main(String[] args) {


// Création de plusieurs objets robots

Robot robot1 = new Robot(1, 0, 0);

Robot robot2 = new Robot(2, 0, 0);

Robot robot3 = new Robot(3, 0, 0);

// Manipulation des robots

robot1.Avancer();

robot2.Tourner();

robot3.Avancer();

robot3.Tourner();

// Affichage des robots

robot1.Affiche();

robot2.Affiche();

robot3.Affiche();

// Affichage du nombre total des robots créés

System.out.println("Nombre total des robots créés : " + Robot.getCount());

EXERCICE 2

public class Fonction_Math {

// Méthode pour calculer le factoriel d'un entier

public static long Factoriel(int n) {

if (n == 0 || n == 1) {

return 1;

} else {

return n * Factoriel(n - 1);

}
}

// Méthode pour calculer la somme des n premiers entiers

public static int Somme(int n) {

int somme = 0;

for (int i = 1; i <= n; i++) {

somme += i;

return somme;

// Méthode pour calculer la racine carrée d'un nombre

public static double RacineCarre(double x) {

return Math.sqrt(x);

EXERCICE 3

Voici le code Java pour les classes Etudiant et GestionNotesIME :

java
import java.util.ArrayList;

import java.util.List;

public class Etudiant {

// Attributs

private String nom;

private List<Double> notes;

// Constructeur
public Etudiant(String nom) {

this.nom = nom;

this.notes = new ArrayList<>();

// Méthode pour ajouter une note à l'étudiant

public void ajouterNote(double note) {

notes.add(note);

// Méthode pour calculer la moyenne des notes de l'étudiant

public double calculerMoyenne() {

if (notes.isEmpty()) {

return 0;

double somme = 0;

for (double note : notes) {

somme += note;

return somme / notes.size();

// Méthode pour afficher les informations de l'étudiant

public void afficherInfos() {

System.out.println("Nom de l'étudiant : " + nom);

System.out.println("Liste des notes : " + notes);

System.out.println("Moyenne : " + calculerMoyenne());

}
public class GestionNotesIME {

public static void main(String[] args) {

// Création des objets Etudiant

Etudiant etudiant1 = new Etudiant("Jean");

Etudiant etudiant2 = new Etudiant("Marie");

// Ajout de notes pour chaque étudiant

etudiant1.ajouterNote(12.5);

etudiant1.ajouterNote(15.75);

etudiant1.ajouterNote(10.0);

etudiant2.ajouterNote(14.0);

etudiant2.ajouterNote(16.25);

etudiant2.ajouterNote(13.5);

// Affichage des informations de chaque étudiant

System.out.println("Informations pour l'étudiant 1 :");

etudiant1.afficherInfos();

System.out.println("\nInformations pour l'étudiant 2 :");

etudiant2.afficherInfos();

EXERCICE 4

Voici le code Java pour implémenter cet exercice en utilisant la classe Scanner :

import java.util.ArrayList;

import java.util.List;
import java.util.Scanner;

public class Etudiant {

// Attributs

private String nom;

private List<Matiere> matieres;

// Constructeur

public Etudiant(String nom) {

this.nom = nom;

this.matieres = new ArrayList<>();

// Méthode pour ajouter une matière avec sa note et son coefficient

public void ajouterMatiere(String nom, double note, int coefficient) {

matieres.add(new Matiere(nom, note, coefficient));

// Méthode pour calculer la moyenne pondérée des matières

public double calculerMoyenne() {

double sommeNotes = 0;

double sommeCoefficients = 0;

for (Matiere matiere : matieres) {

sommeNotes += matiere.getNote() * matiere.getCoefficient();

sommeCoefficients += matiere.getCoefficient();

return sommeNotes / sommeCoefficients;

}
// Méthode pour afficher les informations de l'étudiant

public void afficherInfos() {

System.out.println("Nom de l'étudiant : " + nom);

System.out.println("Liste des matières :");

for (Matiere matiere : matieres) {

System.out.println(" - " + matiere.getNom() + " : Note = " + matiere.getNote() + ", Coefficient = " +
matiere.getCoefficient());

System.out.println("Moyenne : " + calculerMoyenne());

// Classe interne pour représenter une matière avec sa note et son coefficient

private class Matiere {

private String nom;

private double note;

private int coefficient;

public Matiere(String nom, double note, int coefficient) {

this.nom = nom;

this.note = note;

this.coefficient = coefficient;

public String getNom() {

return nom;

public double getNote() {

return note;
}

public int getCoefficient() {

return coefficient;

// Méthode statique pour créer un étudiant en demandant des entrées à l'utilisateur

public static Etudiant creerEtudiant(Scanner scanner) {

System.out.print("Entrez le nom de l'étudiant : ");

String nom = scanner.next();

Etudiant etudiant = new Etudiant(nom);

System.out.print("Entrez le nombre de matières pour cet étudiant : ");

int nombreMatieres = scanner.nextInt();

for (int i = 0; i < nombreMatieres; i++) {

System.out.println("Matière " + (i + 1) + " :");

System.out.print("Nom de la matière : ");

String nomMatiere = scanner.next();

System.out.print("Note de la matière : ");

double noteMatiere = scanner.nextDouble();

System.out.print("Coefficient de la matière : ");

int coefficientMatiere = scanner.nextInt();

etudiant.ajouterMatiere(nomMatiere, noteMatiere, coefficientMatiere);

return etudiant;

}
public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Entrez le nombre d'étudiants : ");

int nombreEtudiants = scanner.nextInt();

List<Etudiant> listeEtudiants = new ArrayList<>();

for (int i = 0; i < nombreEtudiants; i++) {

System.out.println("\nÉtudiant " + (i + 1) + " :");

Etudiant etudiant = creerEtudiant(scanner);

listeEtudiants.add(etudiant);

System.out.println("\nInformations des étudiants :");

for (Etudiant etudiant : listeEtudiants) {

etudiant.afficherInfos();

System.out.println();

scanner.close();

EXERCICE 5

public class CompteBancaire {

// Attributs

private String numeroCompte;

private double solde;

private String titulaire;


// Constructeur

public CompteBancaire(String numeroCompte, double solde, String titulaire) {

this.numeroCompte = numeroCompte;

this.solde = solde;

this.titulaire = titulaire;

// Méthode pour effectuer un dépôt sur le compte

public void depot(double montant) {

solde += montant;

// Méthode pour effectuer un retrait du compte

public void retrait(double montant) {

if (solde >= montant) {

solde -= montant;

} else {

System.out.println("Solde insuffisant pour effectuer le retrait.");

// Méthode pour obtenir le solde actuel du compte

public double getSolde() {

return solde;

// Méthode pour afficher les informations du compte

public void afficherInfos() {

System.out.println("Numéro de compte : " + numeroCompte);


System.out.println("Solde : " + solde);

System.out.println("Titulaire : " + titulaire);

public static void main(String[] args) {

// Création d'un compte bancaire

CompteBancaire compte1 = new CompteBancaire("123456789", 1000.0, "Jean Dupont");

// Affichage des informations du compte

System.out.println("Informations du compte :");

compte1.afficherInfos();

// Dépôt sur le compte

compte1.depot(500.0);

System.out.println("\nAprès le dépôt de 500€ :");

compte1.afficherInfos();

// Retrait du compte

compte1.retrait(200.0);

System.out.println("\nAprès le retrait de 200€ :");

compte1.afficherInfos();

EXERCICE 6

Voici comment vous pouvez créer la classe principale GestionBancaire pour effectuer des
opérations bancaires sur un objet CompteBancaire :

public class GestionBancaire {

public static void main(String[] args) {

// Création d'un compte bancaire


CompteBancaire compte = new CompteBancaire("123456789", 1000.0, "Jean Dupont");

// Affichage des informations du compte avant les opérations

System.out.println("Informations du compte avant les opérations :");

compte.afficherInfos();

// Opérations bancaires (dépôts et retraits)

compte.depot(500.0);

compte.retrait(200.0);

// Affichage des informations du compte après les opérations

System.out.println("\nInformations du compte après les opérations :");

compte.afficherInfos();

EXERCICE 7

Voici la classe CompteBancaire améliorée avec la méthode virement :

public class CompteBancaire {

// Attributs

private String numeroCompte;

private double solde;

private String titulaire;

// Constructeur

public CompteBancaire(String numeroCompte, double solde, String titulaire) {

this.numeroCompte = numeroCompte;

this.solde = solde;

this.titulaire = titulaire;

}
// Méthode pour effectuer un dépôt sur le compte

public void depot(double montant) {

solde += montant;

// Méthode pour effectuer un retrait du compte

public void retrait(double montant) {

if (solde >= montant) {

solde -= montant;

} else {

System.out.println("Solde insuffisant pour effectuer le retrait.");

// Méthode pour effectuer un virement vers un autre compte

public void virement(CompteBancaire compteDestinataire, double montant) {

if (solde >= montant) {

retrait(montant);

compteDestinataire.depot(montant);

System.out.println("Virement de " + montant + " effectué avec succès vers le compte " +
compteDestinataire.numeroCompte);

} else {

System.out.println("Solde insuffisant pour effectuer le virement.");

// Méthode pour obtenir le solde actuel du compte

public double getSolde() {


return solde;

// Méthode pour afficher les informations du compte

public void afficherInfos() {

System.out.println("Numéro de compte : " + numeroCompte);

System.out.println("Solde : " + solde);

System.out.println("Titulaire : " + titulaire);

public static void main(String[] args) {

// Création de deux comptes bancaires

CompteBancaire compte1 = new CompteBancaire("123456789", 1000.0, "Jean Dupont");

CompteBancaire compte2 = new CompteBancaire("987654321", 2000.0, "Marie Durand");

// Affichage des informations des comptes avant les opérations

System.out.println("Informations du compte 1 avant les opérations :");

compte1.afficherInfos();

System.out.println("\nInformations du compte 2 avant les opérations :");

compte2.afficherInfos();

// Virement entre les comptes

compte1.virement(compte2, 300.0);

// Affichage des informations des comptes après les opérations

System.out.println("\nInformations du compte 1 après les opérations :");

compte1.afficherInfos();

System.out.println("\nInformations du compte 2 après les opérations :");

compte2.afficherInfos();
}

EXERCICE 8

Voici la classe GestionBancaire modifiée pour créer deux comptes supplémentaires et effectuer
des virements entre les comptes :

public class GestionBancaire {

public static void main(String[] args) {

// Création de quatre comptes bancaires

CompteBancaire compte1 = new CompteBancaire("123456789", 1000.0, "Jean Dupont");

CompteBancaire compte2 = new CompteBancaire("987654321", 2000.0, "Marie Durand");

CompteBancaire compte3 = new CompteBancaire("456789123", 1500.0, "Paul Martin");

CompteBancaire compte4 = new CompteBancaire("789123456", 3000.0, "Sophie Lefevre");

// Affichage des informations des comptes avant les opérations

System.out.println("Informations du compte 1 avant les opérations :");

compte1.afficherInfos();

System.out.println("\nInformations du compte 2 avant les opérations :");

compte2.afficherInfos();

System.out.println("\nInformations du compte 3 avant les opérations :");

compte3.afficherInfos();

System.out.println("\nInformations du compte 4 avant les opérations :");

compte4.afficherInfos();

// Virements entre les comptes

compte1.virement(compte2, 300.0);

compte3.virement(compte4, 500.0);

// Affichage des informations des comptes après les opérations

System.out.println("\nInformations du compte 1 après les opérations :");


compte1.afficherInfos();

System.out.println("\nInformations du compte 2 après les opérations :");

compte2.afficherInfos();

System.out.println("\nInformations du compte 3 après les opérations :");

compte3.afficherInfos();

System.out.println("\nInformations du compte 4 après les opérations :");

compte4.afficherInfos();

EXERCICE 9

Voici la classe Etudiant avec la méthode afficherInfos :

java

public class Etudiant {

// Attributs

private String nom;

private String prenom;

private String matricule;

private String filiere;

// Constructeur

public Etudiant(String nom, String prenom, String matricule, String filiere) {

this.nom = nom;

this.prenom = prenom;

this.matricule = matricule;

this.filiere = filiere;

// Méthode pour afficher les informations de l'étudiant


public void afficherInfos() {

System.out.println("Nom : " + nom);

System.out.println("Prénom : " + prenom);

System.out.println("Matricule : " + matricule);

System.out.println("Filière : " + filiere);

public static void main(String[] args) {

// Création d'un étudiant

Etudiant etudiant1 = new Etudiant("Dupont", "Jean", "123456", "Informatique");

// Affichage des informations de l'étudiant

etudiant1.afficherInfos();

EXERCICE 10

Voici comment vous pouvez modifier la classe GestionInscriptionsIME pour inclure un tableau
d'objets Etudiant et une méthode pour ajouter un nouvel étudiant :

public class GestionInscriptionsIME {

// Tableau pour stocker les étudiants inscrits

private Etudiant[] listeEtudiants;

private int nombreEtudiants;

// Constructeur

public GestionInscriptionsIME(int tailleMax) {

listeEtudiants = new Etudiant[tailleMax];

nombreEtudiants = 0;

}
// Méthode pour ajouter un nouvel étudiant au tableau

public void ajouterEtudiant(Etudiant etudiant) {

if (nombreEtudiants < listeEtudiants.length) {

listeEtudiants[nombreEtudiants] = etudiant;

nombreEtudiants++;

System.out.println("Étudiant ajouté avec succès.");

} else {

System.out.println("Impossible d'ajouter un nouvel étudiant : tableau plein.");

public static void main(String[] args) {

// Création d'un gestionnaire d'inscriptions avec un tableau de taille 10

GestionInscriptionsIME gestionnaire = new GestionInscriptionsIME(10);

// Création de quelques étudiants

Etudiant etudiant1 = new Etudiant("Dupont", "Jean", "123456", "Informatique");

Etudiant etudiant2 = new Etudiant("Durand", "Marie", "789012", "Mathématiques");

// Ajout des étudiants au gestionnaire d'inscriptions

gestionnaire.ajouterEtudiant(etudiant1);

gestionnaire.ajouterEtudiant(etudiant2);

EXERCICE 11
Voici la classe GestionInscriptionsIME mise à jour avec la méthode afficherEtudiants pour
afficher les informations de tous les étudiants inscrits :

java
public class GestionInscriptionsIME {

// Tableau pour stocker les étudiants inscrits


private Etudiant[] listeEtudiants;

private int nombreEtudiants;

// Constructeur

public GestionInscriptionsIME(int tailleMax) {

listeEtudiants = new Etudiant[tailleMax];

nombreEtudiants = 0;

// Méthode pour ajouter un nouvel étudiant au tableau

public void ajouterEtudiant(Etudiant etudiant) {

if (nombreEtudiants < listeEtudiants.length) {

listeEtudiants[nombreEtudiants] = etudiant;

nombreEtudiants++;

System.out.println("Étudiant ajouté avec succès.");

} else {

System.out.println("Impossible d'ajouter un nouvel étudiant : tableau plein.");

// Méthode pour afficher les informations de tous les étudiants inscrits

public void afficherEtudiants() {

System.out.println("Liste des étudiants inscrits :");

for (int i = 0; i < nombreEtudiants; i++) {

System.out.println("---------------------------");

listeEtudiants[i].afficherInfos();

}
public static void main(String[] args) {

// Création d'un gestionnaire d'inscriptions avec un tableau de taille 10

GestionInscriptionsIME gestionnaire = new GestionInscriptionsIME(10);

// Création de quelques étudiants

Etudiant etudiant1 = new Etudiant("Dupont", "Jean", "123456", "Informatique");

Etudiant etudiant2 = new Etudiant("Durand", "Marie", "789012", "Mathématiques");

// Ajout des étudiants au gestionnaire d'inscriptions

gestionnaire.ajouterEtudiant(etudiant1);

gestionnaire.ajouterEtudiant(etudiant2);

// Affichage des informations de tous les étudiants inscrits

gestionnaire.afficherEtudiants();

EXERCICE 12

Voici la classe GestionInscriptionsIME mise à jour avec la méthode rechercherEtudiant pour


rechercher un étudiant par son matricule :

public class GestionInscriptionsIME {

// Tableau pour stocker les étudiants inscrits

private Etudiant[] listeEtudiants;

private int nombreEtudiants;

// Constructeur

public GestionInscriptionsIME(int tailleMax) {

listeEtudiants = new Etudiant[tailleMax];

nombreEtudiants = 0;

}
// Méthode pour ajouter un nouvel étudiant au tableau

public void ajouterEtudiant(Etudiant etudiant) {

if (nombreEtudiants < listeEtudiants.length) {

listeEtudiants[nombreEtudiants] = etudiant;

nombreEtudiants++;

System.out.println("Étudiant ajouté avec succès.");

} else {

System.out.println("Impossible d'ajouter un nouvel étudiant : tableau plein.");

// Méthode pour afficher les informations de tous les étudiants inscrits

public void afficherEtudiants() {

System.out.println("Liste des étudiants inscrits :");

for (int i = 0; i < nombreEtudiants; i++) {

System.out.println("---------------------------");

listeEtudiants[i].afficherInfos();

// Méthode pour rechercher un étudiant par son matricule

public void rechercherEtudiant(String matricule) {

boolean trouve = false;

System.out.println("Résultat de la recherche pour le matricule " + matricule + " :");

for (int i =

Vous aimerez peut-être aussi