Write a program to do the following: a. Read the raw vote totals from a data fil
ID: 3761554 • Letter: W
Question
Write a program to do the following:
a. Read the raw vote totals from a data file that contains one row for each precinct.
b. Display the table with appropriate headings for the rows and columns.
c. Compute and display the total number of votes received by each candidate and
the percent of the total votes cast.
d. If any one candidate received over 50% of the votes, the program should print, in the last column, a message declaring that candidate the winner.
e. If no candidate received 50% of the votes, the program should print a message
declaring a run-off between the two candidates receiving the highest number of
votes; the two candidates should be identified by their letter names.
f. For testing, run the program with the above data, and also with another
data file where Candidate C receives only 108 votes in precinct 4.
Data file:
(precinct, candiadateAvotes, , candiadateBvotes, candiadateCvotes, candiadateDvotes
1 192 48 206 37
2 147 90 312 21
3 186 12 121 38
(Using java as the language)
Explanation / Answer
/**The java program that reads a text file contais
* votes and prints the winner among the candidates*/
//Voting.java
import java.io.File;
import java.util.Scanner;
public class Voting
{
public static void main(String[] args)
{
Scanner scanner=null;
//an array to store 3 rows and 4 candidates votes
int[][] votes=new int[3][4];
int totalVotes=0;
try
{
//open a input scanner object
scanner=
new Scanner(new File("votes.txt"));
int row=0;
int col=0;
//read data from the votes.txt file
while(scanner.hasNextLine())
{
int precinct=scanner.nextInt();
votes[row][col]=scanner.nextInt();
votes[row][col+1]=scanner.nextInt();
votes[row][col+2]=scanner.nextInt();
votes[row][col+3]=scanner.nextInt();
row++;
col=0;
}
}
catch (Exception e)
{
System.out.println(e);
}
scanner.close();
//find total votes
totalVotes=getTotalVotes(votes);
//print candidate votes
print(votes);
int maximumVotes=Integer.MIN_VALUE;
char winner=' ';
int winnerId=0;
//print the candidate total votes and percentage of total votes
for (int candidate = 0; candidate < votes.length; candidate++)
{
int candidateVotes=candidateVotes(votes,totalVotes,candidate);
if(candidateVotes>maximumVotes)
{
maximumVotes=candidateVotes;
winnerId=candidate;
}
System.out.printf("%-3d %-2.2f%%",candidateVotes,
((double)candidateVotes/totalVotes)*100.0);
}
//winner of candidates
System.out.printf(" Winner : %c",(char)(65+winnerId));
}
/**Method that prins the votes of candidates*/
private static void print(int[][] votes)
{
for (int row = 0; row < votes.length; row++)
{
for (int col = 0; col < votes[row].length; col++)
{
System.out.printf("%-10d",votes[row][col]);
}
System.out.println();
}
}
//Returns the total number of votes polled in voting
private static int getTotalVotes(int[][] votes)
{
int total=0;
for (int row = 0; row < votes.length; row++)
{
for (int col = 0; col < votes[row].length; col++)
{
total+=votes[row][col];
}
}
return total;
}
//Returns the votes poled for candidateNumber
private static int candidateVotes(int[][] votes,int totalVotes,
int candidtateNumber)
{
int total=0;
for (int row = 0; row < votes.length; row++)
{
total+=votes[row][candidtateNumber];
}
return total;
}
}
----------------------------------
votes.txt
1 192 48 206 37
2 147 90 312 21
3 186 12 121 38
--------------------------------
sample output:
192 48 206 37
147 90 312 21
186 12 121 38
525 37.23%150 10.64%639 45.32%
Winner : C
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.