Define a class whose objects are records on animal species. The class should have instance variables for the species name, population, and growth rate. The growth rate is a percentage that can be positive or negative and can exceed 100%. Include a suitable collection of constructors, mutator methods, and accessor methods. Include a toString method and an equals method. Include a boolean valued method named endangered that returns true when the growth rate is negative and returns false otherwise. Write a test program (or programs) that tests each method in your class definition.
Java Program:
public class AnimalSpecies
{
private String species;
private double growth;
private int population;
public AnimalSpecies(String str)
{
this.species = str;
population = 0;
growth = 0;
}
public AnimalSpecies(String s, int p)
{
this.species = s;
if (p>= 0)
this.population = p;
else
this.population = 0;
growth = 0;
}
public AnimalSpecies(String s, int p, double g)
{
this.species = s;
if (p >= 0)
this.population = p;
else
this.population = 0;
this.growth = g;
}
public void setSpecies(String s)
{
species = s;
}
public void setPopulation(int p )
{
if (p >= 0)
population = p;
else
population = 0;
}
public void setgrowth(double r)
{
growth = r;
}
public String getSpeciesName()
{
return species;
}
public int getPopulation()
{
return population;
}
public double getgrowth()
{
return growth;
}
public String toString()
{
return "There are " + population + " " + species + "(s) growing at a ratee of " + growth + "%";
}
public boolean equals(AnimalSpecies as)
{
return species.equals(as.species);
}
//a boolean valued method named endangered that returns true
//when the growth rate is negative and returns false otherwise.
public boolean endangered()
{
return growth < 0;
}
public static void main(String[] args)
{
AnimalSpecies a1 = new AnimalSpecies("Tiger");
AnimalSpecies a2 = new AnimalSpecies("Snake", 50);
AnimalSpecies a3 = new AnimalSpecies("Mouse", 124, 2.5);
AnimalSpecies a4 = new AnimalSpecies("Mouse");
a1.setPopulation(20);
a1.setgrowth(-1.5);
a2.setgrowth(4.5);
System.out.println(a2.toString());
System.out.println(a3.toString());
System.out.println(a4.toString());
System.out.println();
System.out.println(a1.getSpeciesName() + " statistics: ");
System.out.println("Growth Rate: " + a1.getgrowth());
System.out.println("Population: " + a1.getPopulation());
System.out.println();
System.out.println("Endangered status: ");
System.out.println(a1.getSpeciesName() + ": " + a1.endangered());
System.out.println(a2.getSpeciesName() + ": " + a2.endangered());
System.out.println(a3.getSpeciesName() + ": " + a3.endangered());
System.out.println();
System.out.println(a3.equals(a4));
System.out.println(a3.equals(a1));
System.out.println(a3.equals(a2));
}
}
Result Output:
There are 50 Snake(s) growing at a ratee of 4.5%
There are 124 Mouse(s) growing at a ratee of 2.5%
There are 0 Mouse(s) growing at a ratee of 0.0%
Tiger statistics:
Growth Rate: -1.5
Population: 20
Endangered status:
Tiger: true
Snake: false
Mouse: false
true
false
false