Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In java Election Results The results from the mayor\'s race have been reported b

ID: 3812520 • Letter: I

Question

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      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

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
   4,114,21,408,39
   5,267,13,382,29

Explanation / Answer

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Driver {

   public static void main(String[] args) {
       try {
           BufferedReader br = new BufferedReader(new FileReader("D:/docs/Chegg/Election/src/datafile.txt"));
       String line = br.readLine();
       List presinctLines = new ArrayList<>();
       for(int i = 0; i < 5; i ++) {
       presinctLines.add(line);
       line = br.readLine();
       }
       br.close();
      
      
       String[][] table = new String[6][7];
      
       table[0][0] = "PRECINCT";
       table[0][1] = "A";
       table[0][2] = "B";
       table[0][3] = "C";
       table[0][4] = "D";
       table[0][5] = "Winner";
       table[0][6] = "RUNOFF";
      
       for(int i = 1; i < 6; i++){
           String lineString = (String) presinctLines.get(i-1);
           String[] vals = lineString.split(",");
          
           for(int j = 0; j < 5; j++){
               table[i][j] = vals[j];
           }
       }
      
       int totalVote = 0;
       int totalA = 0;
       int totalB = 0;
       int totalC = 0;
       int totalD = 0;
      
         
      
      
      
       for(int i = 1; i < 6; i++){
           for(int j = 1; j < 5; j++){
               totalVote = totalVote + Integer.valueOf(table[i][j]);
           }
       }
      
       for(int j = 1; j < 6; j++){
           totalA = totalA + Integer.valueOf(table[j][1]);
           totalB = totalB + Integer.valueOf(table[j][2]);
           totalC = totalC + Integer.valueOf(table[j][3]);
           totalD = totalD + Integer.valueOf(table[j][4]);
       }
      
       int fiftyPercent = totalVote / 2;
      
       if(totalA > fiftyPercent){
           for(int i = 1; i < 6; i++){
               table[i][5] = "A";
           }
       }
       else if(totalB > fiftyPercent){
           for(int i = 1; i < 6; i++){
               table[i][5] = "B";
           }
       }
       else if(totalC > fiftyPercent){
           for(int i = 1; i < 6; i++){
               table[i][5] = "C";
           }
       }
       else if(totalD > fiftyPercent){
           for(int i = 1; i < 6; i++){
               table[i][5] = "D";
           }
       }
       else{
           for(int i = 1; i < 6; i++){
               table[i][5] = "NONE";
           }
           String first = null;
           String second = null;
           int[] sortArr = {totalA, totalB, totalC, totalD};
           Arrays.sort(sortArr);
          
           if(sortArr[3] == totalA){
               first = "A";
           }
           if(sortArr[3] == totalB){
               first = "B";
           }
           if(sortArr[3] == totalC){
               first = "C";
           }
           if(sortArr[3] == totalD){
               first = "D";
           }
          
           if(sortArr[2] == totalA){
               second = "A";
           }
           if(sortArr[2] == totalB){
               second = "B";
           }
           if(sortArr[2] == totalC){
               second = "C";
           }
           if(sortArr[2] == totalD){
               second = "D";
           }
          
           for(int i = 1; i < 6; i++){
               table[i][6] = first + " and " + second;
           }
          
       }
      
       for(int j = 0; j < 7; j++){
           System.out.print(table[0][j]);
           if(j != 0)
               System.out.print(" ");
       }
       System.out.println();
      
       for(int i = 1; i < 6; i++){         
           for(int j = 0; j < 7; j++){
               System.out.print(table[i][j]);
               System.out.print(" ");
           }
           System.out.println();
       }
      
       } catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
      

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote