Java Reference
In-Depth Information
The definition of this method is:
public static void printResults(String[] cNames,
int [][] vbRegion, int [] tVotes)
{
int largestVotes = 0;
int winLoc = 0;
int sumVotes = 0;
for ( int i = 0; i < tVotes.length; i++)
{
if (largestVotes < tVotes[i])
{
largestVotes = tVotes[i];
winLoc = i;
}
sumVotes = sumVotes + tVotes[i];
System.out.printf("%-11s ", cNames[i]);
for ( int j = 0; j < vbRegion[0].length; j++)
System.out.printf("%6d
", vbRegion[i][j]);
System.out.printf("%5d%n", tVotes[i]);
}
System.out.println("\n\nWinner: " + cNames[winLoc]
+ ", Votes Received: "
+ tVotes[winLoc]);
System.out.println("Total votes polled: " + sumVotes);
}
Suppose that the variables in the method main are:
Main Algorithm:
method main
String[] candidatesName = new String[NO_OF_CANDIDATES]; //array
//to store candidates' names
int [][] votesByRegion =
new int [NO_OF_CANDIDATES][NO_OF_REGIONS]; //array
//to hold voting data by region
int [] totalVotes = new int [NO_OF_CANDIDATES]; //array to hold
//total votes received by
//each candidate
1
4
Scanner inFile; //input file variable
Further suppose that the candidates' names are in the file candData.txt , and the
voting data is in the file voteData.txt .
Search WWH ::




Custom Search