Jeremy Brooks - 1 year ago 77

Java Question

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);

}

}

Answer Source

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.