PDA

View Full Version : Elements 6th Java Assignment



Element17
10-10-2013, 11:26 PM
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?



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");
}
}
}

euphemism
10-10-2013, 11:41 PM
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?

Element17
10-10-2013, 11:49 PM
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.

euphemism
10-10-2013, 11:58 PM
Well, I'm thinking a cleaner way of going about it might be something like:


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.