fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. private List<String> ballotList;
  11.  
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14. List<String> list = new ArrayList<String>();
  15. list.add("a");
  16. list.add("c");
  17. list.add("c");
  18.  
  19.  
  20. Ideone i = new Ideone(list);
  21.  
  22. for(String s : list)
  23. {
  24. System.out.println(s + " votes " + i.numFirstVotes(s,list));
  25. }
  26. System.out.println(i.getWinner(list));
  27. }
  28.  
  29. public Ideone(List<String> ballotList)
  30. {
  31. this.ballotList = ballotList;
  32. }
  33.  
  34. public int numFirstVotes(String s, List<String> ss)
  35. {
  36. return (int) s.charAt(0) - 'a';
  37. }
  38.  
  39. /**
  40. * Returns the winner of the election using the candidatesWithFewest()
  41. * method.If there is no winner method returns a statement stating the
  42. * election is not decisive.
  43. *
  44. * @param vbal VoterBallots object
  45. * @param candList a list of candidate names
  46. * @return the winner of the election
  47. */
  48. public String getWinner(List<String> candList) {
  49. // Run rounds until down to a single candidate
  50. while (candList.size() > 1) {
  51. ArrayList<String> loser = candidatesWithFewest(candList);
  52. String listString = "";
  53.  
  54. for (String s : loser) {
  55. listString += s;
  56. candList.remove(s);
  57. }
  58. }
  59.  
  60. if (candList.size() > 0) {
  61. return candList.iterator().next(); // Return the surviving candidate
  62. } else {
  63. return "Election is non decisive.";
  64. }
  65. }
  66.  
  67. /**
  68. * Returns a list of one or more candidates tied with the fewest
  69. * first choice votes
  70. *
  71. * Precondition: each String in candidateList appears exactly once
  72. * in each Ballot in ballotList
  73. *
  74. * @param candidateList a list of candidate names
  75. *
  76. * @return a list of those candidates tied with the fewest first
  77. * choice votes
  78. */
  79. public ArrayList<String> candidatesWithFewest(List<String> candidateList) {
  80. ArrayList<String> losers = new ArrayList<String>(); //empty list for losers
  81. int minTally = ballotList.size() + 1; //number of min votes
  82. for (int can = 0; can < candidateList.size(); can++) {
  83. String candidate = candidateList.get(can);
  84. // // number of first place votes
  85. int votes = numFirstVotes(candidate, candidateList);
  86. if (votes < minTally) {
  87. minTally = votes;
  88. losers = new ArrayList<String>(); // adds loser to list
  89. }
  90. if (votes == minTally) {
  91. losers.add(candidateList.get(can)); //adds losers with the same vote
  92. }
  93. }
  94. return losers; // returns list of candidates with fewest votes
  95. }
  96.  
  97. }
Success #stdin #stdout 0.09s 320320KB
stdin
Standard input is empty
stdout
a votes 0
c votes 2
c votes 2
Election is non decisive.