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

Write a program in JAVA that prompts for an integer N, and allows the user to en

ID: 3752291 • Letter: W

Question

Write a program in JAVA that prompts for an integer N, and allows the user to enter the last names of N candidates in a local election and the number of votes received by each candidate.
The program then outputs each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate.
The program also outputs the winner of the election.

(1) Prompt the user to input N candidates and the number of votes, then display the inputs

Enter an integer: You entered: 4 Enter 4 names and numbers: You entered: King 5000 Harris 3500 Jones 6000 Duffy 1900

Calculate the total votes received and percentage of received by each candidate

Candidate Votes Received % of Total Votes King 5000 30.49 Harris 3500 21.34 Jones 6000 36.59 Duffy 1900 11.59 Total 16400

Fine the Winner

The Winner is Jones.

Explanation / Answer

Election.java

import java.util.Scanner;

public class Election {

public static void main(String[] args) {

//Declaring variables

int n, totalVotes = 0;

/*

* Creating an Scanner class object which is used to get the inputs

* entered by the user

*/

Scanner sc = new Scanner(System.in);

//Getting the input entered by the user

System.out.print("Enter an integer:");

n = sc.nextInt();

//Declaring and initializing an array

String candidates[] = new String[n];

int votes[] = new int[n];

double percentages[] = new double[n];

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

System.out.print("Enter candidate name and number of votes :");

candidates[i] = sc.next();

votes[i] = sc.nextInt();

totalVotes += votes[i];

}

int winnerIndex = findWinner(votes);

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

percentages[i] = (((double)(votes[i])) / totalVotes) * 100;

}

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

System.out.println(" -------- ------------ --------------");

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

System.out.printf("%-10s %d %.2f ", candidates[i], votes[i], percentages[i]);

}

System.out.println("Total:" + totalVotes);

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

}

//This function will find the winner

private static int findWinner(int[] votes) {

int winnerIndex = 0;

int max = votes[0];

for (int i = 0; i < votes.length; i++) {

if (max < votes[i]) {

max = votes[i];

winnerIndex = i;

}

}

return winnerIndex;

}

}

__________________

Output:

Enter an integer:4

Enter candidate name and number of votes :King 5000

Enter candidate name and number of votes :Harris 3500

Enter candidate name and number of votes :Jones 6000

Enter candidate name and number of votes :Duffy 1900

Candidate Votes Received % of Total Votes

King 5000 30.49

Harris 3500 21.34

Jones 6000 36.59

Duffy 1900 11.59

Total:16400

The winner of the Election is Jones

_________Could you plz rate me well.Thank You

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