Solutions possibles aux exercices de POO (1 à 3)
1. Classe Eleve
public class Eleve
{
public string nom;
public string prenom;
public string da;
public int[] notes;
public int position;
public Eleve(string nom, string prenom, string da, int nbNotes)
{
this.nom = nom;
this.prenom = prenom;
this.da = da;
this.notes = new int[nbNotes];
this.position = 0;
}
public void ajouterNote(int note)
{
if (this.position >= this.notes.Length)
{
int[] nouvTableau = new int[this.notes.Length + 1];
for(int i = 0; i < this.notes.Length; i++)
{
nouvTableau[i] = this.notes[i];
this.notes = nouvTableau;
}
}
this.notes[this.position] = note;
this.position++;
}
public double calculerMoyenne()
{
int total = 0;
double moyenne = 0.0;
for (int i = 0; i < this.notes.Length; i++)
{
total = total + notes[i];
}
moyenne = total / this.notes.Length;
return moyenne;
}
}
2. Classe Fraction
public class Fraction
{
public int numerateur;
public int denominateur;
public Fraction(int numerateur, int denominateur)
{
this.numerateur = numerateur;
if(denominateur == 0)
{
this.denominateur = 1;
}
else
{
this.denominateur = denominateur;
}
}
}
3. Classe Arme
public class Arme
{
public string nom;
public int dmgMax;
public int bonusMagic;
public Arme(string nom, int dmgMax)
{
Random rng = new Random();
this.nom = nom;
this.dmgMax = dmgMax;
// L'arme a 5% de chances d'être magique
if (rng.Next(0, 101) <= 5)
{
int rngBonus = rng.Next(0, 10001);
if (rngBonus <= 1)
{
this.bonusMagic = 5;
}
else if (rngBonus <= 6)
{
this.bonusMagic = 4;
}
else if (rngBonus <= 56)
{
this.bonusMagic = 3;
}
else if (rngBonus <= 156)
{
this.bonusMagic = 2;
}
else if (rngBonus <= 356)
{
this.bonusMagic = 1;
}
else
{
this.bonusMagic = 0;
}
}
}
}
4. Classe Caracteristique
5. Classe Tableau dynamique et programme
5.1 TableauDynamique
public class TableauDynamique
{
double[] tab;
int cnt;
public TableauDynamique()
{
this.tab = new double[10];
this.cnt = 0;
}
public void ajouterNombre(double nombre)
{
// Si le nombre de donné
if (this.cnt < this.tab.Length)
{
this.tab[this.cnt] = nombre;
this.cnt++;
}
else
{
// Nouveau tableau de 1.5 fois la longueur
double[] nouvTab = new double[this.tab.Length + this.tab.Length/2];
// Copier les données du tableau actuel dans le nouveau tableau
for (int i = 0; i < this.tab.Length; i++)
{
nouvTab[i] = this.tab[i];
}
// Remplacer le tableau actuel par le nouveau tableau
this.tab = nouvTab;
this.tab[this.cnt] = nombre;
this.cnt++;
}
}
public double calculerTotal()
{
double total = 0.0;
for (int i = 0; i < this.cnt; i++)
{
total += this.tab[i];
}
return total;
}
public double calculerMoyenne()
{
return this.calculerTotal()/ this.cnt;
}
public double calculerMediane()
{
double mediane = 0;
this.trierAsc();
int position = this.cnt / 2;
if (this.cnt % 2 == 0)
{
mediane = (this.tab[position] + this.tab[position - 1])/2;
}
else
{
mediane = this.tab[position];
}
return mediane;
}
public double calculerEcartType()
{
double moyenne = this.calculerMoyenne();
double sommeCarreEcart = 0.0;
double ecartType = 0.0;
for (int i = 0; i < this.cnt; i++)
{
sommeCarreEcart += Math.Pow(this.tab[i] - moyenne, 2);
}
sommeCarreEcart /= this.cnt - 1;
ecartType = Math.Sqrt(sommeCarreEcart);
return ecartType;
}
public void trierAsc()
{
// Classer mon tableau du plus petit au plus grand
// pour chaque case du tableau
for (int i = 0; i < this.cnt; i++)
{
// trouver l'index(la position) de la note minimum du tableau restant
int positionMin = trouverMin(i);
// échange la note à la position minimum avec la note à la position actuelle
double temp = this.tab[positionMin];
this.tab[positionMin] = this.tab[i];
this.tab[i] = temp;
}
}
public int trouverMin(int depart)
{
int positionMin = depart;
for (int i = depart; i < this.cnt; i++)
{
if (this.tab[i] < this.tab[positionMin])
{
positionMin = i;
}
}
return positionMin;
}
}
5.2 Programme principale
static void Main(string[] args)
{
double note = 0.0;
TableauDynamique tabNotes = new TableauDynamique();
int option = 1;
while (note >= 0.0)
{
Console.Clear();
Console.WriteLine("Entrer les notes de votre groupe, note négative pour arrêter");
double.TryParse(Console.ReadLine(), out note);
if (note >= 0.0)
{
tabNotes.ajouterNombre(note);
}
}
while (option > 0 && option < 5)
{
Console.Clear();
Console.Write("Choisir une option 1-4\n 1. Moyenne\n 2. Mediane\n 3. Ecart type\n 4. Quitter");
int.TryParse(Console.ReadLine(), out option);
switch (option)
{
case 1:
Console.WriteLine("Moyenne : {0}", tabNotes.calculerMoyenne());
break;
case 2:
Console.WriteLine("Mediane : {0}", tabNotes.calculerMediane());
break;
case 3:
Console.WriteLine("Ecart type {0}", tabNotes.calculerEcartType());
break;
case 4:
Console.WriteLine("Fin");
break;
default:
Console.WriteLine("Option invalide");
option = 1;
break;
}
Console.WriteLine("Appuyer sur une touche pour continuer");
Console.ReadKey();
}
}