Part A Problem and Program Details As a parttime blog writer
Part A
Problem and Program Details:
As a part-time blog writer for a major sports network you are tasked with providing a readerfriendly summary of the most recent Winter Olympics. This will be an integral part of an article that will be used to discuss the next Olympic Games. Given detailed results from the Winter Olympics, which were provided to you by your supervisor, you are asked to summarize the medal standing for all participating countries. Your input data, which is provided in the olympics.mat file that can be downloaded from the eClass/Moodle, includes the following variables:
- countries, which lists countries that scored medals. The countries are coded using three letter abbreviations, e.g., CAN stands for Canada. We use country code XXX to denote a case where two countries received the same medal (silver) in the same sport, i.e., there was a tie.
- gold, silver and bronze, which list the countries that received gold, silver, and bronze medals, respectively. Note that the gold, silver and bronze variables list the data in the same order. This means that the i th row in these variables gives the countries that received the corresponding three types of medals for the same sport.
Your objective is to convert the results per sport (from the olympics.mat file) into a table that lists medal counts and total medal tally per country where countries are sorted alphabetically. Note that countries variable already lists the countries alphabetically. You should also show the best performing country (or countries in case of a tie), when scored on the total medal tally and the number of gold medals. Moreover, you are required to list the countries that have 20 or more medals. The results should be printed to the Command Window using a specific format shown below.
Part B
There will be no submission of a file, we will be further testing the functionality of your program with another version of the olympics.mat file which contains more data that the initial version. Please download the olymics.mat file from eClass under assignment 4B and re-run your solution.
Code Requirements:
1. You must use three sub-functions: the first that will compute number of gold, silver, and bronze medals per country; the second that will print the table with medal standings; and the third that will compute and print the best performing countries. The third sub-function should be called three times; each time for a different criteria of performance (most medals, most gold medals, and at least 20 medals).
2. Your primary/main function assignment4 should have the following outline: function [] = assign4_()
%load olympics.mat file load(\'olympics.mat\');
% compute medals for each country using a sub-function that is
% called using a loop over all countries
% display medal counts for all countries using a sub-function
% display countries with most medals, most gold medals, >=20 medals
% using a sub-function, the same sub-function should be used 3 times end
%this end terminates the function
3. You must use the fprintf statement to display your results to the command window.
4. Do not use the table function to display your results.
Hints
1. In Matlab, load the olympics.mat and see how the data is stored.
2. Do not display results for the XXX “country”, which is included in the olympics.mat file. It may be more straightforward to calculate the medal totals for the XXX \"country\" initially, and when displaying the results exclude it from being displayed.
3. Note that it is possible that there is more than one country with the highest number of all medals, or highest number of gold medals, or at least 20 medals. In that case, you code should list all of these countries.
4. In the third sub-function, you may want to use switch statement to select between different criteria.
5. You must duplicate the shown below output, including spacing and formatting, and provide the data for the remaining countries. The “...” denotes the part of the report that you have to fill in.
Country Gold Silver Bronze Total
AUS 2 1 0 3
AUT 4 6 6 16
...
Countries with the most medals: ...
Countries with the most gold medals: ...
Countries with at least 20 medals: CAN GER : ...
Solution
public class country{
private static String countryName;
private static String gold;
private static String silver;
private static String bronze;
private static String totalMedals;
public country(String countryName, String gold, String silver, String bronze, String totalMedals)
{
this.countryName=countryName;
this.gold=gold;
this.silver=silver;
this. bronze = bronze;
this. totalMedals = totalMedals;
}
public static String getName()
{
return countryName;
}
public static String getGold()
{
return gold;
}
public static String getSilver()
{
return silver;
}
public static String getBronze()
{
return bronze;
}
public static String getTotalMedals()
{
return totalMedals;
}
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import java.io.*;
import java.util.*;
import java.io.PrintWriter;
public class medalTally
{
ArrayList<country> cntry;
public medalTally()
{
cntry = new ArrayList<country>();
}
public void addEntry(country x)
{
cntry.add(x); // adding object x of the person class to the arrayList
}
public void write() throws java.io.IOException
{
FileWriter fw = new FileWriter(\"London2012.txt\", true);
PrintWriter write = new PrintWriter(fw);
for(int index =0; index < cntry.size(); index++)
{
country x = cntry.get(index);
write.println(x.getName()+\" \"+x.getGold()+\" \"+x.getSilver()+\" \"+x.getBronze()+\" \"+x.getTotalMedals());
write.close();
}
}
}
[code=java]
import java.io.IOException;
import java.util.Scanner;
public class londonGames{
private static String name;
private static String gold;
private static String silver;
private static String bronze;
private static String totalMedals;
public static void main(String[] args) throws IOException
{
Scanner in = new Scanner(System.in);
londonGames tally = new londonGames();
int choice =1;
while(choice >=1 && choice <=6)
{
printOptions();
System.out.println(\"Enter choice >>\");
choice = in.nextInt();
if(choice==1)
{
int subchoice=1;
System.out.println(\"1. Add Country\");
System.out.println(\"\\t\" +\"Add Menu\");
System.out.println(\"\\t\" +\"1. Country Name\");
System.out.println(\"\\t\" +\"2. Exit\");
System.out.println(\"Enter choice >>\");
subchoice = in.nextInt();
if (subchoice==1)
{
System.out.println(\"Enter the name of the country to be added\");
name=in.next();
tally.addEntry(new country(name, \"0\", \"0\", \"0\", \"0\"));
//.addEntry(new country(name));
tally.write();
System.out.println(\"Country has been successfully added to medal tally\");
}
if(choice==2)
{
}
// name = in.next();
// System.out.println(\"Enter phone\");
// gold = in.next();
// System.out.println(\"Enter phone\");
// silver = in.next();
// System.out.println(\"Enter phone\");
// bronze = in.next();
// totalMedals=(gold+silver+bronze);
// tally.addEntry(new country(name,gold, silver, bronze, totalMedals));
// tally.write();
}
if(choice==2)
{
}
if(choice==3)
{
}
if(choice==4)
{
}
if(choice==5)
{
}
if(choice==6) // exit the program and also write all the changes to the text file
{
System.out.println(\"See you later\");
tally.write();
System.exit(0);
}
}
System.out.println(\"This choice doesn\'t exist\");
}
private void addEntry(country country) {
// TODO Auto-generated method stub
}
private void write() {
// TODO Auto-generated method stub
}
public static void printOptions()
{
System.out.println(\"Welcome to the London Olympic Games Medal Tally Main Menu\");
System.out.println(\"1. Add Country\");
System.out.println(\"2. Delete Country\");
System.out.println(\"3. Add Medal(s)\");
System.out.println(\"4. Display Medal Tally\");
System.out.println(\"5. Save to file\");
System.out.println(\"6. Exit\");
}
}
| public class country{ private static String countryName; private static String gold; private static String silver; private static String bronze; private static String totalMedals; public country(String countryName, String gold, String silver, String bronze, String totalMedals) { this.countryName=countryName; this.gold=gold; this.silver=silver; this. bronze = bronze; this. totalMedals = totalMedals; } public static String getName() { return countryName; } public static String getGold() { return gold; } public static String getSilver() { return silver; } public static String getBronze() { return bronze; } public static String getTotalMedals() { return totalMedals; } } |











