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

JAVA- USE PARALLEL ARRAYS - DO NOT CREATE A CLASS Write a java program that allo

ID: 3670126 • Letter: J

Question

JAVA- USE PARALLEL ARRAYS - DO NOT CREATE A CLASS

Write a java program that allows the user to enter the last names of 7 candidates in a local election and the votes received by each candidate. Save file as A7_Ex1.java.

Your program should use the Selection sort algorithm to sort the list in ascending order based on the votes received by each candidate.Note: You must sort row by row.

The program should then output

Candidate last name, the votes received by that candidate, and the percentage of total votes received by the candidate.

Then it should output the total number of votes cast and the winner of the election.

****** Don't create a class for this exercise and then code the program. You must create a String array, int array and a double array. Then get the inputs from user into the String and int arrays, calculate the percentage and store in double array, then sort (row by row) using selection sort and print the result .

THis should be what the ouput looks like:

Explanation / Answer

import java.util.*;
import java.lang.*;
import java.io.*;


class Codechef
{
   public static void main (String[] args) throws java.lang.Exception
   {
   //
   int numOfCandidates = 7;
   Scanner input = new Scanner(System.in);
   System.out.print("Enter number of students: ");
   //int numOfCandidates = input.nextInt();
   int[] votes = new int[numOfCandidates];
   double[] percentage = new double[numOfCandidates];
   String[] names = new String[numOfCandidates];
   int vote = 0;
   int total = 0;
   String name = "";

   for(int i = 0; i < numOfCandidates; i++){
       name = input.next();
       vote = input.nextInt();
       names[i] = name;
       votes[i] = vote;
       total = total + vote;
       System.out.println("");
   }

   for(int i = 0; i < numOfCandidates; i++){
       percentage[i] = votes[i] * 100.0 /total;
       percentage[i] = (double) Math.round(percentage[i] * 100) / 100;
       System.out.println("");
   }
   for(int i = votes.length - 1; i > 0; i--){
       int currentMax = votes[0];
       int currentMaxIndex = 0;
       for (int k = 1; k <= i; k++){
           if (currentMax < votes[k]){
               currentMax = votes[k];
               currentMaxIndex = k;
           }
       }
       String tempname = names[currentMaxIndex] ;
       names[currentMaxIndex] = names[i];
       names[i] = tempname;
       votes[currentMaxIndex] = votes[i];
       votes[i] = currentMax;
       double temp = percentage[currentMaxIndex];
       percentage[currentMaxIndex] = percentage[i];
       percentage[i] = temp;
      
   }
   for(int i = 0; i < numOfCandidates; i++){
           System.out.printf("%-15s       %5d           %.2f ",names[i],votes[i],percentage[i]);
   }
   System.out.println(" Total = "+total);
   System.out.println("The winner of the election is "+names[6]);
   }
}