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

Use CircleOfLife.Java for testing Species.java as you follow these steps: Phase

ID: 3594594 • Letter: U

Question

Use CircleOfLife.Java for testing Species.java as you follow these steps:

Phase 1

Create a Species constructor that takes in a String for its name, an int for its population, and an int for its growth rate. Don't let someone initialize a species with a population above 1500 or below 1, or a growth rate outside of the range of 1 to 20 percent. If the population is initialized below 1, set it to 1, and if above 1500, set it to 1500. Similarly for the growth rate.

Phase 2

Return an appropriate String in the toString method that describes the state of a Species object.

Example String to return in the toString() method:

Name of species: cat

Population: 1200

Growth Rate: 12%

Once you have created a toString method, you can use it to display a Species object stored in a variable s as follows:

System.out.println(s);

which is a shortcut for

System.out.println(s.toString());

Phase 3

Create two species objects of whatever animal that you wish (do this in CircleOfLife.java).

Phase 4

Complete the code for mergeSpecies(Species other). This is a non-static method (as all of the rest of them are) that takes another species as a parameter. This method adds the populations of the two species, changes the name of the species to the concatenation of the two names, and the growth rate to the maximum of the two growth rates

Phase 5

Complete the code for grow() and populationInXYears(int x) (hint: don't change the value of the population for the populationInXYears method -- simply return what the population will be in X years given the growth rate of that species.

EXAMPLE

If the species are rabbits and their growth rate is 10%, population is 100, and x is 2, you should see

The projected population for the rabbits in 2 years will be 121.

Explanation / Answer

Species.java

public class Species {
// Put the instance variable here.
private String name;
private int population;
private int growthRate;
// ....

// Create a Species constructor that takes in a String for its name, an int for
// its population, and an int for its growth rate per year as a percent.

// ....
public Species(String name, int population, int growthRate) {
super();
this.name = name;
if (population >= 1 && population <= 1500) {
this.population = population;
} else if (population < 1) {
this.population = 1;
} else if (population > 1500) {
this.population = 1500;
}
if (growthRate < 1)
this.growthRate = 1;
else if (growthRate > 20) {
this.growthRate = 20;
} else if (growthRate >= 1 && growthRate <= 20) {
this.growthRate = growthRate;
}
}
// mergeSpecies adds the populations of the two species, changes the name
// of the species to the concatenation of the two names, and the growth
// rate to the maximum of the two growth rates.

public void mergeSpecies(Species other) {
this.population += this.population + other.population;
this.name += " " + other.name;

if (this.growthRate > other.growthRate) {
this.growthRate = growthRate;
} else if (other.growthRate > this.growthRate) {
this.growthRate = other.growthRate;
}

}

public int getGrowthRate() {
return growthRate;
}


public int getPopulation() {
return population;
}

public String toString() {

System.out.println("Name of species: " + name);
System.out.println("Population: " + population);
System.out.println("Growth Rate: " + growthRate);

return "";
}

// Increases the population according to the growth rate of the species, i.e.
// updates the population instance variable by adding to it the growth rate/100 times the current population.

public void grow() {
this.population = (int)(population + (population * ((double) growthRate / 100)));
}

// Returns the population of the species in x years according to its growth rate.

public int populationInXYears(int x) {
int tot = population;

for (int i = 0; i < x; i++) {
tot = (int)(tot + (tot * ((double) growthRate / 100)));
}

return tot;

}

public static void main(String[] args) {

// Put simple code here to test the Species class. It is always a good idea to include
// a main method in any class you define.

Species a = new Species("Frog", 100, 10);
System.out.println(a);

a.grow();
System.out.println(a);

System.out.println("a.populationInXYears(10) returns " + a.populationInXYears(10));

Species b = new Species("Rabbit", 10, 23);
a.mergeSpecies(b);
System.out.println("a.mergeSpecies(b) is ");
System.out.println(a);

}
}

__________________

Output:

Name of species: Frog
Population: 100
Growth Rate: 10

Name of species: Frog
Population: 110
Growth Rate: 10

a.populationInXYears(10) returns 281
a.mergeSpecies(b) is
Name of species: Frog Rabbit
Population: 230
Growth Rate: 20

_________________

CircleOfLife.java

public class CircleOfLife {
public static void main(String[] args) {
// Create a new Species object here, passing in the appropriate arguments
Species s = new Species("Lizard", 1000, 12);

// Print out the species' growth rate
System.out.println("Growth Rate :" + s.getGrowthRate());

// Use the species' toString here
System.out.println(s);
// Call populationInXYears here
System.out.println("s.populationInXYears(10) returns " + s.populationInXYears(5));
// Create a new Species object here, passing in the appropriate arguments
// using a very large number for the population (e.g. 100000000)

Species s1 = new Species("Rat", 100000000, 14);
// Print out the species' population to make sure it is set to 1500
System.out.println("Species :" + s.getPopulation());
// Call populationInXYears here, feel free to hardcode in the int to be passed to the method
System.out.println("s1.populationInXYears(8) returns " + s1.populationInXYears(8));
// Call mergeSpecies here. Test that mergeSpecies is doing what you expected it to
s.mergeSpecies(s1);
System.out.println("-- After Merging --");
System.out.println(s);
}

}

__________________

Output:

Growth Rate :12
Name of species: Lizard
Population: 1000
Growth Rate: 12

s.populationInXYears(10) returns 1760
Species :1000
s1.populationInXYears(8) returns 4272
-- After Merging --
Name of species: Lizard Rat
Population: 3500
Growth Rate: 14

________________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