Professional Documents
Culture Documents
corrigéTD2
corrigéTD2
import java.util.Scanner;
public class Candidat {
private String CIN;
private String nom;
private String prenom;
private String adresse;
private String fonction;
private int age;
private int[]Résultat= new int[33];
public Candidat(String ci, String no, String pr, String ad, String f, int a)
{
CIN=ci;
nom =no;
prenom =pr;
adresse=ad;
fonction =f;
if (age >=25)
age =a;
else age = a;
}
public int getResultat (int nc)
{ if (nc >=1 && nc <=33)
return Résultat[nc-1];
else
return -1;
}
public String getNomPrenom ()
{
return nom + " " + prenom;
}
------------------------------------------------------------------------------------------------------------------------
package propElection;
import java.util.Scanner;
public class Election {
private String dateElection;
private int nbInscrits;
private int nbVotants;
private int nbCandidats = 0;
private Candidat[]tabCandidats = new Candidat[100];
private static String[]listeCirconscription;
public Election (String d, int nbIns)
{
dateElection =d;
nbInscrits = nbIns;
}
public int getNbInscrits()
{
return nbInscrits;
}
public static void setListeCirconscription ()
{ listeCirconscription = new String[2];
Scanner sc = new Scanner (System.in);
for (int i=0; i< 33; i++)
{
System.out.println ("Circonscription " + (i + 1) + " : ");
listeCirconscription[i] = sc.nextLine();
}
}
public void addCandidat (Candidat c)
{ if (nbCandidats <100)
{boolean existe = false;
int i=0;
while ((i <nbCandidats) && (existe == false))
{if (tabCandidats[i].getNomPrenom().equals(c.getNomPrenom ())
existe = true ;
else
i++;
}
if (existe == false)
{tabCandidats[nbCandidats] =c; nbCandidats++;}
else
System.out.println("Ce candidat est déjà inscrit!!!");
}
else
System.out.println ("Trop des candidats, impossible d'ajouter!!!");
}
public void setNbVotants(int nv)
{
if (nv<=nbInscrits)
nbVotants = nv;
}
public int getNbVotants()
{
return nbVotants;
}
public void EnregistrerResultat ()
{ Scanner sc = new Scanner (System.in);
int nv;
for (int i=0; i<33; i++)
{
for (int j=0; j<nbCandidats;j++)
{ nv =sc.nextInt();
tabCandidats[j].setResultat(i, nv);
}
}
}
public Candidat leGagnant ()
{
int i;
int max = tabCandidats[0].totalVotes();;
Candidat g= tabCandidats[0];
for (i=1; i<nbCandidats; i++)
{ if (tabCandidats[i].totalVotes() > max)
{max = tabCandidats[i].totalVotes(); g = tabCandidats[i];}
}
return g;
}
public double getTauxParticipation ()
{
return ((double)nbVotants/nbInscrits)*100;
}
}
------------------------------------------------------------------------------------------------------------------------
package propElection;