JAVA- USE PARALLEL ARRAYS Write a java program that allows the user to enter the
ID: 3669616 • 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 % of Total Votes 350 750 900 1250 2200 4500 5600 2.25 4·82 5.79 8·04 14.15 28.94 36.01 Total The winner of the Election is Dan 15550Explanation / Answer
import java.util.Scanner;
public class A7_Ex1 {
public static void main(String[] args) {
String[] names = new String[7];
int[] votes = new int[7];
double percentages[] = new double[7];
Scanner sc =new Scanner(System.in);
int totalVote = 0;
int max = 0;
int max_index = 0;
for(int i=0; i<7; i++){
System.out.println("Enter last name of "+(i+1)+" candidate: ");
names[i] = sc.next();
System.out.println("Enter votes of "+(i+1)+" candidate: ");
votes[i] = sc.nextInt();
if(max < votes[i]){ // maintaining candidate index in array with maximum voted
max= votes[i];
max_index = i;
}
totalVote+=votes[i]; // total votes
}
for(int i=0; i<7; i++){
percentages[i] = ((double)votes[i]/totalVote)*100;
}
System.out.println("Candidate Votes Received % of Total votes");
for(int i=0; i<7; i++){
System.out.println(names[i]+" "+votes[i]+" "+percentages[i]+"");
}
System.out.println("Total "+totalVote);
System.out.println("Winner of the game is "+names[max_index]);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.