Results 1 to 4 of 4

Thread: Elements 6th Java Assignment

  1. #1
    Join Date
    Jun 2007
    Posts
    532
    Mentioned
    1 Post(s)
    Quoted
    68 Post(s)

    Default Elements 6th Java Assignment

    Write a program that will predict the the size of a population of organisms. The program should ask for the starting number of organisms, their 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 percent, and will be allowed to multiplied for 7 days. The program should use a loop to display the size of the population for each day.

    Input validation: Do not accept a number less than 2 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 1 for the number of days they will multiply.


    My code...but it doesn't work. I get the same output for everyday. EDIT************Needed to remove the short before the expression!
    How do I get it to print just the 48 for day 1?

    java Code:
    import java.util.Scanner;

    /*Andrew Varnick
      10/8/2013
      CSCI 1015
      Programming Assignment 6
      A program that will predict the size of a population of organisms
    */


    public class VarnickPass6
    {
       public static void main(String[] args)
       {
       int input;
       double input2;
       int input3;
       double temp;
     
       Scanner keyboard = new Scanner(System.in);
       
       System.out.println("Enter the starting number organisms.");
       input = keyboard.nextInt();
       
       while (input < 2)
       {
          System.out.println("You must enter at least 2 for the starting number.");
          System.exit(0);
       }
          System.out.println("Enter the daily increase.");
          input2 = keyboard.nextInt();
          while (input2 < 0)
          {
             System.out.println("You cannot enter a negative number for daily growth.");
             System.exit(0);
          }
          System.out.println("Enter the number of days the organims will multiply.");
          input3 = keyboard.nextInt();
          while (input3 < 1)
          {
             System.out.println("You must enter at least 1 day for the number of days.");
             System.exit(0);
          }
          System.out.println("Calculating...");
          System.out.println("\n");
           System.out.println("Day \t  # of Organisms");
           System.out.println("----------------------------");
           
          temp = input;
          input2 =(input2 / 100) + 1.0; //What I had input2 = (short)((input2 / 100) + 1.0);

          for(int i = 1; i <= input3; i++)
          {
             temp = temp * input2;
               System.out.println("\nPopulation increase for day " + i + " is " + temp + "\n");
          }
    }
    }
    Finished B.S. Program in Radiology!!

    Projects: A big one! Total secret! hehe

  2. #2
    Join Date
    Jun 2008
    Location
    United States
    Posts
    818
    Mentioned
    60 Post(s)
    Quoted
    90 Post(s)

    Default

    I'm not sure what all is going on there, it's a bit messy. Can you give more information as to what this '48' is? Did you input 48 as the initial number of organisms?
    [10/14/13:19:03] <BenLand100> this is special relatively, just cleverly disguised with yachts

  3. #3
    Join Date
    Jun 2007
    Posts
    532
    Mentioned
    1 Post(s)
    Quoted
    68 Post(s)

    Default

    Yes 48 is just the number of organisms I decided to input. I'm sorry for the messy code. I planned on cleaning it up later.
    Finished B.S. Program in Radiology!!

    Projects: A big one! Total secret! hehe

  4. #4
    Join Date
    Jun 2008
    Location
    United States
    Posts
    818
    Mentioned
    60 Post(s)
    Quoted
    90 Post(s)

    Default

    Well, I'm thinking a cleaner way of going about it might be something like:

    java Code:
    import java.util.Scanner;

    public class VarnickPass6 {

        public static void main(String[] args) {
           
            /* Constants indicating what the indices of
             * populationInformation contain. */

            final int NUMBER_OF_ORGANISMS = 0;
            final int DAILY_INCREASE      = 1;
            final int NUMBER_OF_DAYS      = 2;
           
            /* Stores the user's input. */
            double[] populationInformation = new double[3];
            boolean isValid = false; //Loop condition
           
            Scanner input = new Scanner(System.in); //Scanner to get user's input.
           
            while (! isValid) {
               
                System.out.println("Enter the starting number of organisms:");
               
                populationInformation[NUMBER_OF_ORGANISMS] = input.nextInt();
               
                if (populationInformation[NUMBER_OF_ORGANISMS] < 2) {
               
                    System.out.println("You must enter at least 2 for the " +
                            "starting number.");
                } else {
                   
                    isValid = true;
                }
            }
           
            isValid = false;
           
            while (! isValid) {
               
                System.out.println("Enter the daily population increase " +
                        "(as a percentage):");
               
                populationInformation[DAILY_INCREASE] = input.nextDouble();
               
                if (populationInformation[DAILY_INCREASE] < 0) {
               
                    System.out.println("You cannot enter a negative number for " +
                            "daily increase.");
                } else {
                   
                    isValid = true;
                }  
            }
           
            isValid = false;
           
            while (! isValid) {
               
                System.out.println("Enter the number of days the organisms " +
                        "will multiply:");
               
                populationInformation[NUMBER_OF_DAYS] = input.nextDouble();
               
                if (populationInformation[NUMBER_OF_DAYS] < 1) {
               
                    System.out.println("You must enter at least 1 for the " +
                            "number of days.");
                } else {
                   
                    isValid = true;
                }  
            }
        }
    }

    Let me know if there's anything else with which I can help.
    [10/14/13:19:03] <BenLand100> this is special relatively, just cleverly disguised with yachts

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •