Implement a class InsectPopulation that simulates the growth of a population of
ID: 3629168 • Letter: I
Question
Implement a class InsectPopulation that simulates the growth of a population of insects. Your class will have one instance variable for the size of the population. It will have the following methods:* A constructor that has one parameter for the size of an insect population to be constructed.
* A breed method that simulates a period where the insects double their population by breeding.
* A spray method that simulates spraying the insects with an insecticide, which reduces their population by 10%.
* A getSize method that returns the current number of insects.
* A display method that calls the getSize method and then prints the current population of the insects.
* A main method that constructs a population of 10 insects and then iterates 8 times (using a for-loop, over the sequence of actions breed, spray, and display.
Explanation / Answer
Please rate...
Program "InsectPopluation.java"
==============================================================
class InsectPopulation
{
double sizePopulation;
InsectPopulation(double s)
{
sizePopulation=s;
}
public void breed()
{
sizePopulation=2*sizePopulation;
}
public void spray()
{
sizePopulation=sizePopulation-(0.1*sizePopulation);
}
public double getSize()
{
return sizePopulation;
}
public void display()
{
System.out.printf("The current population of insects is: %.2f ",sizePopulation);
}
}
==========================================================
Program "TestInsectPopulation.java"
==========================================================
class TestInsectPopulation
{
public static void main(String args[])
{
int i;
InsectPopulation is=new InsectPopulation(10);
for(i=1;i<=8;i++)
{
System.out.println("Iteration #"+i);
is.breed();
System.out.println("After Breeding #"+i);
is.display();
is.spray();
System.out.println("After Spraying #"+i);
is.display();
}
}
}
=========================================================
Sample output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.