election night cits1001. example development understanding the problem understanding the solution...
TRANSCRIPT
![Page 1: ELECTION NIGHT CITS1001. Example development Understanding the problem Understanding the solution Structuring the solution Implementing the solution (Testing](https://reader036.vdocuments.site/reader036/viewer/2022072005/56649cf65503460f949c540c/html5/thumbnails/1.jpg)
ELECTION NIGHTCITS1001
![Page 2: ELECTION NIGHT CITS1001. Example development Understanding the problem Understanding the solution Structuring the solution Implementing the solution (Testing](https://reader036.vdocuments.site/reader036/viewer/2022072005/56649cf65503460f949c540c/html5/thumbnails/2.jpg)
2
Example development• Understanding the problem• Understanding the solution• Structuring the solution• Implementing the solution• (Testing the solution)
![Page 3: ELECTION NIGHT CITS1001. Example development Understanding the problem Understanding the solution Structuring the solution Implementing the solution (Testing](https://reader036.vdocuments.site/reader036/viewer/2022072005/56649cf65503460f949c540c/html5/thumbnails/3.jpg)
3
Preferential voting• 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 …
• http://www.aec.gov.au/Voting/counting/hor_count.htm
![Page 4: ELECTION NIGHT CITS1001. Example development Understanding the problem Understanding the solution Structuring the solution Implementing the solution (Testing](https://reader036.vdocuments.site/reader036/viewer/2022072005/56649cf65503460f949c540c/html5/thumbnails/4.jpg)
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
![Page 5: ELECTION NIGHT CITS1001. Example development Understanding the problem Understanding the solution Structuring the solution Implementing the solution (Testing](https://reader036.vdocuments.site/reader036/viewer/2022072005/56649cf65503460f949c540c/html5/thumbnails/5.jpg)
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. vote BEDAC would be transferred to D • 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
![Page 6: ELECTION NIGHT CITS1001. Example development Understanding the problem Understanding the solution Structuring the solution Implementing the solution (Testing](https://reader036.vdocuments.site/reader036/viewer/2022072005/56649cf65503460f949c540c/html5/thumbnails/6.jpg)
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
![Page 7: ELECTION NIGHT CITS1001. Example development Understanding the problem Understanding the solution Structuring the solution Implementing the solution (Testing](https://reader036.vdocuments.site/reader036/viewer/2022072005/56649cf65503460f949c540c/html5/thumbnails/7.jpg)
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 do Votes provide?• Who the Vote is currently for:
public char getFirstPreference(String losers)
• What other methods do Votes provide? • A test for whether the Vote is formal:
public boolean isFormal(String candidates)
![Page 8: ELECTION NIGHT CITS1001. Example development Understanding the problem Understanding the solution Structuring the solution Implementing the solution (Testing](https://reader036.vdocuments.site/reader036/viewer/2022072005/56649cf65503460f949c540c/html5/thumbnails/8.jpg)
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 do Candidates provide? • getName(), getVotes(), getCount()
• What other methods do Candidates 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)
![Page 9: ELECTION NIGHT CITS1001. Example development Understanding the problem Understanding the solution Structuring the solution Implementing the solution (Testing](https://reader036.vdocuments.site/reader036/viewer/2022072005/56649cf65503460f949c540c/html5/thumbnails/9.jpg)
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 do Elections provide? • None – “top-level” class • Although there could be a higher class with a collection of Elections
• What other methods do Elections 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
![Page 10: ELECTION NIGHT CITS1001. Example development Understanding the problem Understanding the solution Structuring the solution Implementing the solution (Testing](https://reader036.vdocuments.site/reader036/viewer/2022072005/56649cf65503460f949c540c/html5/thumbnails/10.jpg)
10
Implementation – Class Diagram
![Page 11: ELECTION NIGHT CITS1001. Example development Understanding the problem Understanding the solution Structuring the solution Implementing the solution (Testing](https://reader036.vdocuments.site/reader036/viewer/2022072005/56649cf65503460f949c540c/html5/thumbnails/11.jpg)
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
![Page 12: ELECTION NIGHT CITS1001. Example development Understanding the problem Understanding the solution Structuring the solution Implementing the solution (Testing](https://reader036.vdocuments.site/reader036/viewer/2022072005/56649cf65503460f949c540c/html5/thumbnails/12.jpg)
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
![Page 13: ELECTION NIGHT CITS1001. Example development Understanding the problem Understanding the solution Structuring the solution Implementing the solution (Testing](https://reader036.vdocuments.site/reader036/viewer/2022072005/56649cf65503460f949c540c/html5/thumbnails/13.jpg)
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”);}