/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
private List<String> ballotList;
{
List<String> list = new ArrayList<String>();
list.add("a");
list.add("c");
list.add("c");
Ideone i = new Ideone(list);
{
System.
out.
println(s
+ " votes " + i.
numFirstVotes(s,list
)); }
System.
out.
println(i.
getWinner(list
)); }
public Ideone(List<String> ballotList)
{
this.ballotList = ballotList;
}
public int numFirstVotes
(String s, List
<String
> ss
) {
return (int) s.charAt(0) - 'a';
}
/**
* Returns the winner of the election using the candidatesWithFewest()
* method.If there is no winner method returns a statement stating the
* election is not decisive.
*
* @param vbal VoterBallots object
* @param candList a list of candidate names
* @return the winner of the election
*/
public String getWinner
(List
<String
> candList
) { // Run rounds until down to a single candidate
while (candList.size() > 1) {
ArrayList<String> loser = candidatesWithFewest(candList);
listString += s;
candList.remove(s);
}
}
if (candList.size() > 0) {
return candList.iterator().next(); // Return the surviving candidate
} else {
return "Election is non decisive.";
}
}
/**
* Returns a list of one or more candidates tied with the fewest
* first choice votes
*
* Precondition: each String in candidateList appears exactly once
* in each Ballot in ballotList
*
* @param candidateList a list of candidate names
*
* @return a list of those candidates tied with the fewest first
* choice votes
*/
public ArrayList<String> candidatesWithFewest(List<String> candidateList) {
ArrayList<String> losers = new ArrayList<String>(); //empty list for losers
int minTally = ballotList.size() + 1; //number of min votes
for (int can = 0; can < candidateList.size(); can++) {
String candidate
= candidateList.
get(can
); // // number of first place votes
int votes = numFirstVotes(candidate, candidateList);
if (votes < minTally) {
minTally = votes;
losers = new ArrayList<String>(); // adds loser to list
}
if (votes == minTally) {
losers.add(candidateList.get(can)); //adds losers with the same vote
}
}
return losers; // returns list of candidates with fewest votes
}
}