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

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.
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...
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...
8) Create the following program using Java. Circle calculation using methods Create scanner declare double variable...
8) Create the following program using Java. Circle calculation using methods Create scanner declare double variable radius = -999 declare character choice create do while loop inside of do loop write: System.out.println(); System.out.println("*** CIRCLE CALCULATIONS ***"); System.out.println(); System.out.println("1. Enter the radius of the circle"); System.out.println("2. Display the area of the circle"); System.out.println("3. Display the circumference of the circle"); System.out.println("4. Quit"); System.out.println(); System.out.println("Enter a number from 1 - 4"); System.out.println(); Declare choice character and relate to scanner declare switch (choice) case...
3. Write a Java program that loads a gallery.xml file, determines the number of photos (should...
3. Write a Java program that loads a gallery.xml file, determines the number of photos (should be only 3) in it and prints out the number of photos in the gallery.
Write a program in JAVA to create the move set of a Pokémon, and save that...
Write a program in JAVA to create the move set of a Pokémon, and save that move set to a file. This program should do the following: Ask for the pokemon’s name. Ask for the name, min damage, and max damage of 4 different moves. Write the move set data into a file with the pokemon’s name as the filename. The format of the output file is up to you, but keep it as simple as possible
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT