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:


program-start
    // data
    inputOK: boolean
    numberOfSeatsToFill: integer
    numberOfLists: integer
    listNames[]: strings
    votesList[]: integer
    removeList[]: boolean
    seatsList[] : integer
    averageList[]: real
    i: integer
    validVotes: integer
    electoralQuotient: real
    numberOfFilledSeats: integer
    MaxAverage: real
    Max: integer iSeat: integer

    // code
    // number of seats to be filled
    inputOK<-false
    while not inputOK
        write "Number of seats to be filled: "
        read numSeatsToFill
        if nSeatsToFill is not a positive integer >0 then
            print "Error: Enter an integer >0"
        else
            inputOK <- true
        endif
    endwhile
    // number of competing lists
    inputOK <- false
    while not inputOK
        print "Number of competing lists: "
        read nbLists
        if nbLists is not a positive integer >0 then
            print "Error: Enter an integer >0"
        else
            inputOK <- true
        endif
    endwhile

    // allocate arrays
    allocate the arrays nameList, voteList, eliminatedList, seatsList, averageList to nbLists elements

    // Entering list names and votes
    totalVotes <- 0
    for i ranging from 0 to nbLists-1
        // Enter the name of list i
        entryOK <- false
        while not entryOK
        print "List name # ", i, " : "
        read listName[i]
        if listName[i] is empty then
            print "Error: Enter a non-empty name"
        else
            inputOK<-true
        end
    endwhile
    // Enter the number of votes for list i
    inputOK <- false
    while not inputOK
        print "Number of votes for list ", listName[i] , " : "
        read votesList[i]
        if votesList[i] is not an integer >= 0 then
            print "Error: Enter an integer >=0"
        else
            inputOK <- true
        endif
    endwhile
  
    // increment the total votes
    totalVotes <- totalVotes + votesList[i]79.endfor 80.
    // calculate valid votes
    validVotes <- 0
    for i ranging from 0 to nbLists-1
        if (voteList[i] / totalVotes) < 0.05 then
            eliminateList[i] <- true
        else
            removeList[i] <- false
            validVotes <- validVotes + listVote[i]
        endif
    endif
    // Are there any lists left?
    if validVotes = 0 then
        print "Error: all lists have been eliminated"
        end program
    end
    // Allocate seats based on the quotient
    electoralQuotient <- validVotes / seatsToBeFilled
    seatsFilled <- 0
    for i ranging from 0 to nbLists-1
        if not eliminatedList[i] then
            seatsList[i] <- integer part of (votesList[i] / electoralQuotient)
            averageList[i] <- votesList[i] / (seatsList[i]+1)
            numberOfSeatsFilled <- numberOfSeatsFilled + listSeats[i]
        else
            seatsList[i] <- 0
        end
    endFor
    // Allocate remaining seats to the highest average
    // 1 seat is allocated in each loop iteration
    for iSeat ranging from 0 to numSeatsToFill - numSeatsFilled - 1
        // find the list with the highest average
        maxAverage <- (-1)
        for i ranging from 0 to nbLists-1
            if not eliminateList[i] then
                if listMean[i] > maxMean then
                    averageMax <- averageList[i]
                    iMax <- i
                end
            end
        endfor
        // assign 1 seat to the list with the highest average
        seatList[iMax] <- seatList[iMax]+1
        // and we update its average
        listAverage[iMax] <- listVotes[iMax] / (listSeats[iMax] + 1)
    endfor
    // display results without sorting
    for i ranging from 0 to nbLists-1
        if listEliminated[i] then
            print "The list ", listName[i], " has been eliminated"
        else
            print "The list ", listName[i], " received ",
            seatsList[i], " seat(s)"
        endif
    endfor
end-program

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