You do not need to submit this lab, BUT TRY TO FINISH IT ON YOUR OWN ANYWAY. Three different solutions will be posted on Friday, November 25.
Write a program that counts the number of gold, silver, and bronze medals won
by each country in the Olympic Games. Your program should read the input from
a file (see program at the bottom of the page).
Assume that the input is line based, with the results for an event given as a comma-delimited list of fields:
men's curling, Canada, Finland, USA
where the first field is the event name, the second field is the gold medal winning country,
the third field is the silver medal winning country, and the fourth field is the bronze medal
winning country (note that this problem ignores the case where a tie has occurred).
Your program should output the medal count for each country in order of the number of gold medals won. If there is tie for the number of gold medals won, the countries that tied should be listed in alphabetic order.
Sample Input: (based on the Turin 2006 Winter Olympics)
men's curling, Canada, Finland, USA women's curling, Sweden, Switzerland, Canada men's ice hockey, Sweden, Finland, Czech Republic women's ice hockey, Canada, Sweden, USA men's skeleton, Canada, Canada, Switzerland women's skeleton, Switzerland, Great Britain, Canada men's luge, Italy, Russia, Latvia women's luge, Germany, Germany, Germany mixed luge, Austria, Germany, Italy men's 4 x 7.5 km relay biathlon, Germany, Russia, France men's 12.5 km pursuit biathlon, France, Norway, Germany men's 20 km individual biathlon, Germany, Norway, Norway women's 4 x 6 km relay biathlon, Russia, Germany, France women's 10 km pursuit biathlon, Germany, Germany, Russia women's 15 km individual biathlon, Russia, Germany, Russia men's halfpipe snowboarding, USA, USA, Finland women's halfpipe snowboarding, USA, USA, Norway some made up event, Canada, Bosnia and Herzegovina, Serbia and Montenegro
Sample output: Your program should output the country name using 25 spaces,
and each number of medals using 3 spaces.
Canada 4 1 2 Germany 4 5 2 Russia 2 2 2 Sweden 2 1 0 USA 2 2 2 Austria 1 0 0 France 1 0 2 Italy 1 0 1 Switzerland 1 1 1 Bosnia and Herzegovina 0 1 0 Czech Republic 0 0 1 Finland 0 2 1 Great Britain 0 1 0 Latvia 0 0 1 Norway 0 2 2 Serbia and Montenegro 0 0 1
Map<String, List<Integer>>
where the country name is mapped to a list of medal counts (each list has size
3, and element 0 holds the number of gold medals won, element 1 holds the number of silver medals won,
and element 2 holds the number of bronze medals won); (2) use three separate
Map<String, Integer>
s
where the first map maps the country name to the number of gold medals won,
the second map maps the country name to the number of silver medals won, and
the third map maps the country name to the number of bronze medals won.
TreeSet
of the number of gold medals won;
this generates a set sorted in ascending order. Then use the
descendingSet
method in TreeSet
to
produce a new set sorted in descending order.
printf
to format the output.
import java.util.*; import java.io.*; public class MedalCount { public static void main(String[] args) throws IOException { PrintStream out = System.out; // read the input using a scanner final String INPUT_NAME = "/cse/course/1020/medals.txt"; Scanner in = new Scanner(new File(INPUT_NAME)); // YOUR CODE GOES HERE TO DECLARE SOME COLLECTIONS while (in.hasNextLine()) { String line = in.nextLine(); if (line.isEmpty()) { continue; } // YOUR CODE GOES HERE TO READ IN THE FILE // AND POPULATE THE COLLECTIONS } // YOUR CODE GOES HERE TO OUTPUT THE RESULTS } }