468,771 Members | 1,871 Online

# Population program

Hello I am in Intro to Java Programming and I am having problems with assignment. The Homework assignment is called Population.

Population

Write a program that will predict the size of a population of organisms. The program should ask for the starting number of organisms, their average daily population increase (as a percentage), and the number of days they will multiply. For example, a population might begin with two organisms, have an average daily increase of 50%, and will be allowed to multiply for 7 days. The program should use a loop to display the size of the population each day.

Input Validation: Do not accept a number less than two for the starting size of the population. Do not accept a negative number for average daily population increase. Do not accept a number less than one for the number of days they will multiply.

Here is what I have done so far!

import java.util.scanner; // Needed for the scanner class

/**
This program demonstrates a user controlled loop
*/

public class Population

public static void main(String[] args)
{
int Size,
Days,
increase,
number,
Temp;
String input; // To Hold the user's input

//Create a Scanner object for keyboard input.
Scanner Keyboard = new scanner(System.in);

int startingSize = 0, dailyIncrease = 0, numberOfDays = 0;
int temp = 0;

//accept initial user input here

while (startingSize < 2)
{
System.out.print("Please enter a number that is greater than 1 for the population size: ");
startingSize = keyboardInput.nextInt(System.in);
}

while (dailyIncrease < 0)
{
System.out.print("Enter a positive integer for the population size increase: ");
dailyIncrease = keyboardInput.nextInt(System.in);
}

//do the same while loop for numberOfDays as a positive number > 0

temp = startingSize;
dailyIncrease = dailyIncrease + 1;

for(int i = 0; i < numberOfDays; i++)
{
System.out.println("Day " + i);
System.out.println("Number " + temp);
temp = temp * dailyIncrease;
}
Feb 16 '07 #1
14 15149
Ganon11
3,652 Expert 2GB
So far, everything looks fairly OK. I'd like to point out a few things:

1) In your import statement, you say "import java.util.scanner;" but the class name is capitalized - it should be "import java.util.Scanner;". You made a similar mistake when creating your Scanner for input: "Scanner Keyboard = new scanner(System.in);" should be "Scanner Keyboard = new Scanner(System.in);"

2) Your problem specification calls for inputting a percentage for population increase. Right now, if you enter 1, the population will be doubled (because you increase dailyIncrease by 1, and in each 'day', the population is multiplied by dailyIncrease). You should reconfigure your program to treat dailyIncrease as a percentage by dividng it by 100 and storing it into a double before adding 1 to it.
Feb 16 '07 #2
Thank you! So is there anything else I needs to change or add? If you don't mind me asking?!
Feb 17 '07 #3
Ganon11
3,652 Expert 2GB
Only that the first set of variables you create - int Size, Days, increase, number, Temp; and String input; are never used, so you can remove them.

Why don't you compile and run this to see if you're getting the correct results? You can come back with any errors you receive or faulty output, but right now I don't see anything wrong.
Feb 17 '07 #4
So when I go add that change how will it look? I am confused how it should look!
Feb 17 '07 #5
Ganon11
3,652 Expert 2GB
All you will have to do is delete the first 6 lines from your main function where you declare 5 int variables and 1 string variable. These are the only places you use them, so they are the only lines you will have to remove.
Feb 17 '07 #6
So it should look like this, right?

import java.util.Scanner; // Needed for the Scanner class

/**
This program demonstrates a user controlled loop
*/

public class Population
{
public static void main(String[] args)
{

//Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);

int startingSize = 0, dailyIncrease = 0, numberOfDays = 0;
int temp = 0;

//accept initial user input here

while (startingSize < 2)
{
System.out.print("Please enter a number that is greater than 1 for the population size: ");
startingSize = keyboardInput.nextInt(System.in);
}

while (dailyIncrease < 0)
{
System.out.print("Enter a positive integer for the population size increase: ");
dailyIncrease = keyboardInput.nextInt(System.in);
}

//do the same while loop for numberOfDays as a positive number > 0

temp = startingSize;
dailyIncrease = dailyIncrease / 100;

for(int i = 0; i < numberOfDays; i++)
{
System.out.println("Day " + i);
System.out.println("Number " + temp);
Temp = Temp * dailyIncrease;
}
}
}
Feb 17 '07 #7
Ganon11
3,652 Expert 2GB
That code looks perfectly good to me. Have you tried compiling it and testing it?
Feb 17 '07 #8
This is my errors after I compiled it

