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

Write a java program that allows the user to enter the last names of five candid

ID: 3642019 • Letter: W

Question

Write a java program that allows the user to enter the last names of five candidates in a local election and the votes received by each candidate. The program should then output each candidate's name, the votes received by that candidate, and the percetage of the total votes received by the candidate. Your program should also output the winne f the election. A sample output is:

Candidate Votes Received % of Total Votes
Johnson 5000 25.91
Miller 4000 20.72
Duffy 6000 31.09
Robinson 2500 12.95
Ashtony 1800 9.33
Total 19300
The winer of the Election is Duffy.

Explanation / Answer

import java.text.DecimalFormat;

import java.util.Scanner;

public class Candidates {

       public static void main(String[] args) {

              Scanner in = new Scanner(System.in);

              String names[] = new String[5];

              int votes[] = new int[5];

              double total = 0;

              int winner = 0;

              DecimalFormat d = new DecimalFormat("0.00");

              for(int i = 0; i < 5; ++i) {

                     System.out.print("Enter the name of a candidate: ");

                     names[i] = in.next();

                     System.out.print("Enter the number of votes " + names[i] + " received: ");

                     votes[i] = in.nextInt();

                     total += votes[i];

                     if(i > 0) {

                           if(votes[i] > votes[i-1]) {

                                  winner = i;

                           }

                     }

              }

              System.out.println("Candidate Votes Received % of Total Votes");

              for(int i = 0; i < 5; ++i) {

                     System.out.println(names[i] + " " + votes[i] + " " + d.format(100 * votes[i]/total));

              }

              System.out.println("Total: " + (int) total);

              System.out.println("The winner of the Election is " + names[winner] + ".");

       }

}

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