Source of SpeciesThirdTry.java


  1: 
  2: import java.util.Scanner;
  3: 
  4: public class SpeciesThirdTry
  5: {
  6:     private String name;
  7:     private int population;
  8:     private 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:      Precondition: years is a nonnegative number.
 32:      Returns the projected population of the calling object
 33:      after the specified number of years.
 34:     */
 35:     public int predictPopulation(int years)
 36:     {
 37:                 int result = 0;
 38:         double populationAmount = population;
 39:         int count = years;
 40:         while ((count > 0) && (populationAmount > 0))
 41:         {
 42:             populationAmount = (populationAmount +
 43:                           (growthRate / 100) * populationAmount);
 44:             count--;
 45:         }
 46:         if (populationAmount > 0)
 47:             result = (int)populationAmount;
 48:         
 49:         return result;
 50:     }
 51: }