Presentation is loading. Please wait.

Presentation is loading. Please wait.

ELECTION NIGHT CITS1001.

Similar presentations


Presentation on theme: "ELECTION NIGHT CITS1001."— Presentation transcript:

1 ELECTION NIGHT CITS1001

2 Software development Understand the problem Understand the solution
Structure the solution Implement the solution Test the solution And then go around again… 

3 Preferential voting in the Australian House of Reps
An election has M candidates (we will use five candidates ABCDE in examples) N formal votes One winner, who needs N/2+1 votes to triumph Each voter returns a list of preferences for all candidates e.g. CAEDB But not CAE or CAEDA or CAXEDB or …

4 The count Discard informal votes
Allocate each formal vote to its first preference candidate Then in each round of the count If the leading candidate has enough votes, Declare them the winner Otherwise, Identify the candidate X with the fewest votes Redistribute each of X’s votes to its highest-ranked surviving candidate Eliminate X

5 An example 100 votes: 12 informal, and 88 formal
88/2+1 votes = 45 required to win After the initial distribution A 11, B 14, C 29, D 25, E 9 No winner yet, so E is eliminated in Round 1 e.g. vote EABDC would be transferred to A A 17, B 16, C 29, D 26 No winner yet, so B is eliminated in Round 2 e.g. BEDAC goes to D and EBADC goes to A A 22, C 31, D 35 No winner yet, so A is eliminated in Round 3 C 47, D 41 C has enough votes to be declared the winner

6 What types of entities do we have?
Votes Each vote is an expression of a voter’s preferences Candidates Each candidate collects votes until they either win or are eliminated Elections Each election represents one constituency

7 The Vote class What is the state of a Vote?
The list of preferences for surviving candidates What inputs does the constructor need? The voter’s original list of preferences What accessor methods does Vote provide? Who the Vote is currently for: public char getFirstPreference(String losers) What other methods does Vote provide? A test for whether the Vote is formal: public boolean isFormal(String candidates)

8 The Candidate class What is the state of a Candidate?
Their name, and their current pile of Votes What inputs does the constructor need? Their name What accessor methods does Candidate provide? getName(), getVotes(), getCount() What other methods does Candidate provide? A way of adding votes to the pile: public void addVotes(ArrayList<Vote> vs, String losers) A test for whether the candidate has won: public boolean isWinner(int noOfVotes)

9 The Election class What is the state of an Election?
A pile of Votes for each surviving Candidate What inputs does the constructor need? Where to get the voting papers and the candidates’ names from What accessor methods does Election provide? None – “top-level” class Although there could be a higher class with a collection of Elections What other methods does Election provide? The constructor could do the initial distribution A method to perform one round of the count Probably several private methods to structure the code

10 Implementation – Class Diagram
Objects First with Java Implementation – Class Diagram © David J. Barnes and Michael Kölling

11 Implementation questions – Vote
What type should the list of preferences be? If the candidates’ names are just one char each, a simple String will do Otherwise a String[] or an ArrayList<String>, depending on… Should we delete the names of eliminated candidates, or just ignore them? We could ignore them when looking for the next preference We could delete them as needed We could delete all mentions of c at the time they are eliminated

12 Implementation questions – Candidate
What type should the pile of Votes be? For most candidates, the pile will grow during the count So an ArrayList<Vote> is best

13 Implementation questions – Election
What type should the pile of Candidates be? Again, an ArrayList<Candidate> is best Should we delete eliminated Candidates, or just ignore them? We can use multiple constructors for testing: public Election(String candidates, String votes) {…} public Election(String votes) {this(“candidates.txt”, votes);} public Election() {this(“votes.txt”);}

14 Take a look at one solution
Available on the lectures page Along with four sample elections [small, no contest, medium, large] [candidates, votes, results] SC.txt, SV.txt, SR.txt NC.txt, NV.txt, NR.txt MC.txt, MV.txt, MR.txt LC.txt, LV.txt, LR.txt


Download ppt "ELECTION NIGHT CITS1001."

Similar presentations


Ads by Google