Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# Comparing Species -- Need help--badly!

Rose Evans
Ranch Hand
Posts: 195
Ok guys, stumped again!
Can you take a look at the code I have so far, and give me some hints to what I need to do? The problem I need to solve is at the end of my code.
Any help would be greatly appreciated.
____________________________________________________________________-

public class eabCompareSpecies
{

public static void main(String[] args)
{
eabSpecies s1 = new eabSpecies();
eabSpecies s2 = new eabSpecies();
eabSpecies lower = new eabSpecies(); // has lower initial population
eabSpecies higher = new eabSpecies(); // has higher initial population
int i, maxYears = 10;
//get user input for two species
s1.getInput();
s2.getInput();

// if (lower.popNum <= higher.popNum) //*new stuff added*

// while(lower.popNum < higher.popNum) //*new stuff added*

//use loop here as we calculate growth for each year
// if we want to, we can easily change maxYears to a number input by the user.

for (i = 1; i <= maxYears; i++)
{
s1.getNewPopNum();
s2.getNewPopNum();

//compare population and set lower and higher objects
if(s1.getPopNum() < s2.getPopNum())
{
lower.set(s1.getName(), s1.getPopNum(), s1.getGrowthRate());
higher.set(s2.getName(), s2.getPopNum(), s2.getGrowthRate());
}
else
{
lower.set(s2.getName(), s2.getPopNum(), s2.getGrowthRate());
higher.set(s1.getName(), s1.getPopNum(), s1.getGrowthRate());
}
//The following lines can be commented out or left in.
//they were originally in as test lines.
System.out.println("In year "+i+", "+ s1.getName()+" is "+s1.getPopNum()+
" and "+s2.getName()+" is "+ s2.getPopNum()+".The larger population is "+
higher.getName()+".");
}

//The last thing left to do is figure out the "cross-over" year.
//The year in which the species that started out with the lower population,
//now has a higher population.

//Also, if we don't use 10 years, how will we prevent and
//indefinite loop? Is there a mathematical way to determine if the
//cross-over will happen?
}

}

marc weber
Sheriff
Posts: 11343
Originally posted by Rose Evans:
...Is there a mathematical way to determine if the cross-over will happen?

That really depends...

To answer that, we would need to know what kind of growth models are applicable here. For example, are we talking about simple exponential growth -- with the two species independent of one another? Do we have a predator/prey relationship between these species? What's the environmental limit on population (logistic growth)? Are these species competing for resources?

There are various statistical tests that can be performed on the data in order to see how well they appear to fit these different models, and a "best guess" could then be made within a certain level of confidence.

Rose Evans
Ranch Hand
Posts: 195
No, it's not that complicated. Its just a simple type test....I think we were using the examples of cats and dogs, and any ol made up figure for population and the growth rate. Just a simple type thing. But its too complicated for MY brain.

marc weber
Sheriff
Posts: 11343
So does the "growth rate" stay constant over this 10-year time period? (Even if we know the annual percentage rate, we would also need to know how frequently it compounds: annually, monthly, daily, continuously...)

More to the point: What does the eabSpecies class look like? In particular, what exactly does the getNewPopNum() method do?
[ October 27, 2004: Message edited by: marc weber ]

Rose Evans
Ranch Hand
Posts: 195
The getnewpopnum just gets the input that we choose to put in as the growth rate. Does that make since? This is the most messed up program I've ever seen in my life! Im assuming the growth rate does stay the same.
We enter something like species - dog / population - 500 / growth rate - 50 percent, and then we enter something like species - cat / population - 1000 / growth rate - 20 percent then we see which species takes over. I have someone who is trying to teach me java, and I cannot follow her to save my life! I am totally confused. I'd rather start out with smaller programs than something like this. It's too confusing for my brain.

marc weber
Sheriff
Posts: 11343
I agree: You should definitely be starting with smaller programs in order to grasp the concepts first. I think you're getting lost in details that really don't have much to do with learning the language(s).

But anyway... If you have, "species - dog / population - 500 / growth rate - 50 percent," is that just simple growth (multiplying by 1.5 each year)?
• Initial: 500
• Year 1: 750
• Year 2: 1,125
• Year 3: 1,688
• Year 4: 2,532
• etc...

• Assuming that's the case, then the population for any year can be expressed as:

population = initialPopulation * [(1 + rate)^year]

...where ^ denotes "raised to the power of."

So if we start with an initial population of 500, and the growth rate is a simple 50%, then we have:

population = 500 * (1.5^year)

So, for example, the population at year 7 would be:

500 * (1.5^7) = 8543 (approx.).

Now, if one species' population is going to overtake the other, then there will be a point at which they cross -- that is, when the two populations are equal.

So, if we have:

Dog: 500 with a growth rate of 50%
Cat: 1000 with a growth rate of 20%

...then our expressions are...

dogPop = 500 * (1.5^year)
and
catPop = 1000 * (1.2^year)

...and if one overtakes the other, then these will be equal at some point:

500 * (1.5^year) = 1000 * (1.2^year)

So we want to find a "year" for which this is true. Solving for year:

(500/1000) = (1.2/1.5)^year

log(0.5) = log(0.8)^year

log(0.5) = year * log(0.8)

year = [log(0.5) / log(0.8)] = 3.1 (approx.).

Therefore, the population levels will cross between the 3rd and 4th year.

Anyway, that's the math part of it. (Even if the growth models are different, the idea remains the same.) I hope this allows you to focus on the coding...
[ October 28, 2004: Message edited by: marc weber ]

Rose Evans
Ranch Hand
Posts: 195
Marc! Wow! You must be one talented guy!!! I hope someday I can look at a problem and figure it out as well as you did. My hat is off to you! Thank you so much for your help! It is greatly appreciated!!!

Rose Evans
Ranch Hand
Posts: 195
Thanks guys, I finally got this one working!

 It is sorta covered in the JavaRanch Style Guide.