JAVA PROBLEM: The President of the United States is elected by winning the most
ID: 3861210 • Letter: J
Question
JAVA PROBLEM:
The President of the United States is elected by winning the most electoral votes. Each state is allocated a certain number of electoral votes. The candidate with the most votes in a states wins all the electoral votes for that state.
Write a program that uses the file ElectoralVotes.csv and the varous Election Data files to calculate the number of electoral votes earned by each candidate in each election
Your program should build a lookup table with the state names, another table with the associated electoral votes for that state.
The output for each election should show two numbers – the total electoral votes earned by each candidate. Optionally, you could print each state’s name , the winner, and number of electoral votes won.
ElectoralVotes.csv State Idaho Maryland Kentucky Arizona Arkansas California Colorado Alaska Delaware D. C. Illinois Georgia Hawaii Connecticut Minnesota Indiana Iowa Kansas Alabama Maine Florida Massachusetts Michigan Louisiana Mississippi Missouri Montana Nebraska Nevada New Hampshire New Jersey New Mexico New York North Carolina North Dakota Ohio Oklahoma Oregon Pennsylvania Rhode Island South Carolina South Dakota Tennessee Texas Utah Vermont Virginia Washington West Virginia Wisconsin Wyoming Electoral Votes 4 10 8 8 6 54 8 3 3 2 22 13 4 8 10 12 7 6 9 4 25 12 18 9 7 11 3 5 4 4 15 5 33 14 3 21 8 7 23 4 8 3 11 32 5 3 13 11 5 11 3Explanation / Answer
//*******************************************************************
// Dear CompileJava users,
//
// CompileJava has been operating since 2013 completely free. If you
// find this site useful, or would otherwise like to contribute, then
// please consider a donation (link in 'More Info' tab) to support
// development of the new CompileJava website (stay tuned!).
//
// Most sincerely, Z.
//*******************************************************************
import java.lang.Math; // headers MUST be above the first class
// one class needs to have a main() method
public class HelloWorld
{
// arguments are passed using the text field below this editor
public static void main(String[] args)
{
int BushVotes=0;
int KerryVotes=0;
Map<String,Integer> state_seats = new HashMap<String,Integr>();
//read ElectoralVotes.csv
String csvFile = "/Users/mkyong/csv/country.csv";
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
try {
br = new BufferedReader(new FileReader("D://ElectoralVotes.csv"));
while ((line = br.readLine()) != null) {
// use comma as separator
String[] country = line.split(cvsSplitBy);
state_seats.put(country[0],Integer.parseInt(country[1]));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
br.close();
}
try {
//read year wise electiondetails file
br = new BufferedReader(new FileReader("D://Electiondata2004.csv"));
while ((line = br.readLine()) != null) {
// use comma as separator
String[] votes = line.split(cvsSplitBy);
if(votes[1]>votes[2])
{
BushVotes = BushVotes+state_seats.getValue(votes[0]);
System.out.println("Bush won the "+votes[0] +"with "+state_seats.getValue(votes[0]+"electoral votes" );
}
else
{
KerryVotes = KerryVotes+state_seats.getValue(votes[0]);
System.out.println("kerry won the "+votes[0] +"with "+state_seats.getValue(votes[0]+"electoral votes" )
}
} catch (FileNotFoundException e) {
e.printStackTrace();
br.close();
}
System.out.println("Bush electoral votes--"+BushVotes);
System.out.println("kerry electoral votes--"+KerryVotes);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.