C:\Documents and Settings\Administrator\My Documents\Population.java:25: cannot find symbol
symbol : variable keyboardInput
location: class Population
startingSize = keyboardInput.nextInt(System.in);
^
C:\Documents and Settings\Administrator\My Documents\Population.java:31: cannot find symbol
symbol : variable keyboardInput
location: class Population
dailyIncrease = keyboardInput.nextInt(System.in);
^
C:\Documents and Settings\Administrator\My Documents\Population.java:44: cannot find symbol
symbol : variable Temp
location: class Population
Temp = Temp * dailyIncrease;
^
C:\Documents and Settings\Administrator\My Documents\Population.java:44: cannot find symbol
symbol : variable Temp
location: class Population
Temp = Temp * dailyIncrease;
^
4 errors

Tool completed with exit code 1
Feb 18 '07 #9
Ganon11
3,652 Expert 2GB
OK, the first two errors occur for the following reason: You declare your Scanner object as keyboard, but then refer to it is keyboardInput - the names don't match.

The second two errors occur because you declare your int variable as temp, but then refer to it as Temp - again, the names don't match.
Feb 18 '07 #10
Thank you! I went and change that and yeah that is just me not paying attention! I went in and changed that and I went and compile it again ofter the chang and I got thes errors:

C:\Documents and Settings\Administrator\My Documents\Population.java:25: cannot find symbol
symbol : method nextInt(java.io.InputStream)
location: class java.util.Scanner
startingSize = keyboard.nextInt(System.in);
^
C:\Documents and Settings\Administrator\My Documents\Population.java:31: cannot find symbol
symbol : method nextInt(java.io.InputStream)
location: class java.util.Scanner
dailyIncrease = keyboard.nextInt(System.in);
^
2 errors

Tool completed with exit code 1
Feb 18 '07 #11
Ganon11
3,652 Expert 2GB
I don;'t think you need to pass System.in to each function of the Scanner - since you used System.in in the constructor, I think you can just use

Expand|Select|Wrap|Line Numbers
1. startingSize = keyboard.nextInt();
Feb 18 '07 #12
Thanks You! I cahnged that and it compiled good! But I don't think it doing what I need it to do!

When I compile it it just asks for a number greater then 1 in Population and after you type 2 or what every number it finished!

here is my compelte assignment with out any errors!

import java.util.Scanner; // Needed for the Scanner class

/**
This program demonstrates a user controlled loop
*/

public class Population
{
public static void main(String[] args)
{
int startingSize = 0, dailyIncrease = 0, numberOfDays = 0;
int Temp = 0;

//Create a Scanner object for keyboard input.
Scanner Keyboard = new Scanner(System.in);

//accept initial user input here

while (startingSize < 2)
{
System.out.print("Please enter a number that is greater than 1 for the Population Size: ");
startingSize = Keyboard.nextInt();
}

while (dailyIncrease < 0)
{
System.out.print("Enter a positive integer for the Population Size Increase: ");
dailyIncrease = Keyboard.nextInt();
}

//do the same while loop for numberOfDays as a positive number > 0

Temp = startingSize;
dailyIncrease = dailyIncrease / 100;

for(int i = 0; i < numberOfDays; i++)
{
System.out.println("Day " + i);
System.out.println("Number " + Temp);
Temp = Temp * dailyIncrease;
}
}
}
Feb 18 '07 #13
Ganon11
3,652 Expert 2GB
This is because of the way you initialize your variables before getting input. Your while() loop will continue while population increase is less than 0, but you initialize it to 0. You should refrain from giving your variables an initial value because you are using user input for all of them.

However, if you simply declare the variables without giving them a value, your while() loops will have some trouble. Change each while() loop to a do...while() loop to fix this. Also, don't forget to add the loop for numberOfDays - currently, you don't input into it, so the loop doesn't execute at all.
Feb 18 '07 #14
so what was the final code for the population program? Did it work?
May 4 '07 #15