Skip to content

2. [Assignment]: The Problem

Keywords: algorithms, Java basics: arrays, I/O, loops, testing, exception handling

Recommended reading: Chapter 1 of [ref1]: The Basics of the Java Language

2.1. Support

  

The folder [support / chap-02] contains the algorithm to be implemented in C# and Java.

2.2. The problem to be solved

We want to write a program that, on election night, can calculate the number of seats won by the various candidate lists. Further on, we will find the method for calculating seats for a proportional representation election using the highest average, as explained in an article in the Ouest-France newspaper on March 15, 1986.

We will write a Java "console" application, i.e., an application that uses the keyboard and screen to interact with the user. It will ask the user for the following information (entered via the keyboard):

  • number of seats to be filled
  • number of competing lists
  • for each list: its name and number of votes

Using this information, the application calculates the number of seats won by each list and displays them on the screen in the following format:

  • List [X1] has won [N1] seats

  • List [X2] won [N2] seats

  • ...

where [Xi] is the name of list No. i and [Ni] is the number of seats it won.

The Ouest-France article of March 15, 1986:

Image

Image

2.3. The algorithmic solution

An algorithmic solution could be as follows:

début-programme
    // données
    saisieOK : booléen
    nbSiègesAPourvoir : entier
    nbListes : entier
    nomListe[] : chaînes de caractères
    voixListe[] : entier
    elimineListe[] : booléen
    siegesListe[] : entier
    moyenneListe[] : réel
    i : entier
    nbVoixUtiles : entier
    quotientElectoral : réel
    nbSiègesPourvus : entier
    moyenneMax : réel
    Max : entier iSiège : entier

    // code
    // nbre de sièges à pourvoir
    saisieOK<-faux
    tant que non saisieOK
        écrire "Nombre de sièges à pourvoir : "
        lire nbSiègesAPourvoir
        si nbSiègesAPourvoir n'is not an integer >0 then
            écrire "Erreur : tapez un nombre entier >0"
        sinon
            saisieOK<-vrai
        finsi
    fintantque
    // nbre de listes en compétition
    saisieOK<-faux
    tant que non saisieOK
        écrire "Nombre de listes en compétition : "
        lire nbListes
        si nbListes n'is not an integer >0 then
            écrire "Erreur : tapez un nombre entier >0"
        sinon
            saisieOK<-vrai
        finsi
    fintantque

    // dimensionnement des tableaux
    dimensionner les tableau nomListe, voixListe, elimineListe, siegesListe, moyenneListe à nbListes éléments

    // saisie des noms et voix des listes
    totalVoix<-0
    pour i variant de 0 à nbListes-1
        // saisie du nom de la liste i
        saisieOK<-faux
        tantque non saisieOK
        écrire "Nom de la liste n° ", i, " : "
        lire nomListe[i]
        si nomListe[i] est vide alors
            écrire "Erreur : Tapez un nom non vide"
        sinon
            saisieOK<-vrai
        finsi
    fintantque
    // saisie du nombre de voix de la liste i
    saisieOK<-faux
    tantque non saisieOK
        écrire "Nombre de voix de la liste ", nomListe[i] , " : "
        lire voixListe[i]
        si voixListe[i] n'is not an integer >=0 then
            écrire "Erreur : tapez un nombre entier >=0"
        sinon
            saisieOK<-vrai
        finsi
    fintantque

    // on incrémente le total des voix
    totalVoix<- totalVoix+voixListe[i]79.finpour 80.
    // calcul des voix utiles
    nbVoixUtiles<-0
    pour i variant de 0 à nbListes-1
        si (voixListe[i]/totalVoix)<0.05 alors
            elimineListe[i]<-vrai
        sinon
            elimineListe[i]<-faux
            nbVoixUtiles<-nbVoixUtiles+voixListe[i]
        finsi
    finpour
    // y-a-t-il des listes non éliminées ?
    si nbVoixUtiles=0 alors
        écrire "Erreur : toutes les listes ont été éliminées"
        arrêt du programme
    finsi
    // répartition des sièges au quotient
    quotientElectoral <- nbVoixUtiles / nbSiègesAPourvoir
    nbSiègesPourvus<- 0
    pour i variant de 0 à nbListes-1
        si non elimineListe[i] alors
            siegesListe[i]<- partie entière de (voixListe[i]/quotientElectoral)
            moyenneListe[i] <- voixListe[i] / (siegesListe[i]+1)
            nbSiègesPourvus<-nbSiègesPourvus+siegesListe[i]
        sinon
            siegesListe[i]<-0
        finsi
    finpour
    // répartition des sièges restants à la plus forte moyenne
    // 1 siège est attribué à chaque tour de boucle
    pour iSiège variant de 0 à nbSiègesAPourvoir - nbSiègesPourvus - 1
        // recherche de la liste ayant la + forte moyenne
        moyenneMax<- (-1)
        pour i variant de 0 à nbListes-1
            si non elimineListe[i] alors
                si moyenneListe[i] > moyenneMax alors
                    moyenneMax <- moyenneListe[i]
                    iMax <- i
                finsi
            finsi
        finpour
        // on attribue 1 siège à la liste de + forte moyenne
        siegeListe[iMax] <- siegeListe[iMax]+1
        // et on change sa moyenne
        moyenneListe[iMax] <- voixListe[iMax]/(siegeListe[iMax]+1)
    finpour
    // affichage résultats sans tri
    pour i variant de 0 à nbListes-1
        si elimineListe[i] alors
            écrire "La liste ", nomListe[i], " a été éliminée"
        sinon
            écrire "La liste ", nomListe[i], " a obtenu ",
            siegesListe[i], " siège(s)"
        finsi
    finpour
fin-programme

2.4. Work to be done

Q1: Translate the algorithm into C#. Implement it using Visual Studio.

Q2: Translate the algorithm into Java, drawing inspiration from the C# code. Implement the Java program in an Eclipse environment similar to the following:

  • [1]: The project is named [elections-01]
  • [2]: The application will be placed in a package, here [istia.st.elections]
  • [3]: [MainElections.java] is the source code for the application written in the previous section
  • [4]: the [MainElections] class is executed

An example of execution could be as follows:

Image