Source of SpeciesSecondTry.java


  1: 
  2: import java.util.*;
  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 = Scanner.create(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:         keyboard = Scanner.create(System.in);
 18:         population = keyboard.nextInt( );
 19:         while (population < 0)
 20:         {
 21:             System.out.println("Population cannot be negative.");
 22:             System.out.println("Reenter population:");
 23:             population = keyboard.nextInt( );
 24:         }
 25: 
 26:         System.out.println(
 27:                       "Enter growth rate (percent increase per year):");
 28:         growthRate = keyboard.nextDouble( );
 29:     }
 30: 
 31:     public void writeOutput( )
 32:     {
 33:          System.out.println("Name = " + name);
 34:          System.out.println("Population = " + population);
 35:          System.out.println("Growth rate = " + growthRate + "%");
 36:     }
 37: 
 38:     /**
 39:      Returns the projected population of the calling object
 40:      after the specified number of years.
 41:     */
 42:     public int projectedPopulation(int years)
 43:     {
 44:         double populationAmount = population;
 45:         int count = years;
 46:         while ((count > 0) && (populationAmount > 0))
 47:         {
 48:             populationAmount = (populationAmount +
 49:                           (growthRate/100) * populationAmount);
 50:             count--;
 51:         }
 52:         if (populationAmount > 0)
 53:             return (int)populationAmount;
 54:         else
 55:             return 0;
 56:     }
 57: }