# Comparing Species -- Need help--badly!

Rose Evans

Ranch Hand

Posts: 195

posted 11 years ago

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*

// System.out.println("Sorry, please try again."); //*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?

}

}

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*

// System.out.println("Sorry, please try again."); //*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?

}

}

) ) ) )

posted 11 years ago

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.

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.

*~Joe Strummer*

sscce.org

Rose Evans

Ranch Hand

Posts: 195

posted 11 years ago

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 ]

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 ]

*~Joe Strummer*

sscce.org

Rose Evans

Ranch Hand

Posts: 195

posted 11 years ago

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.

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.

) ) ) )

posted 11 years ago

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...

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,

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

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

[ October 28, 2004: Message edited by: marc weber ]

But anyway... If you have, "species - dog / population - 500 / growth rate - 50 percent," is that just simple growth (multiplying by 1.5 each year)?

*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 ]

*~Joe Strummer*

sscce.org

Rose Evans

Ranch Hand

Posts: 195