Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Population and Growth Rate Problem

 
Jem Edwards
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Internet exercise this week involves the Species class and none of us has a clue. This week it's Chapter 4 (Defining Classes and Methods) and most of us still can't understand Branching/Looping statements from Chapter 3 last week! So I will attempt to post it here and hopefully someone will give me plain, vanilla instructions on how to proceed. If anyone of you remember your Java schooling, I bet you remember this exercise.
OK - here's the exercise which I am attempting to work on in a stand-alone program:
Write a program to find out how many years it will take for one population to exceed the other population. The program should ask for the data on both species (name of species, the population and growth rate) and will respond by telling how many years it will take for the populations to change so that the species that starts with the lower population has a population that exceeds that of the species that starts with the higher population.
I am also including a copy of the Species class and what little code I've just figured out how to use today. We need to use the set method of the Species class but don't know how to get it to run. I think that there has to be a loop involved somewhere to keep calculating the years and growth rate with some kind of break code to tell it when to stop and give me results.
Thank you.
My pathetic little code so far:

This is the code for the Species class:
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you explain exactly where you are having problems? Does this code compile? If not, what are the compiler errors? If it does, then what is the output? How does this differ from what you expect?
If you can fill in some of these details, then we will be glad to help out.
Thanks,
Layne
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever I have a problem like this (and admittedly mine are more complex these days) I start be determining by hand how to acheive the desired effect (how long it will take for one population to exceed the other). Then I translate this into a program to do it for me. The key is to work it out on paper first, analyze how you came to your solution, and then translate that into a computer program. After you done with that you can optimize your output.
From the wording of your assignment, it looks as though you are supposed to use loops to figure out the solution. If you assume that each iteration through the loop represents a year, you can adjust the population of the species accordingly. Try working it out from there.
If you need to experiment by hand, try using nice, round numbers. Say SpeciesA starts with 100 and grows at 3/year, and SpeciesB starts with 200 and grows at 2/year.
As far as loops are concerned, you have two options: for and while. (OK, you have do...while, but I never use that one; it is always possible to write code that uses while instead of do...while). for loops are best when you know how long something will take (such as iteration). while loops are better used when something depends on some condition and not necessarily a specific number of iterations.
BTW, you don't need to use loops to figure this out, but I think that defeats the point of the assignment
One more BTW (style nit-picking): Personally, I try to avoid using end-of-line comments. I find that it is better to state what I am gong to do before actually doing it. It helps from a logical flow, and it makes the code (IMHO) more readable: the code and the comments are on separate lines. eg:

instead of

This is just my preference, of course. But you might get some benefit from it. (Of course, you are free to be completely different from me, too )
[ April 11, 2003: Message edited by: Joel McNary ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We need to use the set method of the Species class but don't know how to get it to run.
Actually it's not clear that you need to use set() at all. You've already got code that uses readInput() to establish the values for both species; after that, you don't really need set() to change the data to something else. I'd concentrate on creating some sort of loop, using the advice given by others so far.
Incidentally if you keep expanding on the code for this assignment, you could eventually use it to study my favorite example of population dynamics, here. (Beware typo - the first equation should have "-aHV" as a completely separate term, not part of the denominator.) Enjoy...
[ April 11, 2003: Message edited by: Jim Yingst ]
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
still can't understand Branching/Looping statements from Chapter 3 last week!

I would suggest you post some of those questions (in a new topic) also. Since knowing that work will be needed to move on.
I start by determining by hand how to acheive the desired effect

Damn good suggestion. Trying to get the computer (by you writing a program) to solve a problem you don't know how to solve yourself isn't going to get you very far.
And nice population dynamics link!
 
Jem Edwards
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Layne-nope I wasn't having problems compiling my "pathetic little code". It compiled fine but I was stumped as to how to proceed on with getting the solution to the problem of "how many years it will take for the populations to change so that the species that starts with the lower population has a population that exceeds that of the species that starts with the higher population".
Joel-you're right about figuring out something on paper first so this morning I am trying to do a pseudocode (learned about that in Chapter 1!) Thank you for the advice on the comment line. I appreciate any advice from more experienced users.
Jim-you're right. None of us can figure out where the set method comes in but the Dr. posted just that portion of the Species class in his homework assignment after the book tells us to use the Species class for the exercise(it didn't say just to use a portion of the class).
I'm now going to start a new post regarding a question on pseudocode.

[ April 12, 2003: Message edited by: Jem Edwards ]
[ April 12, 2003: Message edited by: Jem Edwards ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic