01204212/homework/sorting

จาก Theory Wiki
ไปยังการนำทาง ไปยังการค้นหา
This is part of 01204212

You are given the election result data from the US Election 2012 which Obama won. You have to produce a nice report on the vote for each county, sorted by the relative percentage of votes for Obama.

You program should read the data (in csv format) either by directly reading the file or reading the file from the standard input and produce the report in the format shown below. The csv data contains both vote counts for each county and each state, see the description below on how to throw away state data.

The data

Source: This data is taken from The Guardian.

Download: us-election.csv

Example of the first few lines:

State Postal,County Name,FIPS,Obama vote,Romney vote
AK,Alaska,0,91696,121234
AK,Alaska,2000,91696,121234
AL,Alabama,0,793620,1252453
AL,Autauga,1001,6354,17366
AL,Baldwin,1003,18329,65772
AL,Barbour,1005,5873,5539
AL,Bibb,1007,2200,6131
AL,Blount,1009,2961,20741
AL,Bullock,1011,4058,1250
AL,Butler,1013,4367,5081
AL,Calhoun,1015,15500,30272
AL,Chambers,1017,6853,7596
AL,Cherokee,1019,2126,7494
AL,Chilton,1021,3391,13910

Data format: The file is csv (comma separated values), so each line contains many columns of data separated by commas. The first line contains column labels. The rows where FIPS = 0 are state data, so you should throw them away.

Output

Your program must sort the county data using the relative percentage of Obama votes against all Obama+Romney votes. (This is not the correct percentage as people also voted for other candidates.) Format the output as shown below. (See the additional info section for how to use string formatter in Java.)

Sample

County                                     Obama     Romney   Obama%   Romney%
-------------------------------------------------------------------------------
Shannon (SD)                                2922        188   93.955     6.045
District of Columbia (DC)                 222332      17337   92.766     7.234
Penobscot Nation Vot Dst (ME)                253         23   91.667     8.333
Bronx (NY)                                288378      26304   91.641     8.359
Provincetown (MA)                           2121	210   90.991     9.009
Prince George's (MD)                      317342      32236   90.779     9.221
Monhegan Plt. (ME)                            49          5   90.741     9.259
Isle Au Haut (ME)                             48          5   90.566     9.434
Petersburg (VA)                            14377       1526   90.404     9.596
Indian Township Vtng Dst (ME)                208         25   89.270    10.730
Jefferson (MS)                              3508        431   89.058    10.942
Baltimore City (MD)                       201042      25501   88.743    11.257

Additional info

  • String formatting in Java: Tutorial, API
  • How to submit this homework
    • send an email to me with all your java files attached. Use "01204212 homework 3 sorting (58xxxxxxxx)" as the subject (replace xxx with your id).