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.
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...
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...
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...
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
Program should be written in Java a) Write a program that asks the user to enter...
Program should be written in Java a) Write a program that asks the user to enter the approximate current population of India. You should have the computer output a prompt and then YOU (as the user should enter the population.)  For testing purposes you may use the value of 1,382,000,000 from August 2020. Assume that the growth rate is 1.1% per year. Predict and print the predicted population for 2021 and 2022. The printout should include the year and the estimated...
In Java please: 3.39 Lab 6e: CharacterOps Write a program that prompts the user to enter...
In Java please: 3.39 Lab 6e: CharacterOps Write a program that prompts the user to enter their first name. Say hello to the person. Then display whether their name begins with a vowel or consonant. If it is neither, perhaps a special character or a number digit, then print neither a vowel nor a consonant. Example 1: Enter your first name: Barbara Hello, Barbara! The first letter of your name, 'B', is a consonant. Example 2: Enter your first name:...
In Java: Write a nested loop that allows the user to continuously enter phrases and output...
In Java: Write a nested loop that allows the user to continuously enter phrases and output underscores for each character of the phrase. End when the user enters "done" (ignoring case). Once you have this completed, modify your code so that if the character is whitespace, you print a space " " instead of an underscore - Hint: use Character.isWhitespace(YOURCHARHERE) //returns a boolean.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT