Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

forecast an Olympic winner  RSS feed

 
Zach Christian
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am attempting to write a program to forecast an Olympic winner based off the American/Canadian forecasting types.

The program needs to do the following:
Ask the user what scoring method they would like to use to score the Olympics (a character).
• Prompt the user for the number of countries competing in the current Olympics.
• Read in the country names and their medal count.
• Print out the name of the winning country.
• In case of a tie, print out the winning countries in input order.

So far I've got the user input and an array setup, but I am overthinking how to store the medals for each country

 
Knute Snortum
Sheriff
Posts: 4091
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look into a HashMap for storing countries and medals.

Some other things about your code: variables should be camelCase, that is, the first letter lower case. Your indenting is okay but not as good as it could be. A lot of errors can be avoided with good indentation. Also, it is not necessary to double space all your code. I usually put a blank line around loops and some decisions, or to group similar statements.
 
Carey Brown
Bartender
Posts: 3024
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A simple, non-OO way, would be to create a second array to contain the medalCount.

A more OO way would be to create a Country class with fields name and medalCount and have an array of those.
 
Carey Brown
Bartender
Posts: 3024
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Misc:
This is the C style way to declare arrays. Although it works it is not the Java style convention.

Use this style instead.


You variable Answer is not as descriptive as it could be. scoringMethod might have been better.
 
Zach Christian
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:A simple, non-OO way, would be to create a second array to contain the medalCount.

A more OO way would be to create a Country class with fields name and medalCount and have an array of those.


how would I reference the 2 arrays? Say user inputs "Germany" as the country and 1,2,3 for the gold/silver/bronze medals. How would one tie Germany to the number of medals to do a comparison of the other countries?
 
Carey Brown
Bartender
Posts: 3024
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zach Christian wrote:
Carey Brown wrote:A simple, non-OO way, would be to create a second array to contain the medalCount.


how would I reference the 2 arrays? Say user inputs "Germany" as the country and 1,2,3 for the gold/silver/bronze medals. How would one tie Germany to the number of medals to do a comparison of the other countries?





I don't think a HashMap (per Knute) is appropriate because you'll have to output the countries in the order that they were entered in the event of a tie. An ArrayList would be a better choice.
 
Zach Christian
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Zach Christian wrote:
Carey Brown wrote:A simple, non-OO way, would be to create a second array to contain the medalCount.


how would I reference the 2 arrays? Say user inputs "Germany" as the country and 1,2,3 for the gold/silver/bronze medals. How would one tie Germany to the number of medals to do a comparison of the other countries?





I don't think a HashMap (per Knute) is appropriate because you'll have to output the countries in the order that they were entered in the event of a tie. An ArrayList would be a better choice.


I am still not getting what I need for this program. I just need the country and the olympic medals per country (I can do the math for the array) but am unsuccessful.

>
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're recording three different medal types you would need at least three different arrays for each medal (int[] bronze, int[] silver, int[] gold, and maybe int[] total). You don't have to loop through your medals in any way while you're looping through your countries.

Parallel arrays can get really messy, if you're capable of using an object oriented approach you should do so.
 
Carey Brown
Bartender
Posts: 3024
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tyson Lindner wrote:If you're recording three different medal types you would need at least three different arrays for each medal (int[] bronze, int[] silver, int[] gold, and maybe int[] total). You don't have to loop through your medals in any way while you're looping through your countries.

Parallel arrays can get really messy, if you're capable of using an object oriented approach you should do so.

Hmmm. "Read in the country names and their medal count." wasn't clear about what to do with gold/silver/bronze, it just said count.

If you really need gold/silver/bronze counts separately I think it would be time to create a Country class to hold all of that.
 
Zach Christian
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay so I need a for loop to send user input to the class variables, and create a new object for each iteration of the for loop based off the number of countries being compared.

How would you create those objects in a loop and pass those scanner inputs?
 
Carey Brown
Bartender
Posts: 3024
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would create a constructor that takes all the necessary parameters. Then create an instance with 'new'. And add the instance to your array.
 
Zach Christian
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I have a Country class, so how would I pass my Scanner into that? Can you show me?
Also I need for loop to run till i = NumOfCountries to create an object for each Country class.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zach Christian wrote:I have a Country class, so how would I pass my Scanner into that? Can you show me?

Sure:Hopefully you get the idea..,

Another tip: Class names should generally be singular. And if you absolutely must make it plural, then call it 'Countries'. You don't have to forget good spelling just because you're writing a program.

HIH

Winston
 
Zach Christian
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Zach Christian wrote:I have a Country class, so how would I pass my Scanner into that? Can you show me?

Sure:Hopefully you get the idea..,

Another tip: Class names should generally be singular. And if you absolutely must make it plural, then call it 'Countries'. You don't have to forget good spelling just because you're writing a program.

HIH

Winston


Thanks for the help. Now, if multiple countries are inputted, how do I create multiple objects, say, in a "for loop"? One to data for each country?
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zach Christian wrote:Thanks for the help. Now, if multiple countries are inputted, how do I create multiple objects, say, in a "for loop"? One to data for each country?

Yup.However, from here on out you're on your own; otherwise we'll end up writing the program for you, and that's your job.

We're happy to help, but you have to ShowSomeEffort.

Winston

PS: Use 'i' for loop variables unless you have a good reason not to.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!