Write a program using the bucket sort idea to create histograms of an election.
ID: 3641480 • Letter: W
Question
Write a program using the bucket sort idea to create histograms of an election.
•The input data are in an external file named election.txt. The first line is an integer N representing the number of candidates. The next N lines are the names of the canditates. The remaining lines are individual votes, each recorded as an integer from 0 to N-1 representing the number of one of the candidates, as in election.txt, below.
•The output for the above file should look like
Election Results
================
Albert *****
Ford ***
Harris ******
Jones ********
Smith ************
•For input from election.txt, put the following in a try{} block, and import java.io.*;
Scanner sc = new Scanner(new FileReader("election.txt"));
•If the Scanner is declared in a block, it cannot be referenced outside of the block.
•Remember, if sc is your Scanner, then you should always use sc.nextLine(); after each call to sc.nextInt() to clear the input buffer.
•You may assume that the input file is in the correct format.
Election.txt is listed below:
5
Albert
Ford
Harris
Jones
Smith
0
3
3
4
2
0
3
4
1
1
2
3
4
4
4
4
4
4
0
0
2
3
2
4
4
3
0
4
3
1
4
0
3
2
2
The code must be completed how i asked to get full points. The code must include a comment for each method describing the method, a comment for each parameter describing what the parameter represents, and a comment for each variable declaration (including instance variables) describing what the variable represents.
Explanation / Answer
import java.io.*; public class Histogramm { static String file = "Path/to/your/textfile"; static String can[]; static int lengthOfFile; public static void readFile() throws IOException // throws this exception if file is not found; { int i = 0; can = new String[500]; try { BufferedReader in = new BufferedReader(new FileReader(file)); String readLine; while((readLine = in.readLine()) != null) { //first line is number of candidates //next N lines are the names of the candidates //next lines until end of file are votes can[i] = readLine; i++; } in.close(); // close the file that we just finished reading from. } catch (FileNotFoundException e) { e.printStackTrace(); } lengthOfFile = i; // to keep track of how long the file was } public static void countResults() { int getVote; // holds the vote read from the array int numCan = Integer.parseInt(can[0]); //get the number of candidates int votes[] = new int [numCan]; // the buckets for(int i = numCan+1;iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.