Les solutions Javascool aux exercices posés dans ce chapitre sont disponibles
en cliquant ici, mais je vous conseille vivement de chercher
auparavant à les résoudre et à les tester sur machine (en Javascool ou dans le langage
de votre choix).
Exercice 1 : bon de commande
1) Réécrivez le programme gérant un bon de commande (section 7.1)
avec des enregistrements :
Définition du type |
Programme principal |
class BonCommande {
int n;
String[] nom;
double[] prixUnitaire;
int[] quantite;
}
|
void main() {
// saisie des donnees
BonCommande bon = new BonCommande();
bon.n = readInt("nombre d'articles :");
bon.nom = new String[bon.n];
...
// calcul et affichage du total
double totalCom;
...
print("le total de la commande vaut ");
println(totalCom);
}
|
2) Réécrivez le programme de la question 1 en introduisant une procédure de saisie
et une fonction calculant le total de la commande :
Programme principal |
Sous-programmes |
void main() {
BonCommande bon=new BonCommande();
saisirCommande(bon);
double totalCom;
totalCom = totalCommande(bon);
print("le total vaut ");
println(totalCom);
}
|
void saisirCommande(BonCommande bon) {
bon.n=readInt("nombre d'articles :");
bon.nom = new String[bon.n];
...
}
double totalCommande(BonCommande bon) {
double totalCom;
totalCom = 0;
...
return totalCom;
}
|
Exercice 2 : notes
1) Résolvez l'exercice 3
de travaux dirigés du chapitre 6 avec des enregistrements :
Définition du type et de la fonction |
Programme principal |
class Etudiant {
String nom;
double math;
double ang;
}
String getMention(Etudiant etud) {
String mention;
...
return mention;
}
|
void main() {
// saisie des donnees
Etudiant etud = new Etudiant();
...
// calcul de la mention
String mention;
...
print("la mention est ");
println(mention);
}
|
2) Adaptez l'algorithme de tri étudié dans le chapitre 7
afin de trier un tableau d'étudiants par moyenne décroissante.
void main() {
// saisie des donnees
int n;
n = readInt("nombre d'etudiants :");
Etudiant [] tab = new Etudiant[n];
int i;
for(i=0; i<n; i=i+1) {
tab[i] = new Etudiant();
tab[i].nom = readString("nom de l'etudiant :");
tab[i].math = readDouble("note de mathematique :");
tab[i].ang = readDouble("note d'anglais :");
}
// tri par moyenne decroissante
...
// affichage du tableau trie
...
}