Vous êtes sur la page 1sur 6

Corrigé de DS2019

package ds2019;

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 (a >=25)
age =a;
else
age = 25;
}
public int getResultat (int nc)
{ if (nc >=1 && nc <=33)
return Résultat[nc-1];
else
return -1;
}
public String getNomPrenom ()
{
return nom + " " + prenom;
}

public void setResultat (int nc, int nv)


{
if (nc >=1 && nc <=33)
{
if (nv>=0)
Résultat[nc-1]=nv;
}
}

public int totalVotes()


{
int s=0;
for (int i=0; i<33; i++)
s+=Résultat[i];
return s;
}
public String toString()
{
return CIN + " " + nom + " " + prenom +
"(" + age + " ans), " + fonction ;
}
}
package ds2019;

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;
}
//Initialisation des circonscriptions à partir du clavier
/*public static void setListeCirconscription ()
{ listeCirconscription = new String[33];
Scanner sc = new Scanner (System.in);
for (int i=0; i< 33; i++)
{
System.out.println ("Circonscription " + (i + 1) + " : ");
listeCirconscription[i] = sc.nextLine();
}
}*/

//Initialisation des circonscriptions sans utiliser la lecture du


clavier

public static void setListeCirconscription ()


{
String lc [] = {"Tunis1", "Tunis2", "Mahdia",
"Sousse","Sfax1", "Sfax2", "Monastir", "Gafsa", "Gabes",
"Tozeur", "France1", "France2", "Italie", "Allemagne",
"Amerique", "Monde arabe", "Tataouine", "Mednine", "Kebelli",
"Sidi Bouzid", "Gasserine","Kairouan","Ben Arous", "El kef",
"Seliana", "Beja", "Jendouba", "Mannouba", "Bizerte",
"Ariana", "Nabeul1", "Nabeul2", "Zaghouan"};

listeCirconscription = lc;

}
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 && nv>=0)
nbVotants = nv;
}
public int getNbVotants()
{
return nbVotants;
}

//Saisie de résultat à partir du clavier

public void EnregistrerResultat ()


{ Scanner sc = new Scanner (System.in);
int nv;
int nbt=0; //nombre de votants
for (int i=0; i<33; i++)
{
System.out.println ("Circonscription " +
listeCirconscription[i] + ": ");
for (int j=0; j<nbCandidats;j++)
{
System.out.println(tabCandidats[j].getNomPrenom() + ": ");
nv =sc.nextInt();
tabCandidats[j].setResultat(i, nv);
nbt+= nv;

}
setNbVotants(nbt);
}
public Candidat leGagnant ()
{ //On a pas traité le cas des ex aequo
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 ds2019;

public class StatElection {


public static void main (String []argv)
{
System.out.println ("***************************Election
présentielle 2019***************************");
Election El2019 = new Election ("13/10/2019", 7074566);
Election.setListeCirconscription();
Candidat c1 = new Candidat("12345678", "Said", "Kais","Ariana",
"Enseignant universitaire", 61);
Candidat c2 = new Candidat ("91025897", "Karoui", "Nebil",
"Biezrte", "Homme d'affaires", 56);
El2019.addCandidat(c1);
El2019.addCandidat(c2);

System.out.println ("Listes des candidats : ");


System.out.println ("\t" + c1 + "\n\t" + c2);
System.out.println
("******************************************************************
**************");
El2019.setNbVotants(3820825);
System.out.println ("Nombre des inscrits : "+
El2019.getNbInscrits());
System.out.println ("Nombre des votants : "+ El2019.getNbVotants());
System.out.println ("Taux de participation : "+
El2019.getTauxParticipation());
El2019.EnregistrerResultat();
System.out.println
("******************************************************************
**************");
System.out.println ("Total des votes par Candidat :");
System.out.println ("\t" + c1.getNomPrenom() + ": ( " +
c1.totalVotes()+ " votes), pourcentage = " +
((double)c1.totalVotes()/El2019.getNbVotants())*100 );
System.out.println ("\t" + c2.getNomPrenom() + ": ( " +
c2.totalVotes()+ " votes), pourcentage = " +
((double)c2.totalVotes()/El2019.getNbVotants())*100 );
System.out.println
("******************************************************************
**************");
System.out.println ("Le gagnant est : " + El2019.leGagnant());

Vous aimerez peut-être aussi