DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2

Thread: more help!

  1. #1
    Join Date
    Feb 2005
    Posts
    18

    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
    {
    static BufferedReader keyboard =
    new BufferedReader(new InputStreamReader(System.in));

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

    System.out.println("Enter the starting population: ");
    System.out.flush();
    startP = Integer.parseInt(keyboard.readLine());
    System.out.println("Enter the birth rate: ");
    System.out.flush();
    birth = Integer.parseInt(keyboard.readLine());
    System.out.println("Enter the death rate: ");
    System.out.flush();
    death = Integer.parseInt(keyboard.readLine());
    System.out.println("Enter the number of years: ");
    System.out.flush();
    years = Integer.parseInt(keyboard.readLine());
    }

  2. #2
    Join Date
    Feb 2005
    Posts
    3
    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
    {
    BufferedReader myIn = new BufferedReader(new InputStreamReader(System.in));
    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: ");
    strStartP = myIn.readLine();
    startP = Integer.parseInt(strStartP);
    if(startP < 2)
    throw new NumberFormatException();

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

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

    System.out.print("Enter the number of years: ");
    strYears = myIn.readLine();
    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;
    }
    }
    Last edited by octobclrnts; 02-22-2005 at 06:34 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links