can someone please double check my results or outcome

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mattg0620
    New Member
    • Sep 2015
    • 4

    #1

    can someone please double check my results or outcome

    this is a population problem that asks for number of starting organisms, the percent increase per day and the number of days allowed to grow. this program is done and fully compiled but i feel like im missing something. can someone double check my equations. this is the entire code. Thanks much

    Code:
    //Programmer: David 
    //CS 75 TTh
    //Assignment #4 Programming Challenge "Population"
    //Due: 9-29-15
    //This program will allow the user to predict the size of
    //the population of an organism by using loops. In addition, the results should
    //be stored in an output file and printed.
    
    
    
    
    import java.util.Scanner; //needed for the Scanner class
    import java.io.*; //needed for the File and IOException
    
    public class Population
    {
    	public static void main (String [] args) throws IOException
    	{
    		double numOfOrganisms; //store user input for starting number of organisms
    		double dailyIncrease; //store user input for percent increase per day
    							  //this needs to be converted to a percent by dividng by 100
    		double startValue; //store for initial number of organisms and daily increase
    		double days; //store user input for the number of days organism will multiply
    
    
    		Scanner keyboard = new Scanner (System.in); //create object for user input
    
    		//ask the user for starting amount of organisms
    		System.out.print("Enter the amount of starting organisms: ");
    		numOfOrganisms = keyboard.nextInt();
    
    		//determine if amount of organisms is valid choice using while loop
    		while (numOfOrganisms < 2)
    		{
    			System.out.println("Invalid input for starting organisms.");
    			System.out.print("Please enter 2 or more starting organisms: ");
    			numOfOrganisms = keyboard.nextInt();
    			System.out.print("You entered a total of " + numOfOrganisms + " organisms\n");
    		}
    
    		//ask the user for percent population increase, will convert to percentage later
    		System.out.print("Enter the average daily percent population increase: ");
    		dailyIncrease = keyboard.nextDouble();
    
    		//determine if daily increase is valid using while loop
    		while (dailyIncrease <= 0)
    		{
    			System.out.println("Invalid input for daily increase.");
    			System.out.print("Please enter a non-negative number for daily increase: ");
    			dailyIncrease = keyboard.nextInt();
    			System.out.println("You entered a total of " + dailyIncrease + " percnet daily increase.");
    		}
    
    		//ask the user number of days
    		System.out.print("Enter the number days for the population to grow: ");
    		days = keyboard.nextInt();
    
    		//determine if days is valid using while loop
    		while (days <= 1)
    		{
    			System.out.println("Invalid input for days entered.");
    			System.out.print("Please enter 2 or more for total days: ");
    			days = keyboard.nextInt();
    			System.out.println("You entered a total of " + days + " days.");
    		}
    
    		//Open the file and create PrintWriter object
    		PrintWriter outputFile = new PrintWriter("PopulationList.txt");
    
    		//design file header for displaying final results
    		outputFile.println("Days\t\t Total Population");
    		outputFile.println("*********************************");
    
    		//store result of initial number of organisms multiplied by daily increase
    		//also divided by 100 to convert to percentage
    		startValue = (dailyIncrease/100)*(numOfOrganisms);
    
    		//use for loop to compute each result based on number of days
    		for ( int i = 0; i < days; i++)
    		{
    
    			System.out.println(i+1 + "\t\t " + numOfOrganisms);
    			outputFile.println(i+1 + "\t\t " + numOfOrganisms);
    			numOfOrganisms = numOfOrganisms + startValue;
    
    
    
    		}
    
    		//Close the file
    		outputFile.close();
    		System.out.println("Data written on the file.");
    
    	}
    }
    
    /*
    C:\Users\Matt\Documents\Java>java Population
    Enter the amount of starting organisms: 11
    Enter the average daily percent population increase: 25
    Enter the number days for the population to grow: 10
    1                11.0
    2                13.75
    3                16.5
    4                19.25
    5                22.0
    6                24.75
    7                27.5
    8                30.25
    9                33.0
    10               35.75
    Data written on the file.
    */
    Last edited by Rabbit; Sep 24 '15, 09:57 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    I feel like im missing something
    Why do you feel that way? You must see something off to feel that way. It's hard for us just guess what's wrong based off something as vague as a feeling. If it's compiling and executing and producing expected output, then there's nothing wrong. If it's not doing one of those 3 things correctly, you need to explain which one of the 3 isn't working correctly and explain the symptoms of what's wrong.

    Also, please use code tags when posting code or formatted data.
    Last edited by Rabbit; Sep 24 '15, 10:05 PM.

    Comment

    • mattg0620
      New Member
      • Sep 2015
      • 4

      #3
      ok sorry dude. i just wanted someone to double check my equations and maybe see any hiccups in my while loops. im just beginning here.

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        No need to be sorry, it's just most of the volunteers on the site don't have time to check every line of code. And while someone might come along with enough time to do that, you're more likely to get an answer if you have something specific you want them to check. Like an error code or erroneous output.

        Since this code looks like it's for a class, the teacher probably gave you sample inputs and outputs to check your code against.

        It looks like you are able to compile and run the code so there's nothing "wrong" with the code in that sense. The only thing you need to do now is to check your outputs against the sample inputs/outputs provided by the teacher. If those match up, then you can rest easy.

        Comment

        • mattg0620
          New Member
          • Sep 2015
          • 4

          #5
          i just want someone to double check my code its not like im asking anyone to do the program for me.

          Comment

          • mattg0620
            New Member
            • Sep 2015
            • 4

            #6
            ok Rabbit sounds good. i feel like this program is good i hope lol. thanks for your time.

            Comment

            • Rabbit
              Recognized Expert MVP
              • Jan 2007
              • 12517

              #7
              And that's fine if that's what you need. Hopefully someone will have the time to do that for you. You are missing one piece of information though if you need them to fully check the code, the expected output given certain inputs.

              Someone can check that your code compiles and runs. But without that last piece of information, they can't be sure that your program is running as expected because they don't know what to expect.

              It sounds like you already have compilation and execution checked. If you post that last piece of information, someone can check the last piece of the puzzle.

              Comment

              • Rabbit
                Recognized Expert MVP
                • Jan 2007
                • 12517

                #8
                I had some time to look at the results. Unfortunately, I can't tell you whether or not they are right with 100% certainty. This is because we are missing the information that I mentioned in post #7.

                I can, however, make guesses. But don't take my guesses as definitive answers until you can check it against the full requirements for the assignment.

                As far as straight math goes, the formula you coded and the results coming out of that formula are correct.

                If the assignment is to code linear growth, then your output is correct.

                If the assignment is to code exponential growth, then your output is incorrect.

                If I had to guess what your assignment is, I would guess that it is to code exponential growth. In which case, the growth accelerates with each generation. While there are situations where you do want to code linear growth, they are rare.

                If you do confirm that you need exponential growth, then you need to recalculate your growth amount (startValue) at each iteration of the loop.

                Comment

                Working...