Source of SpeciesFirstTry.java


  1: 
  2: import java.util.*;
  3: 
  4: public class SpeciesFirstTry
  5: {
  6:     public String name;
  7:     public int population;
  8:     public double growthRate;
  9:     public void readInput( )
 10:     {
 11:         Scanner keyboard = new Scanner(System.in);
 12:         System.out.println("What is the species' name?");
 13:         name = keyboard.nextLine( );
 14: 
 15:         System.out.println("What is the population of the species?");
 16:         population = keyboard.nextInt( );
 17:         while (population < 0)
 18:         {
 19:             System.out.println("Population cannot be negative.");
 20:             System.out.println("Reenter population:");
 21:             population = keyboard.nextInt( );
 22:         }
 23: 
 24:         System.out.println(
 25:                       "Enter growth rate (percent increase per year):");
 26:         growthRate = keyboard.nextDouble( );
 27:     }
 28: 
 29:     public void writeOutput( )
 30:     {
 31:          System.out.println("Name = " + name);
 32:          System.out.println("Population = " + population);
 33:          System.out.println("Growth rate = " + growthRate + "%");
 34:     }
 35: 
 36:     public int populationIn10( )
 37:     {
 38:         double populationAmount = population;
 39:         int count = 10;
 40:         while ((count > 0) && (populationAmount > 0))
 41:         {
 42:             populationAmount = (populationAmount +
 43:                               (growthRate/100) * populationAmount);
 44:             count--;
 45:        }
 46:        if (populationAmount > 0)
 47:             return (int)populationAmount;
 48:         else
 49:             return 0;
 50:     }
 51: }