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

JAVA- USE PARALLEL ARRAYS Write a java program that allows the user to enter the

ID: 3669835 • Letter: J

Question

JAVA- USE PARALLEL ARRAYS

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:

Enter candidate' s name and the votes received by the candidate Andrea 900 David 350 Charles 4500 Susan 2200 Dan 5600 Mary 750 Rita 1250 Candidate David Mary Andrea Rita Susan Charles Dan Votes Received 350 750 900 1250 2200 4500 5600 2.25 4·82 5.79 8.04 14.15 28.94 36.01 rotal The Winner f the Elect! n 13 Dan . 15550O

Explanation / Answer

import java.util.*;
import java.text.NumberFormat;
public class Election
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
String[] names = new String [5];
short []votes = new short[5];
for(short i = 0; i < 5; i++)
{
Exception exp = null;
do
{
try
{
exp = null;
System.out.print("Enter the name of the Candidate: " + (i+1) + "");
names[i] = console.next();
System.out.print("Enter the number of votes received: " + (i+1) + "");
votes[i] = console.nextShort();
}catch(Exception e){exp = e;}
}
while(names[i] == null || exp != null);
}
String winner = "";
short winnerVotes = 0;
short totalVotes = 0;
short j;
for (j = 0; j< votes.length; j++)
{
totalVotes += votes[j];
}
NumberFormat formatter = NumberFormat.getNumberInstance();
formatter.setMinimumIntegerDigits(1);
formatter.setMaximumFractionDigits(2);
System.out.println(" Candidate Votes Received % of Total Votes");
System.out.println("--------- -------------- ----------------");
for (j = 0; j< names.length; j++)
{
if (votes[j]>winnerVotes)
{
winner = names[j];
winnerVotes = votes[j];
}
System.out.println(names[j] + " " + (int)votes[j] + " " + (formatter.format(((double)votes[j]/totalVotes)*100)));
}
System.out.println("Total " + totalVotes);
System.out.println("The winner of the election is " + winner);
}
}