Imagine this. I have had to create an arraylist with 5 different objects. These 5 different objects are countries. Each country has different information based on the constructor.
One of these is an double which is the density of the country. It can be whatever, that does not matter. I have already created a method that goes through each object takes out the required information that calculated the density. I have also created a method that calculates the sum of all the densities for each country and followed by the average density of each country. Now I have been asked to take the density of each country and compare that to the average density of all countries and print off which is the nearest to the average. I dont have a clue!
I can find the higest value and the lowest but how do I find which value is closest to the average.
So for example, I have 5 countries with the following densities calculated (not true of course)
Country3 is selected becacuse it is closest to the average. In this case it is the average (concidence)
I will continue to work on this. If anyone can help. Its appreciated
EDIT : I just spoke with my instructor. She gave me some help on the logic side. She said to create two variables, one to save the name of the country and one to save the difference, then I can compare better....Hmmmmmm
You say you have written the code to find the country with the lowest density. All you have to do is modify this to find the country with the lowest difference between its density and the average density. To calculate the difference you simply subtract the average density from the country's density and then get the absolute value of the result (the Math class has a method that will do this for you).
I would have made you explain to me HOW YOU KNEW that country 3 was the closest. You had to do something in your brain. The trick to writing software is converting what your brain does to what the computer does.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
I would suggest the "diff" variable ought to be a local variable to the method, not a field. In your present example, you will have no problems, but there might potentially be problems if there are many values close together, because of the precision or otherwise (see No 20) of the floating-point arithmetic.
Joined: May 04, 2010
Thanks for all the answers and help!
The players have changed but the game has stayed the same. This time I am now calculating which countries residents are closest to the average. Logic stays the same of coruse
This is the first solution outside of a loop. Please note that 'inwoners' means residents and 'landen' means countries
Now I have to put the second part in a loop (even though the first part is already done)
Joined: Oct 13, 2005
Get all that code out of the main () method. The main () method is intended for starting off your application, not for doing all the work.
Your List should be parameterised eg List<Country>, then you can get rid of that nasty class-cast.
You need to iterate through the List, and if you find a value closer to the average, you can reduce your diff value and record the Country you found.
Joined: May 04, 2010
Indeed you are right that should not be in the main method and it shall be removed later. I shall also ask my instructor about 'parameterising' my List
Ok, I managed to solve my loop issue. My logic was to assign a default value and default land and compare the rest (the other countries) to them. This is my finished article. It may not be everyones cup of tea, and it may require a clean up, but I am quiet happy as it is my own logic
The first code shows my finished article. The second shows what Ritchie mentioned in anonther post about casting. See line 11 in the second example.
Joined: Oct 13, 2005
Only had a half-second look at it, but it looks good
You should have methods like calculateAverageXXX and findClosestToAverageXXX. Whether the average is a field of the class or a local variable is a point for discussion; I'll let you work it out first before I tell you what my preference would be
subject: Compare to an Average Value - The NEAREST Value