Question

In: Computer Science

Write a java program using the following instructions: Write a program that determines election results. Create...

Write a java program using the following instructions:

Write a program that determines election results. Create two parallel arrays to store the last names of five candidates in a local election and the votes received by each candidate. Prompt the user to input these values. The program should output each candidates name, the votes received by that candidate, the percentage of the total votes received by the candidate, and the total votes cast. Your program should also output the winner of the election. To do this, write the following method: findHighest which receives an array of int. It determine the highest value in the array and returns the location of that value. Call this method to determine the location of the highest value in your votes array, and use this to display the winner from the names array.

SAMPLE OUTPUT:

Enter candidate name: Johnson

Enter votes received: 5000

Enter candidate name: Miller

Enter votes received: 4000

Enter candidate name: Duffy

Enter votes received: 6000

Enter candidate name: Robinson

Enter votes received: 2500

Enter candidate name: Ashton

Enter votes received: 1800

Candidate           Votes Received % of Total

Johnson 5000 25.91

Miller 4000 20.72

Duffy 6000 31.09

Robinson 2500 12.95

Ashton 1800 9.33

Total votes: 19300

The winner of the election is Duffy.

Solutions

Expert Solution

Please give a thumbsup, if this is helpful for you!!

import java.util.Scanner;
import java.text.DecimalFormat;
public class Election_results {
  
   public static int findHighest(int[] arr){
       int max=0,pos=-1;
       for(int i=0;i<arr.length;i++){
           if(arr[i]>max){
               max=arr[i];
               pos=i;
           }
       }
   return pos;
   }
  
   public static void main(String []args ){
       int loc;
       double temp;
       Scanner sc = new Scanner(System.in);
       System.out.print("Enter the length of the array: ");
       int n=sc.nextInt();
       String arr1[]=new String[n];
       int arr2[]=new int[n];
       for(int i=0;i<n;i++){
           System.out.print("Enter candidate name: ");
           arr1[i]=sc.next();
           System.out.print("Enter votes received: ");
           arr2[i]=sc.nextInt();
       }
       int total=0;
       for(int i=0;i<n;i++){
           total+=arr2[i];
       }
       DecimalFormat df = new DecimalFormat("##.##");
       System.out.print("\n\nCandidate\tVotes Received\t% of Total\n");
       for(int i=0;i<n;i++){
           temp= ((double)arr2[i]/(double)total)*100;
           System.out.print(arr1[i]+"\t\t"+arr2[i]+"\t\t"+df.format(temp) +"\n");
       }
       System.out.print("\n\nTotal votes: "+total);
       loc=findHighest(arr2);
       System.out.print("\nThe winner of the election is "+arr1[loc]+".");
   sc.close();
   }
}

Output:


Related Solutions

Instructions: Create a Java program that reads a string entered by the user and then determines...
Instructions: Create a Java program that reads a string entered by the user and then determines and prints how many of each lowercase vowel (a, e, i, o, and u) appear in the entire string. Have a separate counter for each vowel. Also, count and print the number of non-vowel characters. Example: User enters: "This house is beautiful." a: 1 e: 2 i: 3 o: 1 u: 2 non-vowel:10
Write a JAVA program that reads in a string from standard input and determines the following:...
Write a JAVA program that reads in a string from standard input and determines the following: - How many vowels are in the string (FOR THE PURPOSE OF THIS PROGRAM 'Y' is NOT considered a vowel)? - How many upper case characters are in the string? - How many digits are in the string? - How many white space characters are in the string? - Modify the program to indicate which vowel occurs the most. In the case of a...
Write a program in JAVA that takes in two words, and then it recursively determines if...
Write a program in JAVA that takes in two words, and then it recursively determines if the letters of the first word are contained, in any order, in the second word. If the size of the first word is larger than the second then it should automatically return false. Also if both strings are empty then return true. YOU MAY NOT(!!!!!!!!!!): Use any iterative loops. In other words, no for-loops, no while-loops, no do-while-loops, no for-each loops. Use the string...
I am trying to write a java program that determines if an inputted year is a...
I am trying to write a java program that determines if an inputted year is a leap year or not, but I am not able to use if else statements how would I do it. I don't need the code just advice.
Write a program in java that does the following: Create a StudentRecord class that keeps the...
Write a program in java that does the following: Create a StudentRecord class that keeps the following information for a student: first name (String), last name (String), and balance (integer). Provide proper constructor, setter and getter methods. Read the student information (one student per line) from the input file “csc272input.txt”. The information in the file is listed below. You can use it to generate the input file yourself, or use the original input file that is available alone with this...
java program Create a program that creates and prints a random phone number using the following...
java program Create a program that creates and prints a random phone number using the following format: XXX-XXX-XXXX. Make sure your output include the dashes.  Do not let the first three digits contain an 8 or 9 (HINT: do not be more restrictive than that) and make sure that the second set of three digits is not greater than 773. Helpful Hint:   Think though the easiest way to construct the phone number. Each digit does do not have to be determined...
how to create BANKACCOUNT program using Arrays in JAVA.
how to create BANKACCOUNT program using Arrays in JAVA.
Using Linked List, create a Java program that does the following without using LinkedList from the...
Using Linked List, create a Java program that does the following without using LinkedList from the Java Library. and please include methods for each function. Create a menu that contains the following options : 1. Add new node at the end of LL. ( as a METHOD ) 2. Add new node at the beginning of LL. ( as a METHOD ) 3. Delete a node from the end of LL. ( as a METHOD ) 4. Delete a node...
Instructions Write a Java program that asks the user t enter five test scores. The program...
Instructions Write a Java program that asks the user t enter five test scores. The program should display a letter grade for each score and the average test score. Write the following methods in the program: * calcAverage -- This method should accept five test scores as arguments and return the average of the scores. * determineGrade -- This method should accept a test score as an argument and return a letter grade for the score, based on the following...
Write a program that produces the following output using nested for loop in Java. ****** ////////////...
Write a program that produces the following output using nested for loop in Java. ****** //////////// ****** ***** //////////\\ ***** **** ////////\\\\ **** *** //////\\\\\\ *** ** ////\\\\\\\\ ** * //\\\\\\\\\\ * \\\\\\\\\\\\
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT