Population program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • neonman14
    New Member
    • Feb 2007
    • 7

    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.scann er; // 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.prin t("Please enter a number that is greater than 1 for the population size: ");
    startingSize = keyboardInput.n extInt(System.i n);
    }

    while (dailyIncrease < 0)
    {
    System.out.prin t("Enter a positive integer for the population size increase: ");
    dailyIncrease = keyboardInput.n extInt(System.i n);
    }

    //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.prin tln("Day " + i);
    System.out.prin tln("Number " + temp);
    temp = temp * dailyIncrease;
    }
    Last edited by neonman14; Feb 16 '07, 09:42 PM. Reason: Mispelled word
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    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.scann er;" but the class name is capitalized - it should be "import java.util.Scann er;". 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.

    Comment

    • neonman14
      New Member
      • Feb 2007
      • 7

      #3
      Thank you! So is there anything else I needs to change or add? If you don't mind me asking?!

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        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.

        Comment

        • neonman14
          New Member
          • Feb 2007
          • 7

          #5
          So when I go add that change how will it look? I am confused how it should look!

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            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.

            Comment

            • neonman14
              New Member
              • Feb 2007
              • 7

              #7
              So it should look like this, right?

              import java.util.Scann er; // 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.prin t("Please enter a number that is greater than 1 for the population size: ");
              startingSize = keyboardInput.n extInt(System.i n);
              }

              while (dailyIncrease < 0)
              {
              System.out.prin t("Enter a positive integer for the population size increase: ");
              dailyIncrease = keyboardInput.n extInt(System.i n);
              }

              //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.prin tln("Day " + i);
              System.out.prin tln("Number " + temp);
              Temp = Temp * dailyIncrease;
              }
              }
              }

              Comment

              • Ganon11
                Recognized Expert Specialist
                • Oct 2006
                • 3651

                #8
                That code looks perfectly good to me. Have you tried compiling it and testing it?

                Comment

                • neonman14
                  New Member
                  • Feb 2007
                  • 7

                  #9
                  This is my errors after I compiled it

                  C:\Documents and Settings\Admini strator\My Documents\Popul ation.java:25: cannot find symbol
                  symbol : variable keyboardInput
                  location: class Population
                  startingSize = keyboardInput.n extInt(System.i n);
                  ^
                  C:\Documents and Settings\Admini strator\My Documents\Popul ation.java:31: cannot find symbol
                  symbol : variable keyboardInput
                  location: class Population
                  dailyIncrease = keyboardInput.n extInt(System.i n);
                  ^
                  C:\Documents and Settings\Admini strator\My Documents\Popul ation.java:44: cannot find symbol
                  symbol : variable Temp
                  location: class Population
                  Temp = Temp * dailyIncrease;
                  ^
                  C:\Documents and Settings\Admini strator\My Documents\Popul ation.java:44: cannot find symbol
                  symbol : variable Temp
                  location: class Population
                  Temp = Temp * dailyIncrease;
                  ^
                  4 errors

                  Tool completed with exit code 1

                  Comment

                  • Ganon11
                    Recognized Expert Specialist
                    • Oct 2006
                    • 3651

                    #10
                    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.

                    Comment

                    • neonman14
                      New Member
                      • Feb 2007
                      • 7

                      #11
                      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\Admini strator\My Documents\Popul ation.java:25: cannot find symbol
                      symbol : method nextInt(java.io .InputStream)
                      location: class java.util.Scann er
                      startingSize = keyboard.nextIn t(System.in);
                      ^
                      C:\Documents and Settings\Admini strator\My Documents\Popul ation.java:31: cannot find symbol
                      symbol : method nextInt(java.io .InputStream)
                      location: class java.util.Scann er
                      dailyIncrease = keyboard.nextIn t(System.in);
                      ^
                      2 errors

                      Tool completed with exit code 1

                      Comment

                      • Ganon11
                        Recognized Expert Specialist
                        • Oct 2006
                        • 3651

                        #12
                        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

                        Code:
                        startingSize = keyboard.nextInt();

                        Comment

                        • neonman14
                          New Member
                          • Feb 2007
                          • 7

                          #13
                          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.Scann er; // 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.prin t("Please enter a number that is greater than 1 for the Population Size: ");
                          startingSize = Keyboard.nextIn t();
                          }

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

                          //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.prin tln("Day " + i);
                          System.out.prin tln("Number " + Temp);
                          Temp = Temp * dailyIncrease;
                          }
                          }
                          }

                          Comment

                          • Ganon11
                            Recognized Expert Specialist
                            • Oct 2006
                            • 3651

                            #14
                            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.

                            Comment

                            • dexterdreads
                              New Member
                              • May 2007
                              • 5

                              #15
                              so what was the final code for the population program? Did it work?

                              Comment

                              Working...