• Post Reply Bookmark Topic Watch Topic
  • New Topic

Olympics java coding  RSS feed

 
Saad Mushtaq
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing a program where i have to tally gold medals for each country. The input will come from a file and i have to process the file. The first line is the country code, the second line is event type and third one is event. The input is like this:

CHN
Diving
Women's 10m Platform
CAN
Rowing
Men's Eight
CHN
Rowing
Women's Quadruple Sculls
The expected output should be like this:

Count of gold medallists by country:
CHN - 2
CAN - 1

Count of gold medallists by event type:
Diving - 1
Rowing - 2
Okay so i have created a class of gold medals where it will create a new instance object for each country according to event type. My code is here:


My main method is here:


I have also added a static method which will make sure that we are not creating objects that are already there. (redundant objects)


What i want to know is how can i get the required output. Cause right now i am getting this output:

ITA- 1- Fencing- Women's Individual Foil
POL- 1- Rowing- Men's Quadruple Sculls
KEN- 1- Athletics- Men's Marathon
CHN- 1- Diving- Men's 3m Springboard
TUN- 1- Swimming- Men's 1500m Freestyle
CHN- 1- Canoe/Kayak - Flatwater- Canoe Double (C2) 500m Men
CHN- 1- Diving- Men's Synchronised 3m Springboard
USA- 1- Gymnastics Artistic- Women's Individual All-Around
RUS- 1- Synchronized Swimming- Duet
which is what i have programmed. I need help with how i can get the total gold medals for each country as well as the gold medals according to event type. In short how can i get the desired output which is above. I can further explain if you need help with my question. Hints would be appreciated.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Your matchDetails() and findMatch() methods are too specific. You need some sort of mechanism to find / match a specific country, regardless of the event. And also, some sort of mechanism to find / match a specific event, regardless of the country.

Henry
 
Saad Mushtaq
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Your matchDetails() and findMatch() methods are too specific. You need some sort of mechanism to find / match a specific country, regardless of the event. And also, some sort of mechanism to find / match a specific event, regardless of the country.

Henry


Can you just give me like the algorithm as to how i should go about it? Can you be a bit more specific please?
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you just give me like the algorithm as to how i should go about it? Can you be a bit more specific please?

In the Beginning Java forum, we try not to give out complete examples, but instead give hints, clarifications, and support.  We find people learn better when they discover the solution for themselves, although I acknowledge that it can be frustrating at times for the OP.
 
Saad Mushtaq
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:
Can you just give me like the algorithm as to how i should go about it? Can you be a bit more specific please?

In the Beginning Java forum, we try not to give out complete examples, but instead give hints, clarifications, and support.  We find people learn better when they discover the solution for themselves, although I acknowledge that it can be frustrating at times for the OP.

I understand that. I have been doing the coding myself all this time but my program is showing me null pointer execption.

This is the code where i believe the problem lies. My winner array is fine thoughg but when i run this, i get null pointer exception. The stacktrace is here:
Exception in thread "main" java.lang.NullPointerException
at A2Q1.findingCountries(A2Q1.java:69)
at A2Q1.main(A2Q1.java:32)
Can you please help me with this. I am using the keyword new to create an instance of class MedalCounter but still i am getting the exception.
 
Tim Moores
Saloon Keeper
Posts: 4035
94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the if condition the array element at "count" is assumed to exist, even though it may be null. Hence the exception - you need to check it for being null before using it.
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apart from all that, I think something isn't correct about the responsibilities you assigned.

I don't think that GoldMedals as themselves supposed to know which countries won them. I'd think either Olympic team or/and Olympic committee should know which medals specific countries won.
MedalCounter I don't think it merits a class on its own, at least not in the way it is now. I'd think about an interface where Olympic team/country or Olympic committee supposed to implement functionality to count medals by countries/teams/events.

But lets see what others think.

 
Saad Mushtaq
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote:In the if condition the array element at "count" is assumed to exist, even though it may be null. Hence the exception - you need to check it for being null before using it.

I put an if condition to check for null array index, however, i am still getting null ararys now. the error is gone now.

