Correction exercices POO dans dart Série 01
Sommaire
- 1- Exercice 01
- 1.1- Énoncé
- 1.2- Solution
- 2- Exercice 02
- 2.1- Énoncé
- 2.2- Solution
- 3- Exercice 03
- 3.1- Énoncé
- 3.2- Solution
- 4- Exercice 04
- 4.1- Énoncé
- 4.2- Solution
- 5- Exercice 05
- 5.1- Énoncé
- 5.2- Solution
- 6- Exercice 06
- 6.1- Énoncé
- 6.2- Solution
- 7- Exercice 07
- 7.1- Énoncé
- 7.2- Solution
- 8- Exercice 08
- 8.1- Énoncé
- 8.2- Solution
- 9- Exercice 09
- 9.1- Énoncé
- 9.2- Solution
- 10- Exercice 10
- 10.1- Énoncé
- 10.2- Solution
- 11- Exercice 11
- 11.1- Énoncé
- 11.2- Solution
- 12- Exercice 12
- 12.1- Énoncé
- 12.2- Solution
- 12.2.1- Cours Flutter
Correction exercices POO dans dart Série 01
Exercice 01
-
Énoncé
- Vous pouvez visualiser l’énoncé de l’exercice
-
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é
- Vous pouvez visualiser l’énoncé de l’exercice
-
Solution
- 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 dethis
vers le paramètre de la méthode. L’argent est retiré d’un compte et déposé sur l’autre.
-
Question 1
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 {
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é
- Vous pouvez visualiser l'énoncé de l'exercice
-
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é
- Vous pouvez visualiser l'énoncé de l'exercice
-
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();
}