Question: JAVA: Write a class encapsulating the concept of a Species. The Species class will have the following attributes: Instance variables/attributes: A string representing the name

JAVA: Write a class encapsulating the concept of a Species. The Species class will have the following attributes:

Instance variables/attributes: A string representing the name of the species An int representing the population of the species A double representing the growth rate of the species

Class variables (static variables): An integer value representing the number of existing species An integer value representing the number of endangered species

Methods: 2 Constructors accessor methods mutator methods toString method equals method Boolean method called endangered. If the growth rate is negative the method returns true otherwise it will return false.

You must also write a client class to test all the methods in the Species class.

UML diagram for the Species class (static members are underlined): Species

- speciesCount: int = 0 - endangeredCount: int = 0 - name: String - population : int - growthRate : double + Species () + Species (String initName, int initPopulation, double initGrowthRate) + setName(String newName) : void + setPopulation(int newPopulation): boolean + setGrowthRate(double newGrowthRate) + getName(): String + getPopulation: int + getGrowthRate: double + getSpeciesCount: int + getEndangeredCount: int + getPercentEndangered() : double + endangered(): boolean + equals( Species otherSpecies ): boolean + toString( ): String Method descriptions:

Species() no-argument constructor sets the name to an empty string, population to zero and growthRate to zero. Increments the static variable speciesCount.

Species (String initName, int initPopulation, double initGrowthRate) explicit-value constructor sets name to initName, calls the setPopulation method to set the population, and sets growthRate to initGrowthRate. Increments the static variable speciesCount and if the growthRate is negative, the static variable endangeredCount is incremented.

setName(String newName) method sets the name to newName.

setPopulation(int newPopulation) if newPopulation >=0 this method sets the population to newPopulation, otherwise it will not change the value of population.

setGrowthRate(double newGrowthRate) sets the growthRate to newGrowthRate. If the newGrowthRate is negative and the old growthRate is positive then the number of endangered species must be incremented. If the newGrowthRate is positive and the old growthRate is negative, then the number of endangered species must be decremented.

getName() returns the name of this species.

getPopulation() returns the population of this species.

getGrowthRate() returns the growthRate of this species.

endangered() returns true if the growthRate is less than zero, otherwise it returns false

getSpeciesCount () static method that returns the total number of existing species.

getEndangeredCount() static method returns the total number of endangered species.

getPercentEndangered() static method returns the percentage of species that are endangered Species.

equals( Species otherSpecies ) method returns true if this species (the one that activated the method) is equal to otherSpecies. Two species objects are equal if all three of their instance variables are equal. otherwise it returns false. NOTE: You cannot use == to compare Strings, you must the String classes equals method.

toString( ) returns a string representing the name, population, and growthRate of a species (see the sample output for the format of the String). Output from the test program should look something like that on the following page.

Output provided is just a sample. Your output must show that you have tested all the methods in the Species class.

CSC15 - Your Name species Class

TESTING constructors and toString METHODS

After using s1 = new Species()

The toString method returned:

Species: , Current Population: 0, Growth Rate: 0%

After using s2 = new Species ("Rabbit", 6750, 1.05);

the toString method returns:

Species: Rabbit, Population: 6750, Growth Rate: 105%

After using s3 = new Species ("Polar Bear", 550, -.02);

the toString method returns: Endangered Species: Polar Bear, Population: 550, Growth Rate: -2%

After using s4 = new Species ("Rabbit", 6750, 1.05);

the toString method returns: Species: Rabbit, Population: 6750, Growth Rate: 105%

TESTING ACCESSOR METHODS:

After using the accessor methods using s3:

Species: Polar Bear

Population: 550

Growth rate: -0.02

Testing getCount():

Number of species is 4

Testing getEndangeredCount()

Number of endangered species is 1

Testing percentEndangered()

25.0% of the species are endangered

TESTING MUTATOR METHODS:

After using setName("Horse") on s1

The toString method returns:

Species: Horse, Current Population: 0, Growth Rate: 0%

After using s3.setGrowthRate(.05)

Number of endangered species is 0

Testing EQUALS METHODS

S2 and s4 are equal S2 and s3 are not equal

**Make sure you use all the methods of the Species class to produce your output**

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!