Jeremy Brooks
Jeremy Brooks

Reputation: 49

Java program using compound growth

So here is what the brief for the program says: "According to CNNMoney.com, Facebook hit 1 billion users in October 2012. Using the compound-growth technique you learned in Fig 4.6 and assuming its user base grows at a rate of 4% per month, how many months will it take to grow its user base to 1.5 billion users? How many months will it take for Facebook to grow its user base to 2 billion users?"

I have the program "working" but I feel as if it is not calculating the values correctly. I also don't know if I am using the most current or efficient Java techniques. I am definitely a beginner so any suggestions or pointers will be much appreciated. Here is what I have so far!

package programmingassignment4.pkg32;

public class ProgrammingAssignment432 {

    public static void main(String[] args) {
        int user= 1000000000;
        int user2= 1000000000;
        double total1 = 0;
        double total2 = 0;
        int grandtotal1=0;
        int grandtotal2=0;
        int i = 1;
        int j=1;
        for(i=1; user<=1500000000; i++)
        {
           total1 = user*(.04);
           user+=total1;
        }
        System.out.print("The total number of months to reach 1.5 billion users will be : " + i + "\n");
        for(j=1;user2<=2000000000;j++)
        {
            total2 = user2*(.04);
            user2+=total2;
        }
        System.out.print("The total number of months to reach 2 billion users will be : " + j);
   }
}

Upvotes: 0

Views: 1206

Answers (2)

Zebra
Zebra

Reputation: 11

I hope my answer will still help anyone who is looking for solution to this question. I think the only solution that was posted here is a bit convoluted. Try mine.

Your assignment say to use the compound growth formula so why don't you use it?

a = p(1 + r)n

where a would be total amount, p is your starting rate(in this case 1bln), r is your growth rate and n is months. Very straight forward.

You can use Math.pow Java method for this.

total = startrate * Math.pow(1.0 + growthrate, month);

Math.pow(x, y) calculates the value of x raised to the yth power. The method receives two double arguments and returns a double value.

Here is mine output from running the code:

    Facebook User Base Growth

Projected User Base in months after October 2012
   1    1,040,000,000.00
Months
   2    1,081,600,000.00
Months
   3    1,124,864,000.00
Months
   4    1,169,858,560.00
Months
   5    1,216,652,902.40
Months
   6    1,265,319,018.50
Months
   7    1,315,931,779.24
Months
   8    1,368,569,050.41
Months
   9    1,423,311,812.42
Months
  10    1,480,244,284.92
Months
  11    1,539,454,056.32
Months

It took Facebook 11 months to grow that big.

Now we will count how many months it will take to get to 2bln users.

  11    1,539,454,056.32
Months
  12    1,601,032,218.57
Months
  13    1,665,073,507.31
Months
  14    1,731,676,447.60
Months
  15    1,800,943,505.51
Months
  16    1,872,981,245.73
Months
  17    1,947,900,495.56
Months
  18    2,025,816,515.38
Months

It took Facebook 18 months to grow that big.

If it sparks enough interest I can post the entire code here. Happy programming!

Upvotes: 0

Bobulous
Bobulous

Reputation: 13169

I'd say your code is on the right track, and you seem to understand the concept. However, I believe your for loops will wrongly leave you with a value which is one greater than the actual number of months required to reach the target, because you start with a value of one rather than zero.

It's also a little odd (but not wrong) to have the loop variable declared before the loop, so I think I'd use a while loop instead. Also, because you're effectively running the same logic twice, the code would be better moved into its own method so you can call it flexibly. Something like this:

private static int monthsOfGrowthRequired(int startAmount,
        int targetAmount, double growthFactor) {
    int monthsOfGrowth = 0;
    int runningAmount = startAmount;
    while (runningAmount < targetAmount) {
        runningAmount *= growthFactor;
        ++monthsOfGrowth;
    }
    return monthsOfGrowth;
}

Note that this method would take a growthFactor value of 1.04 for a growth rate of +4% per month. (And thus a value of 0.96 for a rate of -4% per month.)

As for checking your numbers: that's easy. The exact number can be calculated without resorting to numerical analytical methods like this because logarithms can be used directly. For example, in the case of the goal being 1.5 billion, where m equals the number of months of growth needed, you can write this equation:

1.04 ^ m = 1.5

therefore:

m = (log 1.5) / (log 1.04)

It's over to you to apply the same logic to the case where the goal is two billion, and to improve the suggested method to add any safety checks that might be necessary to avoid bad input and to catch any cases where the method would never end.

Upvotes: 1

Related Questions