Question

In: Computer Science

Please write the code JAVA Write a program that allows the user to enter the last...

Please write the code JAVA

Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. 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.

Solutions

Expert Solution

I have uploaded the Images of the code, Typed code and Output of the Code. I have provided explanation using comments(read them for better understanding).

Images of the Code:

Note: If the below code is missing indentation please refer code Images

Typed Code:

//importing package
import java.util.Scanner;
public class Main
{
   public static void main(String[] args)
   {
   //n is used to store the no. of candidates
   int n;
   Scanner sc = new Scanner(System.in);
   System.out.print("Enter no.of candidates: ");
   //asking the user to input the no. of candidates
   n = sc.nextInt();
   //using three arrays to store names, votes, percentage
       String name[] = new String[n];
       int votes[] = new int[n];
       float per[] = new float[n];
       //total votes will be stored in totalvotes
       float totalvotes = 0;
       //declaring two variables
       int max = 0,a = 0;
       //for loop will iterate n times
       System.out.println();
       for (int i = 0; i < n; i++)
       {
       Scanner scnr = new Scanner(System.in);
       System.out.print("Enter candidate last name: ");
       //asking the user to enter name of the candidate
name[i] = scnr.nextLine();
System.out.print("Enter no. of votes received for this candidate: ");
//asking the user to enter no. of votes they got
votes[i] = scnr.nextInt();
//adding total votes and storing it in totalvotes
totalvotes = totalvotes + votes[i];
System.out.println();
       }
       //for loop will iterate n times
       for(int j = 0; j < n; j++)
       {
       //calculating percentage and storing it in per[]
       per[j] = votes[j]*100/totalvotes;
       }
       System.out.println("----------------------------------------------------------------");
       System.out.println("Name\t\t Votes Received\t\t % of Total Votes");
       System.out.println("----------------------------------------------------------------");
       //for loop will iterate n times
       for(int k = 0; k < n; k++)
       {
       //printing name, votes, percentage of each candidate
       System.out.printf(name[k]+"\t\t\t"+votes[k]+"\t\t\t"+"%.2f",per[k]);
       System.out.println();
       //to check that who is winner
       //initially max = 0 if votes> max then
       if(max<votes[k])
       {
       //that votes will store in max
       max = votes[k];
       //index value of that votes is stored in a
       a = k;
       }
       }
       System.out.println("----------------------------------------------------------------");
       //printing totalvotes
       System.out.println("Total\t\t\t"+totalvotes);
       System.out.println("----------------------------------------------------------------");
       //printing the winner name using index value stored in a
       System.out.println("The Winner of the Election is "+name[a]);
      
   }
}
//code ended here

Output:

If You Have Any Doubts. Please Ask Using Comments.

Have A Great Day!


Related Solutions

C++ Vector Write a program that allows the user to enter the last names of the...
C++ Vector Write a program that allows the user to enter the last names of the candidates in a local election and the votes received by each candidate. The program should then output each candidate's name, votes received by that candidate, and the percentage of the total votes received by the candidate. Assume a user enters a candidate's name more than once and assume that two or more candidates receive the same number of votes. Your program should output the...
Write Java code that allows a user to repeatedly enter numbers. Each time the user enters...
Write Java code that allows a user to repeatedly enter numbers. Each time the user enters a number, the program should print out the average of the last 3 numbers (or all numbers if 3 or less have been entered). I would like a detailed explanation so that a beginner level Java programmer could understand.
Write a C++ program using dynamic arrays that allows the user to enter the last names...
Write a C++ program using dynamic arrays that allows the user to enter the last names of the candidates in a local election and the number of votes received by each candidate. The program must ask the user for the number of candidates and then create the appropriate arrays to hold the data. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should...
Please write in Python code please: Write a program that asks the user to enter 5...
Please write in Python code please: Write a program that asks the user to enter 5 test scores between 0 and 100. The program should display a letter grade for each score and the average test score. You will need to write the following functions, including main: calc_average – The function should accept a list of 5 test scores as an input argument, and return the average of the scores determine_grade – The function should accept a test score as...
Write out code for a nested if statement that allows a user to enter in a...
Write out code for a nested if statement that allows a user to enter in a product name, store the product into a variable called product name and checks to see if that product exists in your nested if statement. You must include 5 product names to search for. If it is then assign the price of the item to a variable called amount and then print the product name and the cost of the product to the console. If...
Java Program 1. Write a program that asks the user: “Please enter a number (0 to...
Java Program 1. Write a program that asks the user: “Please enter a number (0 to exit)”. Your program shall accept integers from the user (positive or negative), however, if the user enters 0 then your program shall terminate immediately. After the loop is terminated, return the total sum of all the previous numbers the user entered. a. What is considered to be the body of the loop? b. What is considered the control variable? c. What is considered to...
Python Code: Write a program to prompt the user to enter a first name, last name,...
Python Code: Write a program to prompt the user to enter a first name, last name, student ID, number of hours completed and GPA for 5 students. Use a loop to accept the data listed below. Save the records in a text file. Write a second program that reads the records from your new text file, then determines if the student qualifies for Dean’s list or probation. Display the records read from the file on screen with appropriate headings above...
Write a program that runs on SPIM that allows the user to enter the number of...
Write a program that runs on SPIM that allows the user to enter the number of hours, minutes and seconds and then prints out the total time in seconds. Name the source code file “seconds.asm
Write a program that runs on SPIM that allows the user to enter the number of...
Write a program that runs on SPIM that allows the user to enter the number of hours, minutes and seconds and then prints out the total time in seconds. Name the source code file “seconds.asm Explain step by step
Please code C# 8. Write a program that prompts the user to enter an integer. The...
Please code C# 8. Write a program that prompts the user to enter an integer. The program then determines and displays the following: Whether the integer is divisible by 5 and 6 Whether the integer is divisible by 5 or 6
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT