Source of SpeciesSecondTry.java


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