Write a program that will predict the size of a population of organisms. The pro
ID: 3796778 • Letter: W
Question
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, and the number of days they will multiply. For example, a population might begin with two organisms, have a daily average increase of 50 percent, and will be allowed to multiply for seven 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.
You should get something like the following when you run this program:
User Prompts: (User input in bold)
Enter the starting number of organisms: 2
Enter the daily increase: .50
Enter the number of days the organisms will multiply: 7
Program Output:
Day Organisms
1 2.0
2 3.0
3 4.5
4 6.75
5 10.125
6 15.1875
7 22.78125
Modify the program you just wrote so it writes the output to a file instead of the computer screen. The number of organisms in this version of the program should be a real number rounded to two significant figures after the decimal point. Open your output text file with Notepad or another text editor to confirm the output. 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, and the number of days they will multiply. For example, a population might begin with two organisms, have a daily average increase of 50 percent, and will be allowed to multiply for seven 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.
You should get something like the following when you run this program:
User Prompts: (User input in bold)
Enter the starting number of organisms: 2
Enter the daily increase: .50
Enter the number of days the organisms will multiply: 7
Program Output:
Day Organisms
1 2.0
2 3.0
3 4.5
4 6.75
5 10.125
6 15.1875
7 22.78125
Modify the program you just wrote so it writes the output to a file instead of the computer screen. The number of organisms in this version of the program should be a real number rounded to two significant figures after the decimal point. Open your output text file with Notepad or another text editor to confirm the output. 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, and the number of days they will multiply. For example, a population might begin with two organisms, have a daily average increase of 50 percent, and will be allowed to multiply for seven 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.
You should get something like the following when you run this program:
User Prompts: (User input in bold)
Enter the starting number of organisms: 2
Enter the daily increase: .50
Enter the number of days the organisms will multiply: 7
Program Output:
Day Organisms
1 2.0
2 3.0
3 4.5
4 6.75
5 10.125
6 15.1875
7 22.78125
Modify the program you just wrote so it writes the output to a file instead of the computer screen. The number of organisms in this version of the program should be a real number rounded to two significant figures after the decimal point. Open your output text file with Notepad or another text editor to confirm the output.
Explanation / Answer
// First part of question
import java.util.Scanner;
public class Organism {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the starting number of organisms: ");
int start = sc.nextInt();
if (start < 2)
{
System.err.println("Please enter a number greater than 1");
return;
}
System.out.print("Enter the daily increase: ");
double increase = sc.nextDouble();
if (increase < 0)
{
System.err.println("Please enter a non negative value");
return;
}
System.out.print("Enter the number of days the organisms will multiply: ");
int days = sc.nextInt();
if (days < 1)
{
System.err.println("Please enter a number greater than 0");
return;
}
double newSize = start;
System.out.println("Day Organisms");
for (int i = 0; i < days; i++)
{
System.out.println((i+1) +" " + newSize);
newSize = newSize + newSize*increase;
}
}
}
// Second part
import java.io.FileWriter;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Scanner;
import java.util.logging.Formatter;
public class Organism {
public static void main(String[] args) throws IOException
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the starting number of organisms: ");
int start = sc.nextInt();
if (start < 2)
{
System.err.println("Please enter a number greater than 1");
return;
}
System.out.print("Enter the daily increase: ");
double increase = sc.nextDouble();
if (increase < 0)
{
System.err.println("Please enter a non negative value");
return;
}
System.out.print("Enter the number of days the organisms will multiply: ");
int days = sc.nextInt();
if (days < 1)
{
System.err.println("Please enter a number greater than 0");
return;
}
double newSize = start;
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2);
FileWriter writer = new FileWriter("output.txt");
writer.write("Day Organisms ");
for (int i = 0; i < days; i++)
{
writer.write((i+1) +" " + df.format(newSize) + " ");
newSize = newSize + newSize*increase;
}
writer.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.