File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Population and Growth Rate Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Population and Growth Rate Problem" Watch "Population and Growth Rate Problem" New topic
Author

Population and Growth Rate Problem

Jem Edwards
Greenhorn

Joined: Mar 28, 2003
Posts: 22
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

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1824

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 ]

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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 ]

"I'm not back." - Bill Harding, Twister
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

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!


Please ignore post, I have no idea what I am talking about.
Jem Edwards
Greenhorn

Joined: Mar 28, 2003
Posts: 22
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Population and Growth Rate Problem