Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement a class InsectPopulation that simulates the growth of a population of

ID: 3650275 • 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. Implement a second class called TestInsectPopulation that can test your InsectPopulation class. It will have in it just a main method, which will do the following: The main method will construct a population of 10 insects and then iterate 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:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote