# more help!

• 02-13-2005, 08:11 PM
blueandconfused
more help!
I need help with this one...at the bottom is what I have done so far.

If P is the population on the first day of the year, B is the birth rate,
and D is the death rate, the estimated population at the end of the year
is given by the formula:
P + B*P - D*P
100 100

The population growth rate is given by the formula:
B-D

a. growthRate: this method takes as its parameters the birth and death rates,
and it returns the population growth rate.

b. estimatedPopulation: this method takes as its parameters the current population,
population growth rate, and n, the number of years. It returns the
estimated population after n years.

Your program should not accept a negative birth rate, negative death rate,
or a population less than 2.
------------------------------------------------------------------------
import java.io.*;

public class birthandDeath
{

public static void main(String[] args) throws IOException
{
int startP, birth, death, years;

System.out.println("Enter the starting population: ");
System.out.flush();
System.out.println("Enter the birth rate: ");
System.out.flush();
System.out.println("Enter the death rate: ");
System.out.flush();
System.out.println("Enter the number of years: ");
System.out.flush();
}
• 02-22-2005, 05:12 PM
octobclrnts
I think this should be what you need. I tested it and it seemed to work if I understand what you want. :)

import java.io.*;

public class BirthAndDeath
{
public static void main(String args[]) throws IOException
{
String strStartP, strBirthRate, strDeathRate, strYears;
int startP, birthRate, deathRate, years, growthRate, estPop;
boolean done = false;

while(!done)
{
try
{
System.out.print("Enter the starting population: ");
startP = Integer.parseInt(strStartP);
if(startP < 2)
throw new NumberFormatException();

System.out.print("Enter the birth rate: ");
birthRate = Integer.parseInt(strBirthRate);
if(birthRate < 0)
throw new NumberFormatException();

System.out.print("Enter the death rate: ");
deathRate = Integer.parseInt(strDeathRate);
if(deathRate < 0)
throw new NumberFormatException();

System.out.print("Enter the number of years: ");
years = Integer.parseInt(strYears);
if(years < 0)
throw new NumberFormatException();

growthRate = growthRate(birthRate,deathRate);
estPop = estPop(years, growthRate, startP);

System.out.println("The estimated population in " + years + " years is " + estPop);
done = true;
}
catch(NumberFormatException e)
{
System.out.println("The number entered was not in the correct format.");
}
}
}

public static int growthRate(int birthRate, int deathRate)
{
int growthRate = birthRate - deathRate;
return growthRate;
}

public static int estPop(int years, int growthRate, int startP)
{
int estPop = startP + years * growthRate;
return estPop;
}
}