Source of SpeciesThirdTry.java


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