Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using exponential functions in Java.
#1
The program itself is simple enough. However, I'm clueless as to how I should implement this math problem in this for loop... I do have the program itself done, of course.

Code:
public class PopulationGrowth
{

    public static void main(String[] args)
    {
        double pop = 231;
        int popCounter = 1980;
        
        for (popCounter = 1980; popCounter <= 2013; ++popCounter)
        {
            pop = pop * ???????;
        }
        System.out.printf("The population in 2013 is approximately: %f million.", pop);

    }

}
Question marks should be obvious. Rolleyes

Basically, I'm supposed to predict the population growth from 1980 to 2013, with a 1.03% increase from a 231 million population.

I know it's 231 * (1.103)[SUP]33[/SUP]. My question is... how do I write that in code? Since it's a for loop, the exponential value would be the counter, but that's all I got.

In case you're wondering, this assignment is to practice the use of for loops, so doing it like this is pretty much the entire point of the assignment.

EDIT: IT'S 1.0103 AND SHOULD NOT BE INCLUDING 2013. Okay oops.
Reply
#2
pop *= 1.03;

?

I fail to understand.
Reply
#3
Why does this need a loop?

Your loop parameters should not be inclusive of 2013 as that's 34 years, and it should be 1.0103 not 1.03 Danny, geez.
Reply
#4
Locked Wrote:pop *= 1.03;

?

I fail to understand.

I did that at first, but I got a different answer from what I calculated...

I'm probably extremely stupid. ;_;

JoeTang Wrote:Why does this need a loop?

Ask my teacher, she assigned this. Rolleyes
Reply
#5
JoeTang Wrote:Why does this need a loop?

Your loop parameters should not be inclusive of 2013 as that's 34 years, and it should be 1.0103 not 1.03 Danny, geez.

Oh it's a 1.03% increase.
Pffft..

> Why does this need a loop?

> In case you're wondering, this assignment is to practice the use of for loops, so doing it like this is pretty much the entire point of the assignment.

You didn't read either. So shhh
Reply
#6
NOTE: I'm a bit of a newbie, but I think I can help.

First of, the pineapple is up with your for loop? Wouldn't that reset the year (popCounter) back to 1980 each time it loops?

As for your real question, you have to multiply the amount the population increases by 1.103 each time it loops, assuming I'm understanding the problem right.

double pop = 231;
int popCounter = 1980;
double percentIncrease = 1.103;

for (popCounter = 1980; popCounter <= 2013; ++popCounter)
{

percentIncrease = percentIncrease * percentIncrease;
}

pop = pop * percentIncrease;

System.out.printf("The population in 2013 is approximately: %f million.", pop);
Reply
#7
JoeTang Wrote:Your loop parameters should not be inclusive of 2013 as that's 34 years, and it should be 1.0103 not 1.03 Danny, geez.

HAHA OOPS APPARENTLY I CAN'T MATH.

Thanks, it works now. :|
Reply
#8
Assignment Wrote:(Round the correct answer to two decimal places.)

Oh, I missed that part.

Too bad she never told us how to do that. Sad
Reply
#9
MariaColette Wrote:Oh, I missed that part.

Too bad she never told us how to do that. Sad

%.2f instead of %f.
(Read up on "printf format")
Reply
#10
SaptaZapta Wrote:%.2f instead of %f.
(Read up on "printf format")

Ooh, did not know that was a thing. I was under the impression there was only %d/%f/%s. I'll definitely read up more on that.

Thanks!
Reply
#11
I've seen you study some pretty in-depth CS classes, how come you're still on a pretty basic programming level?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)