Tuesday, November 2, 2004
The file ballots contains real election data (almost 19,000 ballots) from the 1999 City Council Election in Cambridge, MA. Your job is to write a program using hash table and priority queue data structures to count and sort the number of votes each candidate received.
First, you will need to determine the number of votes each candidate received. For this purpose, you can use a hash table that stores objects mapping a candidates name (the key) to the number of votes they have received. As you read names from the file, access the objects in the hash table and increment the appropriate vote count. Notice, you have to make sure that uppercase/lowercase differences in names in the ballots file should be handled properly.
Once you have got the votes counted in your hash table, you need to sort them and determine the top 9 names who received the most votes. For this purpose, you can insert all the data into a priority queue and then remove the minimum element from the priority queue 9 times.
You may use the HashTable
and
HeapPriorityQueue
classes provided with the textbook
code.
Reading data from a file using Java involves the following steps:
import java.io.*
at the top
of your source file
BufferedReader inFile = new BufferedReader(new FileReader("ballots"));
readLine()
method:
String line = inFile.readLine();When you reach the end of the file,
readLine()
will
return null
.
inFile.close();
Hand in a printout of all the commented Java program files that are necessary to make your program run correctly, and also submit them online through VikingWeb. If you want, you can bundle all the files up into a zip archive so that you only have to upload one file to VikingWeb.