Source of SpeciesSecondTry.java


  1: //SpeciesSecondTry.java
  2: 
  3: import java.util.Scanner;
  4: 
  5: public class SpeciesSecondTry
  6: {
  7:     public String name;
  8:     public int population;
  9:     public double growthRate;
 10: 
 11:     public void readInput()
 12:     {
 13:         Scanner keyboard = new Scanner(System.in);
 14:         System.out.println("What is the species' name?");
 15:         name = keyboard.nextLine();
 16: 
 17:         System.out.println("What is the population of the species?");
 18:         population = keyboard.nextInt();
 19: 
 20:         System.out.println("Enter growth rate (% increase per year):");
 21:         growthRate = keyboard.nextDouble();
 22:     }
 23: 
 24:     public void writeOutput()
 25:     {
 26:         System.out.println("Name = " + name);
 27:         System.out.println("Population = " + population);
 28:         System.out.println("Growth rate = " + growthRate + "%");
 29:     }
 30: 
 31:     /**
 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: 
 47:         if (populationAmount > 0)
 48:             result = (int)populationAmount;
 49: 
 50:         return result;
 51:     }
 52: }