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

Assignment: The class RoachPopulation simulates the growth of a roach population

ID: 3726270 • Letter: A

Question

Assignment:

The class RoachPopulation simulates the growth of a roach population. The constructor takes the size of the initial roach population. The breed method simulates a period in which roaches breed, which doubles their population. The spray method simulates spraying with insecticide, which reduces the population by 10%. The getRoaches method returns the current number of roaches. A program called, RoachSimulation simulates a population that starts out with a specific number of roaches. Breed, spray and print the roach count. Repeat three more times.

Sample output:

Enter the number of roaches in the population

25

Creating the Roach Population

The initial size of the population is 25

Breeding and Spraying

After breeding and spraying 1 times the roach count is 45

Breeding and Spraying

After breeding and spraying 2 times the roach count is 81

Breeding and Spraying

After breeding and spraying 3 times the roach count is 146

Breeding and Spraying

After breeding and spraying 4 times the roach count is 263

Include the following for the RoachPopulation class:

1. The instance variable of the class, RoachPopulation

2. The methods of the class, RoachPopulation

3. Include 2 constructors: No-arg and one that accepts the initial size of the population

4. Add getters and setters for instance variables

5. Include the breed and spray methods

Include the following for the RoachSimulation class:

1. A main method which creates the RoachPopulation object, calls all the required methods and prints the output.

2. A Scanner object must be used.

3. The class should work for all values entered by the user.

4. The output should look like the sample output above.

Please note the following requirements and how the assignment will be graded:

Two classes submitted and named as required: RoachPopulation and RoachSimulation

A comment at the start or the program describing what the program does (5 points)

Coding style: All code must be indented for readability and consist of only one statement per line. Naming conventions followed for variables and methods (5 points)

RoachPopulation class (30 points - Full credit provided only if class works as expected)

o Instance variable (5 points)

o Constructors (5 points)

o Getter (5 points)

o Setter (5 points)

o breed method (5 points)

o spray method (5 points)

RoachSimulation class (45 points - Full credit provided only if class works as expected)

o Scanner object (5 points)

o Prompt to user and getting value (5 points)

o RoachPopulation object created (10 points)

o Loop used to breed, spray and print message (5 points)

o Methods breed and spray called correctly (10 points)

o Output matches sample output in format (10 points) Submitted on time (10 points)

Explanation / Answer

RoachPopulation.java

public class RoachPopulation {
// Declaring instance variables
private int initialSize;
private static int count;

// Parameterized constructor
public RoachPopulation(int initialSize) {
super();
this.initialSize = initialSize;
this.count = 1;
}

// getters and setters
public int getInitialSize() {
return initialSize;
}

public void setInitialSize(int initialSize) {
this.initialSize = initialSize;
}

public void spray() {
System.out.println("Breeding and Spraying");
}

public void breed() {
count++;
initialSize = initialSize * (2);
initialSize = initialSize - (int)(initialSize * 0.10);
System.out.println("After breeding and spraying " + count + " times the roach count is " + initialSize);
}

}

________________

RoachSimulation.java

import java.util.Scanner;

public class RoachSimulation {

public static void main(String[] args) {

//Declaring variables

int initialSize;

/*

* Creating an Scanner class object which is used to get the inputs

* entered by the user

*/

Scanner sc = new Scanner(System.in);

//Getting the input entered by the user

System.out.print("Enter the number of roaches in the population :");

initialSize = sc.nextInt();

System.out.println("Creating the Roach Population");

//Creating an instance of RoachPopulation class

RoachPopulation r = new RoachPopulation(initialSize);

//Displaying the initial size

System.out.println("The initial size of the population is " + r.getInitialSize());

for (int i = 1; i <= 4; i++) {

r.spray();

r.breed();

}

}

}

__________________

Output:

Enter the number of roaches in the population :25
Creating the Roach Population
The initial size of the population is 25
Breeding and Spraying
After breeding and spraying 2 times the roach count is 45
Breeding and Spraying
After breeding and spraying 3 times the roach count is 81
Breeding and Spraying
After breeding and spraying 4 times the roach count is 146
Breeding and Spraying
After breeding and spraying 5 times the roach count is 263

______________Thank You

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