// TP Atelier 3 (classe Eleve)
public class Eleve
{
public String nom;
public float ne,no;
public float moyenne()
{
return ((int) (((2*ne+no)/3) * 100))/100F;
}
public void initial(String n, float no, float ne)
{
nom = n;
this.no=no;
this.ne=ne;
}
public void affich()
{
System.out.println("Nom : " + nom + " Moyenne : " + moyenne());
}
}
// TP Atelier 3 (classe Section)
import java.io.*;
public class Section
{
private String nom;
private Eleve e[]=new Eleve[30];
private int nb=0;
public int effectif()
{
return nb;
}
public void affichnomsec()
{
System.out.println("Nom : " + nom);
}
public void initial(int i) throws IOException
{
nom = "TSIO" + i;
char rep=' ';
int j=1;
System.out.println("\n" + nom);
do
{
float ne=Entree.flottant("\nSaisir la note d'écrit");
float no = Entree.flottant("Saisir la note d'oral");
String n = Entree.chaine("Saisir le nom");
Eleve e = new Eleve();
e.initial(n,no,ne);
j++;
ajouter(e);
if (j<=30)
rep = Entree.car("Saisir O pour un autre élève sinon appuyer sur n'importe quelle touche");
}
while (j<=30 && rep=='O');
}
public Eleve meilleur ()
{
int i=0;
int max=0;
for (i=0; i<nb;i++)
{
if (e[i].moyenne()>e[max].moyenne())
max=i;
}
return e[max];
}
public float moygen()
{
float cum=0;
int i;
for (i=0;i<nb;i++)
cum+=e[i].moyenne();
return cum/nb;
}
public Section compare(Section s)
{
return (this.moygen()>s.moygen() ? this : s);
}
public void affichelevsec()
{
if (nb==0)
System.out.println("\nIl n'y a pas d'élève dans cette section");
else
{
int i;
for(i=0;i<nb;i++)
e[i].affich();
}
}
public void affichelevsec(float moy, int op)
{
int i;
boolean pres=false;
for(i=0;i<nb;i++)
{
if (op==1)
{
if (e[i].moyenne() <= moy)
{
e[i].affich();
pres=true;
}
}
else
{
if (e[i].moyenne() >= moy)
{
e[i].affich();
pres=true;
}
}
if (!pres)
System.out.println("Il n'y a pas d'élève avec ce critère de moyenne dans cette section");
}
}
public void ajouter(Eleve el)
{
e[nb++]=el;
}
}
// TP Atelier 3 (classe de test - main)
import java.io.*;
import metier.*;
class Prog_TP3
{
public static void main(String argv[]) throws IOException
{
int sec, sec2, i, j, choix, op, nbsec=2;
float moy;
String sai;
Section[] tab = new Section[nbsec];
boolean tabExiste[] = new boolean[nbsec];
for (i=0; i<nbsec ; i++)
tabExiste[i] = false;
do
{
System.out.println("\nMenu general (la plupart des controles de saisie n'ont pas été effectués)");
System.out.println("\n************");
System.out.println("\n1 : Ajout d'une section");
System.out.println("\n2 : Afficher tous les eleves");
System.out.println("\n3 : Afficher les eleves d'une section");
System.out.println("\n4 : Afficher les eleves d'une section ayant une moyenne >= a la valeur saisie");
System.out.println("\n5 : Afficher le meilleur eleve d'une section");
System.out.println("\n6 : Afficher la meilleure section");
System.out.println("\n7 : Quitter");
choix=Entree.entier("\n\n\nSaisir votre choix");
switch(choix)
{
case 1 :
do
sec = Entree.entier("\nSaisir le numero de la section");
while (sec <= 0 || sec > nbsec);
if (tabExiste[sec-1])
System.out.println("\nCette section existe deja");
else
{
tab[sec-1] = new Section(sec);
// Ajouter des eleves
int nb = Entree.entier("Combien d'eleves voulez-vous creer ? ");
String nom="";
for (i=0; i < nb; i++)
{
nom = Entree.chaine("Saisir le nom de l'eleve " + (i+1));
float noteEcrit = Entree.flottant("Note d'ecrit ? ");
float noteOral = Entree.flottant("Note d'oral ? ");
Eleve e = new Eleve(nom, noteEcrit, noteOral);
tab[sec-1].ajouter(e);
}
tabExiste[sec-1]=true;
}
break;
case 2 :
for (i = 0; i<nbsec; i++)
{
if (tabExiste[i])
{
System.out.println("\n-- Eleves de la section tsio" + (i+1) + " --");
tab[i].affichelevsec();
}
}
break;
case 3 :
// on n'effectue pas le ctrl de saisie
sec = Entree.entier("\nSaisir le numero de la section");
System.out.println("\n-- Eleves de la section tsio" + sec + " --");
tab[sec-1].affichelevsec();
break;
case 4 :
// on n'effectue pas le ctrl de saisie
sec = Entree.entier("\nSaisir le numero de la section");
moy = Entree.flottant("Saisir la moyenne");
tab[sec-1].affichelevsec(moy);
break;
case 5 :
// on n'effectue pas le ctrl de saisie
sec = Entree.entier("\nSaisir le numero de la section");
tab[sec-1].meilleur().affiche();
break;
case 6 :
// on n'effectue pas le ctrl de saisie
sec = Entree.entier("\nSaisir le numero de la section");
sec2 = Entree.entier("Saisir le numero d'une autre section");
System.out.println("Meilleure des 2 sections :");
tab[sec-1].compare(tab[sec2-1]).affichnomsec();
break;
case 7 : System.out.println("Au revoir");
break;
}
// Saisie factice afin de permettre a l'utilisateur de vopir le resultat affiché
if (choix != 1)
sai=Entree.chaine("\nAppuyer sur une touche pour revenir au menu");
}
while (choix!=7);
}
}