Write a program that allows the user to enter the last namesof five candidates i
ID: 3607891 • Letter: W
Question
Write a program that allows the user to enter the last namesof five candidates in a local election and the votes received byeach candidate. The program should then output each candidate'sname, the votes received by that candidate, and the percentage ofthe total votes received by the candidate. The program should alsooutput the winner of the election. A sample output is: . Candidate VotesReceived %of Total votes . Johnson 5000 25.91 Miller 4000 20.71 Duffy 6000 31.09 Robinson 2500 12.95 Ashtony 1800 12.95 Total 19300 . The winner of the Election is Duffy. Write a program that allows the user to enter the last namesof five candidates in a local election and the votes received byeach candidate. The program should then output each candidate'sname, the votes received by that candidate, and the percentage ofthe total votes received by the candidate. The program should alsooutput the winner of the election. A sample output is: . Candidate VotesReceived %of Total votes . Johnson 5000 25.91 Miller 4000 20.71 Duffy 6000 31.09 Robinson 2500 12.95 Ashtony 1800 12.95 Total 19300 . The winner of the Election is Duffy.Explanation / Answer
import java.util.*; public class Election{ public static void main(String args[]){ String candidate[] = newString[5]; int votes[] = new int[5]; double percent = 0; int i = 0, sum = 0, highest =0, winner = 0; Scanner s = newScanner(System.in); //get input for(i = 0; i < 5;i++){ System.out.print("Enter last name of Candidate " + (i+1) + ":"); candidate[i] = s.next(); System.out.print("Enter number of votes received: "); votes[i] =s.nextInt(); if(votes[i] > highest){ highest = votes[i]; winner = i; }//if sum +=votes[i]; System.out.println(); }//for //generate output System.out.println("Theresults are:"); System.out.println("Candidate Votes Received % of TotalVotes "); for(i = 0; i < 5;i++){ percent =(votes[i]*100)/sum; System.out.println(candidate[i] + " " + votes[i] + " " +percent); }//for System.out.println(" Total: " + sum); System.out.println("Thewinner of the Election is: " + candidate[winner]); }//main }//ElectionRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.