8 Replies - 6140 Views - Last Post: 03 April 2010 - 08:14 PM Rate Topic: -----

#1 Perro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 03-April 10

Population Increase

Posted 03 April 2010 - 05:17 PM

Hi, I have a question involving loops, the question is as follows:

Write a class that will predict the size of a population of organisms. The class should store the starting number of organisms, their average daily population increase (as a percentage), and the number of days they will multiply. The class should have a method that uses a loop to display the size of the population for each day.

Test the class in a program that asks the user for the starting size of the population, their average daily increase, and the number of days they will multiply. The program should display the daily population.

Here is what I got, keep in mind, I'm still a java neophyte.

import java.util.Scanner;

public class Population{
	public static void main(String [] args){
	
		int startingNum;
		int dailyIncrease;
		int days;
		
	
		Scanner scan = new Scanner(System.in);
	
		System.out.println("What is the starting number of organisms?");
		startingNum = scan.nextInt();
		System.out.println("What is their daily population increase?(Percentage)");
		dailyIncrease = scan.nextInt();
		System.out.println("For what number of days will they multiply?");
		days = scan.nextInt();
	
		for(int x=1;x<=days;x++)
		{
		 double avg = startingNum*=dailyIncrease*.01;
		 System.out.println("Popluation increase for day "+x+" = " + avg);
		}
		
		
	}



Is This A Good Question/Topic? 0
  • +

Replies To: Population Increase

#2 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: Population Increase

Posted 03 April 2010 - 05:25 PM

Hi :)
And what is your question?
Do you have trouble with the code? Any errors perhaps?
Was This Post Helpful? 0
  • +
  • -

#3 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Population Increase

Posted 03 April 2010 - 05:46 PM

@PennyBoki I believe the OP is suggesting if the solution they gave is right according to the assignments specification.

@Perro It seems like you have missed out the classes curly bracket, maybe it was a posting error, im not sure.

Anyways you might want to have a look at this line.
double avg = startingNum*=dailyIncrease*.01;


Hint - You see the two = signs :)
Was This Post Helpful? 0
  • +
  • -

#4 Perro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 03-April 10

Re: Population Increase

Posted 03 April 2010 - 05:53 PM

View Postm-e-g-a-z, on 03 April 2010 - 04:46 PM, said:

@PennyBoki I believe the OP is suggesting if the solution they gave is right according to the assignments specification.

@Perro It seems like you have missed out the classes curly bracket, maybe it was a posting error, im not sure.

Anyways you might want to have a look at this line.
double avg = startingNum*=dailyIncrease*.01;


Hint - You see the two = signs :)


Oh yeah, that was my mistake with the extra equals sign haha. I actually am unsure as to whether or not this problem would be better suited for a nested for loop, or if my single for loop can get it done. Any feedback would help and I appreciate it :)
Was This Post Helpful? 0
  • +
  • -

#5 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: Population Increase

Posted 03 April 2010 - 05:55 PM

View Postm-e-g-a-z, on 04 April 2010 - 01:46 AM, said:

I believe the OP is suggesting if the solution they gave is right according to the assignments specification.

Offtopic:
But the suggestion is not very clear to me at least, so by asking the OP we could get more of what exactly OP wants, but also we are pointing to OP to be more specific when asking questions, that way OP could increase the chances for replies. Also consider the impact it could have on other future OPs. Just my thoughts.
Was This Post Helpful? 0
  • +
  • -

#6 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Population Increase

Posted 03 April 2010 - 06:02 PM

What made you bring up this nested for loop idea? From what i can see, you only need a single for loop

@PennyBoki Yep i understand what you're trying to say, maybe i saw different..my bad.
Was This Post Helpful? 0
  • +
  • -

#7 Perro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 03-April 10

Re: Population Increase

Posted 03 April 2010 - 06:12 PM

View PostPennyBoki, on 03 April 2010 - 04:55 PM, said:

View Postm-e-g-a-z, on 04 April 2010 - 01:46 AM, said:

I believe the OP is suggesting if the solution they gave is right according to the assignments specification.

Offtopic:
But the suggestion is not very clear to me at least, so by asking the OP we could get more of what exactly OP wants, but also we are pointing to OP to be more specific when asking questions, that way OP could increase the chances for replies. Also consider the impact it could have on other future OPs. Just my thoughts.


What the question is basically asking is for the user to input a starting number of organisms in a population. For example, I input that the population starts with 2000 organisms. After doing so, the next thing it asks is the percentage at which they increase, for which I say put 75%. The final part is to put the time period (in days) for which they will increase (In the example case, from 2000 to whatever within a time period of 4 days lets say, at a rate of 75%). I am just confused about the algorithm of the for loop, like how to tackle it. If someone just wants to explain to me nested for loops, I'd probably appreciate that alone, because nested for loops are confusing to me heh.
Was This Post Helpful? 0
  • +
  • -

#8 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Population Increase

Posted 03 April 2010 - 06:25 PM

You wont need need another for loop which is nested..


As for your confusion, heres an example. So the inner nested for loop is active whilst the outter loop is active.

public class nested {

	public static void main (String [] args){

		int a=0;
		//outter for loop loops around twice
		for(int i=0;i<2;i++){
			/*inner nested for loop goes around whilst
			outter loop is active which is 4 times*/
			for(int j=0;j<4;j++){
				//incrementing a=a+1..adds 1 each time to a
				a++;

			}
		}

		System.out.println(a);
	}
}



Printing out 8, can you see why it is printing that out? :)

This post has been edited by m-e-g-a-z: 03 April 2010 - 06:31 PM

Was This Post Helpful? 0
  • +
  • -

#9 Perro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 03-April 10

Re: Population Increase

Posted 03 April 2010 - 08:14 PM

View Postm-e-g-a-z, on 03 April 2010 - 05:25 PM, said:

You wont need need another for loop which is nested..


As for your confusion, heres an example. So the inner nested for loop is active whilst the outter loop is active.

public class nested {

	public static void main (String [] args){

		int a=0;
		//outter for loop loops around twice
		for(int i=0;i<2;i++){
			/*inner nested for loop goes around whilst
			outter loop is active which is 4 times*/
			for(int j=0;j<4;j++){
				//incrementing a=a+1..adds 1 each time to a
				a++;

			}
		}

		System.out.println(a);
	}
}



Printing out 8, can you see why it is printing that out? :)


The inner loop goes through all of it's iterations for every one iteration of the outer loop. J increments 4 times, which is equal to one iteration of the outer loop, then does its second set of 4, then the outer loop again. That's what I am getting from it
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1