the result from the mayor\'s race have been reported I need this written in Java
ID: 3811049 • Letter: T
Question
the result from the mayor's race have been reportedI need this written in Java Election Results The results from the mayor's race have been reported by each precinct as follows: Candidate Candidate Candidate Candidate Winner Runoff Precinct 192 48 206 37 147 90 312 21 186 12 121 114 21 39 408 267 13 382 29 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 92,48,206,37 2,147 90,312,21 3,186,12 121,38 4,114,21 408.39 5.267 13,382.29
Explanation / Answer
/******************************************************************************
* Name : Sample.java
* Author : sitaram.chhimpa
* Date : Apr 5, 2017
*****************************************************************************/
package sample;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
/**
* TODO: Type Description
*/
public class Sample
{
private static final String FILENAME = "D:\WS\sample\src\sample\input.txt"; // please give local path of your
// input file
public static void main(String[] args)
{
BufferedReader br = null;
FileReader fr = null;
try
{
fr = new FileReader(FILENAME);
br = new BufferedReader(fr);
String sCurrentLine;
br = new BufferedReader(new FileReader(FILENAME));
int i = 0;
List<String> list;
int[] totalVote = new int[4];
int[] votePercent = new int[4];
boolean winner = false;
System.out.println("precinct A B C D");
while ((sCurrentLine = br.readLine()) != null)
{
// StringTokenizer Tok = new StringTokenizer(sCurrentLine);
list = Arrays.asList(sCurrentLine.split(","));
i = 0;
for (i = 0; i < list.size(); i++)
{
System.out.print(list.get(i) + " ");
if (i > 0)
{
totalVote[i - 1] += Integer.parseInt(list.get(i));
}
}
System.out.println("");
}
int TotalVotes = 0;
for (i = 0; i < 4; i++)
{
TotalVotes += totalVote[i];
}
for (i = 0; i < 4; i++)
{
votePercent[i] = totalVote[i] * 100 / TotalVotes;
}
for (i = 0; i < 4; i++)
{
int j = i + 65;
System.out.println("Total Votes of candidate " + (char) j + ": " + totalVote[i]
+ " percent of total vote : " + votePercent[i] + "%");
}
for (i = 0; i < 4; i++)
{
if (votePercent[i] > 50)
{
i = i + 65;
System.out.println("Candidate " + (char) i + " is winner");
winner = true;
break;
}
}
if (winner == false)
{
int highest, secondHighest;
if (totalVote[1] > totalVote[0])
{
highest = 1;
secondHighest = 0;
}
else
{
highest = 0;
secondHighest = 1;
}
for (i = 2; i < 4; i++)
{
if (totalVote[i] > totalVote[highest])
{
secondHighest = highest;
highest = i;
}
else if (totalVote[i] > totalVote[secondHighest])
{
secondHighest = i;
}
}
char first = (char) (highest + 65);
char second = (char) (secondHighest + 65);
System.out.println("Run off between candidate " + first + " and " + second);
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (br != null)
{
br.close();
}
if (fr != null)
{
fr.close();
}
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
}
//Output of fisrt case:
precinct A B C D
1 192 48 206 37
2 147 90 312 21
3 186 12 121 38
4 114 21 408 39
5 267 13 382 29
Total Votes of candidate A: 906 percent of total vote : 33%
Total Votes of candidate B: 184 percent of total vote : 6%
Total Votes of candidate C: 1429 percent of total vote : 53%
Total Votes of candidate D: 164 percent of total vote : 6%
Candidate C is winner
//output of second test Case
precinct A B C D
1 192 48 206 37
2 147 90 312 21
3 186 12 121 38
4 114 21 108 39
5 267 13 382 29
Total Votes of candidate A: 906 percent of total vote : 38%
Total Votes of candidate B: 184 percent of total vote : 7%
Total Votes of candidate C: 1129 percent of total vote : 47%
Total Votes of candidate D: 164 percent of total vote : 6%
Run off between candidate C and A
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.