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

Election Results using array (JAVA) The results from the mayor\'s race have been

ID: 3811247 • Letter: E

Question

Election Results using array (JAVA)

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 using a two-dimensional array 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.

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

candidateVotes.txt (Save this file under D Drive.Then the path of the file pointing to it is D:\candidateVotes.txt)

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

_____________

ElectionResults.java

import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Scanner;

public class ElectionResults {

   public static void main(String[] args) {
       // DecimalFormat class is used to format the output
       DecimalFormat df = new DecimalFormat("#.##");
      
       // Assigning the file name to the String variable
       String fname = "D://candidateVotes.txt";

       // Creating the Scanner class reference
       Scanner sc = null;
      
       //Create a two dimensional array of name votes
       int votes[][] = new int[5][4];
       int row = 0;
      
       /* This while loop will read the data from the file
       * and populate that data info votes array
       */
       try {
           // Opening the file
           sc = new Scanner(new File(fname));

           // This loops continue to execute until the end of the file
           while (sc.hasNext()) {
               String line = sc.nextLine();
               String strarr[] = line.split(",");
               for (int i = 0; i < 4; i++) {
                   votes[row][i] = Integer.parseInt(strarr[i + 1]);
               }
               row++;

           }
       } catch (IOException e) {
           // Displaying the exception
           System.out.println("Exception :" + e);
       }

       //Displaying the data
       System.out.println("Precinct CandidateA CandidateB CandidateC CandidateD");
       for (int i = 0; i < 5; i++) {
           System.out.print(i + 1 + " ");
           for (int j = 0; j < 4; j++) {
               System.out.print(votes[i][j] + " ");
           }
           System.out.println();

       }
      
       //Displaying the total votes gained by each candidate
       System.out.print("Total: ");
       int tot = 0;
       int sum = 0;
       for (int j = 0; j < votes[0].length; j++) {
           for (int i = 0; i < votes.length; i++) {
               tot += votes[i][j];
               sum += votes[i][j];

           }
           System.out.print(tot + " ");
           tot = 0;
       }
       double percent[] = new double[4];

       //Displaying the percentage of votes gained by each candidate.
       System.out.print(" Percent: ");
       for (int j = 0; j < votes[0].length; j++) {
           tot = 0;
           for (int i = 0; i < votes.length; i++) {
               tot += votes[i][j];
           }
           percent[j] = ((double) tot / sum) * 100;
           System.out.print(df.format(percent[j]) + "% ");
           tot = 0;
       }

       int index1 = 0,index2=0;
       double max = percent[0];
  
       //Getting the which candidate received the maximum votes
       for (int i = 0; i < percent.length; i++) {
               if (max < percent[i])
               {
                   max = percent[i];
                   index1=i;
               }
          
       }
       double second=percent[0];
      
       //Getting the second highest votes received by the candidate
       for(int i=0;i<percent.length;i++)
       {
           if (second<percent[i] && percent[i] != max)
           {
second =percent[i];
index2=i;
           }
       }

       char arr[]={'A','B','C','D'};
      
       /* if any of the candidate votes percentage
       * is greater than 50 the display the winner
       * if not display the runoff between two candidates
       */
if(max>50)
{
   System.out.println(" Candidate "+arr[index1]+" is the winner");
}
else
{
   System.out.println(" Candidate "+arr[index1]+" and Candidate "+arr[index2]+" will have a runoff");
}
   }
}

__________________

Output:

Precinct   CandidateA   CandidateB   CandidateC   CandidateD
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:       906 184 1129 164      

Percent:   38.02% 7.72% 47.38% 6.88%

Candidate C is the winner

_______________________

After changing the data from 408 to 108

candidateVotes.txt (Save this file under D Drive.Then the path of the file pointing to it is D:\candidateVotes.txt)

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

_____________

Output#2:

Precinct   CandidateA   CandidateB   CandidateC   CandidateD
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:       906 184 1129 164      
Percent:   38.02% 7.72% 47.38% 6.88%      

Candidate C and Candidate A will have a runoff

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