-5

I am practicing some Java and one of the applications I am writing asks to output the world population in the next 75 years.

I am using the population growth model. My issue is that my application outputs 'Infinity' in the column where the estimated population should be output.

This is my code:

import java.util.Calendar;
import java.util.regex.Matcher;

public class WorldPopulationGrowth {
    public static void main(String[] args) {
        double currentWorldPopulation = 7.4e9;
        double worldPopulationGrowthRate = 1.13;
        double anticipatedWorldPopulation;
        int initialYear = Calendar.getInstance().get(Calendar.YEAR);

        System.out.println("Year\tAnticipated World Population (in billions)\tPopulation " +
                "increase since last year");
        System.out.println(String.format("%d\t%.1e\t\t\t\t\t\t\t\t\t\t\tNA", initialYear, currentWorldPopulation) );


        for(int i=1; i < 76; i++){
            int year = initialYear + i;
            double growthExponential = worldPopulationGrowthRate*year*1.0;
            anticipatedWorldPopulation =  currentWorldPopulation * Math.pow(Math.E, growthExponential);

            System.out.println(String.format("%d\t%.1e\t\t\t\t\t\t\t\t\t\t\t", year, anticipatedWorldPopulation));

            currentWorldPopulation = anticipatedWorldPopulation;
        }

    }
}
Ko Ga
  • 856
  • 15
  • 25
  • Have you tried debugging through it? Your `growthExponential` is being multiplied by the current year, for each iteration, which will definitely throw off your math – Krease Aug 06 '16 at 14:35
  • You also seem to be doing other incorrect things with your math by using `Math.E` which by itself will nearly triple `currentPopulation` each iteration, but you're raising it to a power of several thousand each time.. – Krease Aug 06 '16 at 14:42
  • The growth should be by the formula `pow(GrowthRate, i)` or `exp(log(GrowthRate)*i)`. – Lutz Lehmann Aug 06 '16 at 17:24

2 Answers2

1

Let's take a careful look at the first iteration of your code, as if we were debugging it (Make sure you try to do this in the future!)

  • currentWorldPopulation = 7.4e9
  • worldPopulationGrowthRate is 1.13
  • initialYear is 2016
  • your loop begins, i is 1
    • year is set to 2017
    • growthExponential is set to 1.13 * 2017 = 2279.21 (this is the start of your problem)
    • anticipatedWorldPopulation is set to 7.4e9 * e^2279.21
    • this is roughly 7.4e9 * 7.05e989... KABOOM

Revisit your calculations, and step through your application (ideally in a debugger) to see your problems.

Krease
  • 15,805
  • 8
  • 54
  • 86
  • This is the correct answer. I'd advise the OP to accept and upvote this one. Good eye, @Krease – duffymo Aug 06 '16 at 15:31
  • Thanks, got it straightened out. I was using the percentage increase as 1.13 instead of the correct 0.0113 and instead of the year, I replace year by the counter i. – Ko Ga Aug 07 '16 at 17:42
  • And I would upvote this if I had enough reputation to be able to! :) – Ko Ga Aug 07 '16 at 17:42
0

@Krease found your problem.

I recoded it. Once you fix the issue he found it's fine. I used JDK 8 lambdas and gave you both percentage and exponential growth models. The code prints both for comparison:

import java.util.function.DoubleFunction;


/**
 * Simplistic population growth model
 * @link  https://stackoverflow.com/questions/38805318/getting-infinity-output-instead-of-actual-numbers/38805409?noredirect=1#comment64979614_38805409
 */
public class WorldPopulationGrowth {

    public static void main(String[] args) {
        double currentWorldPopulation = 7.4e9;
        double worldPopulationGrowthRate = 1.13;
        int numYears = 76;
        int startYear = 1976;

        double populationExponential = currentWorldPopulation;
        ExponentialGrowthModel modelExpGrowth = new ExponentialGrowthModel(worldPopulationGrowthRate);
        double populationPercentage = currentWorldPopulation;
        PercentageGrowthModel modelPercentGrowth = new PercentageGrowthModel(worldPopulationGrowthRate);

        System.out.println(String.format("%10s %20.3e %20.3e", startYear, currentWorldPopulation, currentWorldPopulation));
        for (int i = 1; i < numYears; ++i) {
            populationExponential = modelExpGrowth.apply(populationExponential);
            populationPercentage = modelPercentGrowth.apply(populationPercentage);
            System.out.println(String.format("%10s %20.3e %20.3e", startYear+i, populationExponential, populationPercentage));
        }
    }
}

class ExponentialGrowthModel implements DoubleFunction<Double> {

    private double exponent;

    ExponentialGrowthModel(double exponent) {
        this.exponent = exponent;
    }

    private double getExponent() {
        return exponent;
    }

    public void setExponent(double exponent) {
        this.exponent = exponent;
    }

    @Override
    public Double apply(double value) {
        return value*Math.exp(this.getExponent());
    }
}

class PercentageGrowthModel implements DoubleFunction<Double> {

    private double percentageIncrease;

    PercentageGrowthModel(double percentageIncrease) {
        this.percentageIncrease = percentageIncrease;
    }

    private double getPercentageIncrease() {
        return percentageIncrease;
    }

    public void setPercentageIncrease(double percentageIncrease) {
        this.percentageIncrease = percentageIncrease;
    }

    @Override
    public Double apply(double value) {
        return value*(this.getPercentageIncrease());
    }
}
duffymo
  • 305,152
  • 44
  • 369
  • 561
  • The math is off by even more than that ;) see my comments on the question. – Krease Aug 06 '16 at 14:43
  • Now that I'm reading the code more carefully I can see that you're right. The OP has confused a power model with an annual percentage increase for the fatal effect. – duffymo Aug 06 '16 at 14:51