Java programming I keep having some issues with my program and I\'m trying to ge
ID: 3797305 • Letter: J
Question
Java programming I keep having some issues with my program and I'm trying to get it how my teacher wants it. I've already modified the program from the answers I have gotten from chegg already can you help with this program?
These are the instructions:
java programming 5th edition chapter 9 Parallel Arrays
Write a program that allows the user to enter the last names of five candidites in a local election and the votes received by each candidate. The program should then output each candidate's name, the votes receive by that candidate, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. Make sure your output is in table format. A sample output is:
Candidate Votes Received % of Total Votes
_____________________________________________
Johnson 5000 25.91
Miller 4000 20.73
Duffy 6000 31.09
Robinson 2500 12.95
Ashtony 1800 9.33
__________________________________________________
Total 19300
The Winner of the Election is Duffy
This is the Program
import java.util.*;
public class CandidateVotes
{
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args)
{
//Declare variables
String winnerName = “ “;
int winnerVotes = 0;
int total = 0;
double percentage;
//Declare Parallel Arrays
int[] votes = new int[5];
String[] names = new String[5];
for(int i = 0; i < winnerNames.length(); i = i + 1)
{
System.out.print(“ Enter name of the Cadidate: “);
names[i] = keyboard.nextLine();
System.out.println();
System.out.print(“ Enter number of votes received: “);
Votes[i] = keyboard.nextInt();
}//end for
System.out.println(“ Candidate Votes Received % of Total Votes”);
System.out.println(“______________________________________________”);
//Calculate Total Votes
for(int i = 0; i < votes.length; i++)
{
total+= votes[i];
}//end for
//Loop to find winner
for(int i = 0; i < names.length; i++)
{
//If number of votes greater than winner votes
if(votes[i] > winnerVotes)
{
winnerVotes = votes[i]; //Change votes
winnerName = names[i]; // Change name
}//end if
//Calculate the percentage of votes
percentage = (double)(votes[i] / total) * 100;
System.out.println(names[i] + “ “ +(int)votes[i] + “ “ + percentage);
}//end for
System.out.println(“_____________________________________________”);
System.out.println(“Total “ + total);
System.out.println();
System.out.println(“The winner of the election is “ + winnerName);
}//end main
}//end class
Explanation / Answer
// CandidateVotes.java
import java.util.*;
public class CandidateVotes
{
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args)
{
//Declare variables
String winnerName = " ";
int winnerVotes = 0;
int total = 0;
double[] percentage = new double[5];
int[] votes = new int[5];
String[] names = new String[5];
for(int i = 0; i < names.length; i = i + 1)
{
System.out.print(" Enter name of the Cadidate: ");
names[i] = keyboard.nextLine();
System.out.print("Enter number of votes received: ");
votes[i] = keyboard.nextInt();
keyboard.nextLine(); // Consume newline left-over
total+= votes[i];
}//end for
System.out.println(" Candidate Votes Received % of Total Votes");
System.out.println("____________________________________________");
//Loop to find winner
for(int i = 0; i < names.length; i++)
{
//If number of votes greater than winner votes
if(votes[i] > winnerVotes)
{
winnerVotes = votes[i]; //Change votes
winnerName = names[i]; // Change name
}//end if
//Calculate the percentage of votes
percentage[i] = ((double)votes[i] / total) * 100;
percentage[i] = Math.round(percentage[i] * 100.0) / 100.0;
System.out.println(names[i] + " " + votes[i] + " " + percentage[i]);
}//end for
System.out.println("_____________________________________________");
System.out.println("Total: " + total);
System.out.println();
System.out.println("The winner of the election is " + winnerName);
}//end main
}//end class
/*
output:
Enter name of the Cadidate: Johnson
Enter number of votes received: 5000
Enter name of the Cadidate: Miller
Enter number of votes received: 4000
Enter name of the Cadidate: Duffy
Enter number of votes received: 6000
Enter name of the Cadidate: Robinson
Enter number of votes received: 2500
Enter name of the Cadidate: Ashtony
Enter number of votes received: 1800
Candidate Votes Received % of Total Votes
____________________________________________
Johnson 5000 25.91
Miller 4000 20.73
Duffy 6000 31.09
Robinson 2500 12.95
Ashtony 1800 9.33
_____________________________________________
Total: 19300
The winner of the election is Duffy
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.