Back

Correction exercices POO dans dart Série 01

Correction exercices POO dans dart Série 01

  • Exercice 01

    • Énoncé
    • Solution
      • class Point {
          double _x, _y;
          Point(this._x, this._y);
          void move(double mx, double my) {
            _x += mx;
            _y += my;
          }
        
          void display() {
            print("x : $_x  y : $_y");
          }
        }
        
        void main() {
          var pt = Point(2, 6.5);
          pt.display();
          pt.move(-2, 3.25);
          pt.display();
        }
  • Exercice 02

    • Énoncé
    • Solution
      • Question 1

      • Réponse :La méthode virement fait intervenir deux objets de type Compte : this, l’objet sur lequel la méthode est appelée et destination, le paramètre de la méthode. Le virement s’effectue de this vers le paramètre de la méthode. L’argent est retiré d’un compte et déposé sur l’autre.
      • Question 2 et Question 3

        import 'dart:io';
        
        import 'Compte.dart';
        
        void main() {
          Compte martin, jean;
          List table = List.filled(11, Compte());
        
          martin = Compte();
          jean = Compte();
        // dépôt de 500 euros sur le premier compte.
          martin.deposer(500);
        // dépôt de 1000 euros sur le second compte.
          jean.deposer(1000);
        // retrait de 10 euros sur le second compte.
          jean.retirer(10);
        // virement de 75 euros du premier compte vers le second.
          martin.virerVers(75, jean);
        // affichage des soldes des deux comptes.
          stdout.write("Compte de martin, ");
          martin.afficher();
          print("---------------------------------");
          stdout.write("Compte de jean, ");
          jean.afficher();
          print("----------------------------------");
        //Question03
          for (int i = 0; i < table.length; i++) {
            table[i] = Compte();
            table[i].deposer(200 + i * 100);
          }
          for (int i = 0; i < table.length; i++) {
            for (int j = i + 1; j < table.length; j++) {
              table[i].virerVers(20, table[j]);
            }
          }
          for (int i = 1; i < table.length; i++) {
            stdout.write("Compte numero $i , ");
            table[i].afficher();
            print("----------------------------------");
          }
        }
        
      • class: Compte
        • class Compte {
            int solde = 0;
            void deposer(int montant) {
              solde = solde + montant;
            }
          
            void retirer(int montant) {
              solde = solde - montant;
            }
          
            void virerVers(int montant, Compte destination) {
              retirer(montant);
              destination.deposer(montant);
            }
          
            void afficher() {
              print("solde: $solde");
            }
          }
          
    • Exercice 03

      • Énoncé
      • Solution
        • import 'dart:io';
          
          import 'Compte.dart';
          
          void main() {
            Compte martin, jean;
            List table = List.filled(11, Compte());
          
            martin = Compte();
            jean = Compte();
          // dépôt de 500 euros sur le premier compte.
            martin.deposer(500);
          // dépôt de 1000 euros sur le second compte.
            jean.deposer(1000);
          // retrait de 10 euros sur le second compte.
            jean.retirer(10);
          // virement de 75 euros du premier compte vers le second.
            martin.virerVers(75, jean);
          // affichage des soldes des deux comptes.
            stdout.write("Compte de martin, ");
            martin.afficher();
            print("---------------------------------");
            stdout.write("Compte de jean, ");
            jean.afficher();
            print("----------------------------------");
          //Question03
            for (int i = 0; i < table.length; i++) {
              table[i] = Compte();
              table[i].deposer(200 + i * 100);
            }
            for (int i = 0; i < table.length; i++) {
              for (int j = i + 1; j < table.length; j++) {
                table[i].virerVers(20, table[j]);
              }
            }
            for (int i = 1; i < table.length; i++) {
              stdout.write("Compte numero $i , ");
              table[i].afficher();
              print("----------------------------------");
            }
          }
          
          

          Classe Compte

            // ignore: file_names
            import 'dart:web_gl';
            
            class Compte {
              int solde = 0;
              String? titulaire;
              Compte(String n) {
                titulaire = n;
              }
              void deposer(int montant) {
                solde = solde + montant;
              }
            
              void retirer(int montant) {
                solde = solde - montant;
              }
            
              void virerVers(int montant, Compte destination) {
                destination.retirer(montant);
                destination.deposer(montant);
              }
            
              void afficher() {
                print("Compte de $titulaire, solde: $solde");
              }
            }
            
            
      • Exercice 04

        • Énoncé
        • Solution
          • class Person {
              Person({
                required this.name,
                required this.age,
                required this.height,
              });
              final String name;
              final int age;
              final double height;
            
              void printDescription() {
                print("Je m'appelle ${person['name']}. J'ai ${person['age']} ans, je mesure ${person['height']} mètres.");
              }
            }
            void main() {
              final p1 = Person(name: 'Mohamed', age: 36, height: 1.84);
              final p2 = Person(name: 'Mouna', age: 44, height: 1.76);
              p1.printDescription();
              p2.printDescription();
            }




    Riadh HAJJI

    Abonnez vous à notre chaîne YouTube gratuitement