I have added the above lines to my program.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a couple of problems I see here with your approach.

1. Your abstractions are not very good and they mislead you. They lead you away from a solution rather than help you find one.
2. You are way down in the weeds of the detailed implementation. You're losing sight of the big picture.

#2 is a direct result of #1.  So, to get yourself out of the mental rut that you're in, you need to go back and find a better way to think about the elements of this problem.

A GoldMedals class is not a good abstraction for the data that you are reading in from a file. Looking at your code for that class, I see you are mixing two concerns:

1. Encapsulate a logical subset of the data you read in
2. Keep a tally of the medals each country has won.

The name "GoldMedals" (plural) further adds to the ambiguity in this class since it implies a focus on the 2nd concern rather than the 1st.

So, what would be a better name for a class that represents each subgroup of three lines read from the data file?

Well, you know the data is information about a gold medal that was won for an event. You also have the country that won it, the event type, and the event name.

How about trying out the name EventWinner or EventGoldWinner?

Then, you can try to narrate what you needed to do, in plain English, using that name to see if it makes sense:

1. Read in data about various EventWinners.
2. Each EventWinner will have: country that won, the event type, and the event name.
3. To count how many gold medals each country won, go through the list of EventWinners and create a histogram by country.
4. To count how many gold medals were awarded for each event type, go through the list of EventWinners and create a histogram by event type.

The easiest way to create a histogram is by using a Map. In your case, it would be a Map<String, Integer>.

For your medal count by country histogram, the map key would be the winning country
For your medal count by event type, the map key would be the event type
In both histograms, the value of each entry would be the count of medals.

So, I guess the only question is, do you know how to use a java.util.Map and a java.util.List?
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, I guess the only question is, do you know how to use a java.util.Map and a java.util.List? 

See this thread: http://www.dreamincode.net/forums/topic/397908-olympics-java-coding/
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To give this kind of problem and not introduce Maps and Lists is ridiculous. Especially when you're talking about beginners. That's like asking people to build a house without hammers and nails, using only chisels and saws. Yes, it can be done but it takes more effort and skill. Ridiculous.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OP appears to be putting GoldMedals objects into a winners array.  Does this mean that you know how many lines of data you are going to read in from the file?  That doesn't seem like a reasonable assumption to me since a data file can be hundreds of lines long or it can be only a few lines long. Why would you use a static data structure like an array when the data you are reading in and storing in it can have a varying size? The best data structure to use to hold these objects would be a List. You're kind of hobbled from the start if you have to solve this problem without using a List.
 
Saad Mushtaq
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:OP appears to be putting GoldMedals objects into a winners array.  Does this mean that you know how many lines of data you are going to read in from the file?  That doesn't seem like a reasonable assumption to me since a data file can be hundreds of lines long or it can be only a few lines long. Why would you use a static data structure like an array when the data you are reading in and storing in it can have a varying size? The best data structure to use to hold these objects would be a List. You're kind of hobbled from the start if you have to solve this problem without using a List.

Does that mean that i have no other way to get around my problem? I understand what you said and the problem is we haven't been introduced to the idea of lists or maps. I know about lists as i studied them in my own time but we are not allowed to use them for this question. I have tried different ways to get around this problem now but to no avail. I still don't understand that why is it not creating a new MedalCounter object if there is none in the array.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you can take a very  simplistic view, and forget about classes, and maps, and Lists for now.

Suppose you have two arrays, both of 'm String arrays:

String[] countries = new String[10_000]   and String[] event = new String[10000] (assumming 10.000 is enough). You could also read the number of lines first, so you'll know how large that number should be.
Now, just read in your file. When you read an event, add it to the event array, and when you read a  country, add it to the countries array. You  need to  maintain a counter for this.
When finished reading in, it is time to have a look at the api of the Arrays class. You will  find a method 'sort'  in there. Sort  both arrays. You now need a method that traverses your  arrays, and every  time a country or event changes,  reset a counter.

That should  be straightforward. Agreed: it has nothing to do with OOP (but it'll give the required output)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!